extending Python - passing nested lists

Mel mwilson at the-wire.com
Tue Jan 29 11:25:11 EST 2008


Christian Meesters wrote:
>> You didn't mention speed in your original post.
> Sorry, perhaps I considered this self-evident - which it is, of course, not.
> 
>> What about using 
>> array.array?  Unless I am mistaken, these are just a thin wrapper
>> around normal C arrays.
> The algorithm I want to implement requires several million floating point
> operations. Neither the array-modules nor numpy's thin layer seem thin
> enough for me. ;-)
> 
>> Anyway you could always convert your list 
>> into a c array, do lots and lots of fast calculations, then convert it
>> back again to a list.
> I guess I am too blind to see, but I couldn't discover a method description
> like "double* PyList_toDouble". So, yes, my question is a C-API-newbie
> question: What is the way to say "myCarray = SomePyMethod(InputPyList)"? Or
> better, what should be here instead
> static PyObject *_foo(PyObject *self, PyObject *args) {
>   double *v;
>   if (!PyArg_Parse(args, "(d)", &v))
>     return NULL;
> to get a list as an array of doubles into 'v' (and back to Python)?
> 
> I did read the API-description, but still am lost at this point. I presume,
> once I get to know the answer I'll bang my head on the table ... ;-)

I haven't strictly tried this, but PyArg_ParseTuple and Py_BuildValue 
seem to be the orthodox ways to do Python->C and C->Python conversions.
But if Numpy isn't fast enough, then any Python at all in the solution 
might be too much.  Perhaps keeping your values in a file and reading 
them into the C programs will work.

	Mel.



More information about the Python-list mailing list