[Python-Dev] C API for appending to arrays (original) (raw)

Hrvoje Niksic hrvoje.niksic at avl.com
Wed Feb 4 12:57:38 CET 2009


Mike Klaas wrote:

Do you need to append, or are you just looking to create/manipulate an array with a bunch of c-float values?

Mostly real-life examples I've seen of this were creating an array from C values obtained from an external source, such as an on-disk file, or another process. The code example was a (simplified and de-C++-ized) snippet of actual code.

I find As{Write/Read}Buffer sufficient for most of these tasks.

They improve things, as shown in the second example, but they're still cumbersome to use for appending/initialization of the array.

(Note that you can get the size of the resulting c array more easily than you are by using PyObjectLength).

Note that AsWriteBuffer always gives you the buffer size anyway -- you can't pass bufsize==NULL. Since I have to call AsWriteBuffer in each iteration (because I don't know when the buffer will resize), calling PyObject_Length in addition to that doesn't buy much, if anything.

I've included some example pyrex code that populates a new array.array at c speed. [...] cdef int NA NA = len(W1) W0 = array('d', [colTotal]) * NA

The thing is, when reading values from a file or a general iterator, you typically don't know the number of values in advance. If I did, I would probably use an approach similar to yours.

Thanks for the code -- even if it doesn't help in this case, I appreciate it as an instructing example of the advanced usage of Pyrex.



More information about the Python-Dev mailing list