export an array of floats to python

Larry Bates lbates at websafe.com
Thu Feb 22 12:50:45 EST 2007


Peter Wuertz wrote:
> Hi,
> 
> I'm writing a C module for python, that accesses a special usb camera.
> This module is supposed to provide python with data (alot of data). Then
> SciPy is used to fit the data.
> 
> My question is, how to make python read from a C array? By reading the
> documentation, one could get the impression that PyBufferObjects do that
> job.
> 
> http://docs.python.org/api/bufferObjects.html
> 
> It says:
> "Two examples of objects that support the buffer interface are strings
> and arrays."
> 
> Where this function looks promising:
> "PyBuffer_FromMemory - Return a new read-only buffer object that reads
> from a specified location in memory, with a specified size."
> 
> All right, lets imagine I created a PyBufferObject from my float array
> in the C module, and passed it to python.
> 
> I dont know what to do with a BufferObject in python, I would like to
> "cast" it to a Array of the type float, but Arrays do not have that kind
> of function... they can only be constructed from files or lists.
> 
> Apart from creating these BufferObjects, there is no documentation about
> what to do with them :(
> 
> Thanks for your help!

I've always used the struct module to unpack the C array into the floats
(or ints or strings or ...) that I wanted.

-Larry Bates



More information about the Python-list mailing list