[Pyrex] newbie list processing question
Robert Bradshaw
robertwb at math.washington.edu
Mon May 5 20:30:30 CEST 2008
On May 5, 2008, at 11:07 AM, Daniel Ashbrook wrote:
> So I've done a lot of Google searching and haven't found an answer to
> this question, or at least one that I understand.
>
> I'm trying to write some fairly simple list-processing code using
> pyrex.
> As a simple example, let's say I wanted to translate this:
>
> def addOne(l):
> assert(isinstance(l, list))
> for i in xrange(l):
> l[i] += 1
>
> or even this:
>
> def addOne(l):
> return [i+1 for i in l]
>
> to fast-running pyrex code.
This last implementation of addOne should work as is in Cython, and
will be nearly optimal (assuming your CPU has reasonable branch
prediction). However, if you are manipulating word-sized integers,
using C arrays will give you a manyfold over python arithmetic.
> How can I actually access the list properly?
> (I should point out that in my real problem, I actually need random
> access to the list.) I've seen some talk of using
> PyObject_AsWriteBuffer
> to do it, but the post was related to array objects, which I'd rather
> not use unless I have to. I've been trying to figure it out using
> PyList_GetItem/SetItem and so on, but it seems extremely
> convoluted, like:
>
> PyList_SetItem(l,i,PyInt_FromLong(PyInt_AsLong(PyList_GetItem(l,i))
> +1))
>
> Surely there is another way! (Plus that line doesn't work - trying to
> use "for i from" gives me errors about making i an integer from a
> pointer without a cast.)
Did you do
cdef int i
for i from 0 <= i < len(L):
...
>
> Any tips? If the person who maintains documentation for pyrex is
> reading, I'd love to see a simple list-processing example included.
In Cython, if one writes
L[i]
where i is a cdef int, then it checks to see at runtime if L is a
list and accesses its elements via a macro. Otherwise one can use
PyList_SetItem and friends, but as you have noticed that is
cumbersome (as well ahs being hard to read).
- Robert
More information about the Pyrex
mailing list