By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

How to convert a Python Tuple into a C/C++ array?

P: n/a
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 of float numbers to be passed from Python to C++, with
indefinite size. So I chose to use Tuple on the Python side. Since
PyArg_ParseTuple() has no appropriate format to parse Tuple (The Tuple I am
saying is not the Tuple of arguments) of arbitray size, I simply passed the
tuple as follows

PyObject* myTuple;
if(!PyArg_ParseTuple(args,"O",&myTuple))
return NULL;

Now I have the pointer, myTuple, to the Tuple passed in. How can I convert
the the entired tuple, myTuple pointing to, into a float array, or convert
an individual element into a C/C++ float number? Are there any conversion
functions?

Any suggestions or hints will be deeply appreciated!

Thanks in advance!

Jinming

__________________________________________________ _______________
Express yourself with the new version of MSN Messenger! Download today -
it's FREE! http://messenger.msn.click-url.com/g...ave/direct/01/
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Here is one way to do it (untested...consider it pseudo-code):

....
my_array = (double*) malloc(sizeof(double) * PyTuple_Size(pyObj));

tupleSize = PyTuple_Size(pyObj);

for( i=0; i < tupleSize; i++ ) {
tupleItem = PyTuple_GetItem(pyObj, i);

if( !PyFloat_Check(tupleItem) ) {
printf("Error: tuple contains a non-float value");
exit(1);
}
my_array[i] = PyFloat_AsDouble(tupleItem);
}
....
Jinming Xu wrote:
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 of float numbers to be passed from Python to C++, with
indefinite size. So I chose to use Tuple on the Python side. Since
PyArg_ParseTuple() has no appropriate format to parse Tuple (The Tuple I
am saying is not the Tuple of arguments) of arbitray size, I simply
passed the tuple as follows

PyObject* myTuple;
if(!PyArg_ParseTuple(args,"O",&myTuple))
return NULL;

Now I have the pointer, myTuple, to the Tuple passed in. How can I
convert the the entired tuple, myTuple pointing to, into a float array,
or convert an individual element into a C/C++ float number? Are there
any conversion functions?

Any suggestions or hints will be deeply appreciated!

Thanks in advance!

Jinming

__________________________________________________ _______________
Express yourself with the new version of MSN Messenger! Download today -
it's FREE! http://messenger.msn.click-url.com/g...ave/direct/01/

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.