469,890 Members | 2,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

python to c API, passing a tuple array

Hi,
I want to pass something like this to a C function via the Python C
API.
mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5), ......,
......, )
This tuple is dynamic in size, it needs to be 3 X N dimensions. each
tuple in the
tuple array is of the form (string, float, float) as described above

so from python:

mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5))
api.myCFunction(mytuple)

The C api:
static PyObject *myCFunction(PyObject *self, PyObject *args)
{

if (!PyArg_ParseTuple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTuple!\n");
return Py_None;
}

Thanks.

Apr 14 '06 #1
2 3044
im*******@yahoo.com wrote:
Hi,
I want to pass something like this to a C function via the Python C
API.
mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5), ......,
....., )
This tuple is dynamic in size, it needs to be 3 X N dimensions. each
tuple in the
tuple array is of the form (string, float, float) as described above

so from python:

mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5))
api.myCFunction(mytuple)

The C api:
static PyObject *myCFunction(PyObject *self, PyObject *args)
{

if (!PyArg_ParseTuple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTuple!\n");
return Py_None;
}

Thanks.

Just loop through each item in the arguments and parse the sub-tuple.
Here is some sample code that doesn't do any error checking:

static PyObject *myCFunction(PyObject *self, PyObject *args)
{
int numItems, i;
PyObject *tuple;

numItems = PyTuple_Size(args);

for(i = 0; i < numItems; ++i)
{
tuple = PyTuple_GetItem(args,i);
if(!PyArg_ParseTuple(tuple,"sff",...) {
//handle error
Py_RETURN_NONE;
}
}
}

Also, you need to INCREF Py_None before you return it. Or you can use
the macro used in the sample code above.

-Farshid
Apr 15 '06 #2
Thanks for the solution!

Farshid Lashkari wrote:
im*******@yahoo.com wrote:
Hi,
I want to pass something like this to a C function via the Python C
API.
mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5), ......,
....., )
This tuple is dynamic in size, it needs to be 3 X N dimensions. each
tuple in the
tuple array is of the form (string, float, float) as described above

so from python:

mytuple = (("string_one", 1.2, 1.3), ("string_two", 1.4, 1.5))
api.myCFunction(mytuple)

The C api:
static PyObject *myCFunction(PyObject *self, PyObject *args)
{

if (!PyArg_ParseTuple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTuple!\n");
return Py_None;
}

Thanks.

Just loop through each item in the arguments and parse the sub-tuple.
Here is some sample code that doesn't do any error checking:

static PyObject *myCFunction(PyObject *self, PyObject *args)
{
int numItems, i;
PyObject *tuple;

numItems = PyTuple_Size(args);

for(i = 0; i < numItems; ++i)
{
tuple = PyTuple_GetItem(args,i);
if(!PyArg_ParseTuple(tuple,"sff",...) {
//handle error
Py_RETURN_NONE;
}
}
}

Also, you need to INCREF Py_None before you return it. Or you can use
the macro used in the sample code above.

-Farshid


Apr 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jinming Xu | last post: by
6 posts views Thread by mistabean | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.