[Numpy-discussion] Re: [ctypes-users] copying/slicing ctypes arrays, (c_ulong *n)() - to Numeric
rays at blue-cove.com
Thu Feb 3 12:25:47 EST 2005
At 07:32 PM 2/3/2005 +0100, you wrote:
>Don't be confused that the buffer() object says <read-only buffer ...>!
>The buffer call only asks for readable memory..., but ctypes doesn't
>care about the readonly attribute - it will happily write into this
Yes, I was thinking of what the shell error said upon assignment...
Upon adding to some working code all is well:
>>> import Numeric, ctypes, string
>>> N = Numeric.zeros((10,), Numeric.Float)
>>> buf = buffer(N)
<read-only buffer for 0x008F9C28, ptr 0x008D7780, size 80 at 0x008FE220>
>>> int(string.split(repr(buf))[:-1], 16)
## numarray version
# nAddress = int(string.split(repr(N._data)), 16)
## Numeric version
NAddress = int(string.split(repr(buffer(N)))[:-1], 16)
## Load DLL here...
## do this to get data from the USB A/D's DLL
usb.GetData(usb.Sn, (bufferInsertPos * N.itemsize()) + NAddress,
ctypes.byref( (types.c_long *
buffersize)() ) )
Which is faster than getting data into a ctypes array (c_ulong *n)() and
then doing memmove() to Numeric - one less step.
Maybe this snip would be of help to some others, although more so to numpy
Of course, the Python array works the same:
>>> a = array.array('l',[1,2,3])
>>> int(string.split(repr(buffer(a)))[:-1], 16)
>If this is too confusing, and this may well be, ctypes could expose a
>memory() function which would insist on read-write memory, but apart
>from that do the same that buffer does:
No, not confusing, just not clear to a non-expert C person that ctypes
ignores where Python is read-only. A simple note in the tutorial would be
Some over at numpy were also unaware of memmove()s' existence in the new
releases, and seemed interested.
More information about the NumPy-Discussion