473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3413
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JW | last post by:
Hi, Below is a description of what I would like to do. Would someone tell me if it is possible and if so, how? I have an array (presumably 'large') that is mallocced in a C function and its...
1
by: Jinming Xu | last post by:
Hello everyone, While embedding my C++ program with Python, I am impeded by the conversion from a Python Tuple to a C++ array. I hope to get some assistance from you guys. I have a sequence...
5
by: vml | last post by:
I have a python com object which contains a method to inverse an array in vb 6 the definition of the class is : class Fop: _public_methods_ = def SqVal(self,*val): #vol=(val,val)...
6
by: mistabean | last post by:
Hello, first of all, I am a programming newbie, especially in python... Onwards to the problem, I have been having difficulty embedding a python module into my C/C++ program. (just a test...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.