473,792 Members | 3,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_ParseTupl e() 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_Parse Tuple(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
1 8538

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

....
my_array = (double*) malloc(sizeof(d ouble) * PyTuple_Size(py Obj));

tupleSize = PyTuple_Size(py Obj);

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_AsDoubl e(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_ParseTupl e() 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_Parse Tuple(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2860
by: Glen Wheeler | last post by:
Hello all, My program uses many millions of integers, and Python is allocating way too much memory for these. I can't have the performance hit by using disk, so I figured I'd write a C extension to define a new type. Problem is, my C knowledge is years old and regardless of my attempts distutils will not recognise my installation of the MS compiler. I am thinking, is there any easier way to use a 5 or 6 bit integer in python? Even a...
1
10869
by: Michal Mikolajczyk | last post by:
Is there a quick way to convert a unicode tuple to a tuple containing python strings? (u'USER', u'NODE', u'HASH', u'IDNBR') to this: ('USER', 'NODE', 'HASH', 'IDNBR') I need to be able to do this for a lot of tuples, not just one.
1
3116
by: Bell, Kevin | last post by:
I'm pulling a range of cells from Excel into a list and the data in Excel is a number or possibly some text like an asterisk. Each member of the list is a com object (I think) and I'm converting them to integers (or to None if not numberic) but my method seems very silly. Is this the best way to go about it? It does exactly what it should, but it seems like a lot of extra BS to convert my object to a string to a float to a rounded...
2
3436
by: imdognuts | last post by:
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:
0
219
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 421 open ( +3) / 3530 closed ( +8) / 3951 total (+11) Bugs : 963 open ( +4) / 6426 closed (+21) / 7389 total (+25) RFE : 255 open ( +5) / 246 closed ( +1) / 501 total ( +6) New / Reopened Patches ______________________
5
4033
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) #mat1=mat((vol)) #up=linalg.inv(mat1) return str(val)#up
0
218
by: dudeja.rajat | last post by:
On Tue, Aug 19, 2008 at 12:40 PM, <dudeja.rajat@gmail.comwrote: Googled and found : s = "v%d.%d.%d.%d" % tuple(version) print s it's working. Result is : v1.2.3.4
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.