On Mon, Jul 19, 2010 at 4:28 PM, Caius Howcroft
<caius.howcr...@gmail.com> wrote:
> Hi all
>
> I'm playing with writing some C code to speed up an inner loop in my
> python code. This loop operates on a numpy record, e.g. soemthing like
> this:
>
> a = numpy.zeros((10,), dtype=[("myfvalue" ,"float"), ("myc", "int8"),
> ("anotheri", "uint64")])
>
> which is then passed into c code like so:
>
> myCFunc(a, "blah")
>
> I was wondering if someone had an example of how to access particular
> columns of "a" in the C func. Clearly, this is going to involve  a
> PyArray_Descr.fields somewhere, but an example would really help make
> things clearer for me.
>
> Thanks
> Regards
>
> Caius

Hi Caius -

Indeed, the info is in the descr.

I attached a header-only C++ class I use to wrap records.  The details
of what you want to do are in the "set_field_info" and "set_sizes"
methods, where the info is extracted from the descr.  Its rather
involved, so I won't write it out in detail here.

The accessor methods called "ptr" retrieve a pointer to row/column you
are interested in.

Note the accessor methods are not type safe, you still need to know what
type you need to cast the pointer to.  This can be determined at runtime
from the type code, which you can get via the typecode() method.  I've
considered adding "get" methods that are type safe and will cast to
the type you want, but that will take more effort.

I hope this helps, and please let me know if you find any bugs.

Erin Sheldon
Brookhaven National Laboratory

P.S. The file NumpyRecords.h is part of
the esutil package:  http://code.google.com/p/esutil/

> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

Attachment: NumpyRecords.h
Description: Binary data

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to