Problems returning data from embedded Python
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Aug 12 19:50:11 EDT 2008
En Tue, 12 Aug 2008 19:48:54 -0300, Cromulent
<cromulent at justextrememetal.com> escribi�:
> On 2008-08-12 05:37:53 +0100, "Gabriel Genellina"
> <gagsl-py2 at yahoo.com.ar> said:
>
>> En Mon, 11 Aug 2008 12:58:00 -0300, Cromulent
>> <cromulent at justextrememetal.com> escribi�:
>>
>>> <snip>
>> Uh? You have a complete API for working with list objects, the
>> functions named PyList_*
>> See http://docs.python.org/api/listObjects.html
>> There is also an abstract layer that works both with lists and tuples:
>> http://docs.python.org/api/sequence.html
>> If that's not what you are after, please provide more details...
>
> Spoke too soon.
>
> Right, I've rewritten the Python program and it returns a tuple of lists
> and one integer. Basically as you saw before, the python program reads a
> file in, splits it into elements that were separated by a comma.
>
> The new program just puts each element into its own list. Here is a line
> from the file I am reading in:
>
> 15-Jul-08,37.70,37.70,36.43,36.88,102600
>
> so basically I have 6 lists and one int (which is the total number of
> lines read by the program) I then return that tuple to the C program.
You don't need the integer - it's the list length, and you can easily ask
that value using PyList_Size.
So you are now returning *columns* from the file, ok?
> After that I call the following:
>
> error = PyArg_ParseTuple(value, "isffffi", &totalLines, &finopen,
> &finclose, &finhigh, &finlow, &finvolume);
>
> but that will only give me the first element of the list. What I would
> like to do is return the total number of lines read separately, then use
> that to allocate a C99 style variable sized array and then loop through
> the call to PyArg_ParseTuple to populate the array. The problem with
> that is that I don't think PyArg_ParseTuple is designed in that way. It
> will put the contents of the list in the variable and not split it up so
> that I can populate an array.
Yes, forget about PyArg_ParseTuple. It's intended to parse function
arguments. Use the appropiate convert function for each object type. For
integers, use PyInt_AsLong; for floats, PyFloat_AsDouble, and so on.
Something like this (untested):
// for a column containing float values:
Py_ssize_t nitems = PyList_Size(the_python_list_of_floats)
// ...allocate the C array...
for (Py_ssize_t i=0; i<nitems; i++) {
PyObject* item = PyList_GetItem(the_python_list_of_floats, i);
your_c_array_of_double[i] = PyFloat_AsDouble(item);
}
--
Gabriel Genellina
More information about the Python-list
mailing list