[Pyrex] newbie list processing question

Daniel Ashbrook anjiro at cc.gatech.edu
Mon May 5 20:07:47 CEST 2008


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. 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.)

Any tips? If the person who maintains documentation for pyrex is 
reading, I'd love to see a simple list-processing example included.

Thanks!


dan



More information about the Pyrex mailing list