python to c API, passing a tuple array

Discussion in 'Python' started by imdognuts@yahoo.com, Apr 15, 2006.

  1. Guest

    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 15, 2006
    #1
    1. Advertising

  2. 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
    Farshid Lashkari, Apr 15, 2006
    #2
    1. Advertising

  3. Guest

    Thanks for the solution!

    Farshid Lashkari wrote:
    > 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, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    809
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    954
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    613
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    822
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    964
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page