473,804 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Py Object *self, PyObject *args)
{

if (!PyArg_ParseTu ple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTupl e!\n");
return Py_None;
}

Thanks.

Apr 14 '06 #1
2 3438
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(Py Object *self, PyObject *args)
{

if (!PyArg_ParseTu ple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTupl e!\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(Py Object *self, PyObject *args)
{
int numItems, i;
PyObject *tuple;

numItems = PyTuple_Size(ar gs);

for(i = 0; i < numItems; ++i)
{
tuple = PyTuple_GetItem (args,i);
if(!PyArg_Parse Tuple(tuple,"sf f",...) {
//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(Py Object *self, PyObject *args)
{

if (!PyArg_ParseTu ple(args, "O", ..... ?????????????) {
printf(" error in PyArg_ParseTupl e!\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(Py Object *self, PyObject *args)
{
int numItems, i;
PyObject *tuple;

numItems = PyTuple_Size(ar gs);

for(i = 0; i < numItems; ++i)
{
tuple = PyTuple_GetItem (args,i);
if(!PyArg_Parse Tuple(tuple,"sf f",...) {
//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
4275
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 values are initialized in that same function. I would like to pass just the pointer to the beginning of the array
1
8540
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 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...
5
4036
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
6
465
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 program before moving on into the real thing). I have been making test runs using the codes from http://docs.python.org/ext/pure-embedding.html as a basic, but modifiying it now as a function inside my C/C++ code.
9
5019
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 something like this, but it did not work, gave core dump. class myclass { public: myclass(){}; ~myclass(){};
0
9706
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
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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
10317
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
10075
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...
1
7615
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
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
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...
3
2990
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.