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. 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| | |