473,480 Members | 1,777 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to send python list to C

8 New Member
Hi guys,
I would appreciate your help here. I'm not experienced Python programmer but I have to implement one project that require passing Python list object to C and iterate it somehow.

For example:

in Python: list=[1,2.3,'string']

in C:

I do not know if to use PyArg_ParseTuple is good here:
Expand|Select|Wrap|Line Numbers
  1. static PyObject* pJPMTibPublishALot(PyObject *self, PyObject *args){
  2.     ? myVal;
  3.  
  4.     if (!PyArg_ParseTuple(args, "?", &myVal))
  5.         onError("Crap.")
  6.  
  7.  
  8.     Py_RETURN_NONE;
  9.  
  10. }
Thank you for help !!!
Mar 30 '10 #1
2 1945
Glenton
391 Recognized Expert Contributor
Can you tell us why you want to do this?

Have you looked at shedskin? It takes the python code and compiles it into C.

But if you're trying to do a calculation faster by doing it in C, then it's possible that doing array functions with numpy/scipy will also increase your speed dramatically (ie do whatever calculation to the numpy array, as opposed to looping through the array - in this way the loop is done in the background with C).

The other possibility is to get python to run the C programmes from the command line.

There are supposed to be good ways to call C/C++, but I've never got them to work, to be honest, despite having a book, a couple of days, and the internet. But that was some time ago, and perhaps I'd have more success if I tried now. I've just lumped it in the basket of stuff that's supposed to be possible but appears to be tricky!

Good luck
Mar 31 '10 #2
ilyaw77
8 New Member
Hi Glenton, thank you for the reply.
The thing is we use input from Python for input to another C implemented library. After browsing google I found that I can access list using PyList_ set of functions and I tried this simple app: (again l=[1,2])

Expand|Select|Wrap|Line Numbers
  1. static PyObject* pJPMTibPublishALot(PyObject *self, PyObject *args){
  2.     FILE* file;
  3.     //Py_ssize_t size;
  4.     PyTypeObject* type = args->ob_type;
  5.     Py_ssize_t size=0;
  6.     int i;
  7.  
  8.     //check if type sent is the list
  9.     file=fopen("c:\\python.txt","a+");
  10.     if (type=&PyList_Type){
  11.         fprintf(file,"The container is a LIST\n");
  12.     }
  13.     else{
  14.         fprintf(file,"The container is NOT a LIST\n");
  15.     }
  16.  
  17.     size=PyList_Size(args);
  18.     fprintf(file,"The size_t value is %d\n",(int)size);
  19.  
  20.     fclose(file);
  21.  
  22.     //PyObject* item=PyList_GetItem(args,0);
  23.     //PyObject_Print(item, stdout, 0);
  24.  
  25.     Py_RETURN_NONE;
  26.  
  27. }
in my output file I got:

The container is a LIST
The size_t value is -1

Do you know why the Size method failing ?

Thank you,

Ilya
Mar 31 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6127
by: Alberto Vera | last post by:
Hello: Could you tell me How I can send an email using WIN2000? What do I need? Thanks
5
1564
by: Christian Seberino | last post by:
I already sent a post about problems sending list as argument in a C extension. I think I'll start over and just ask if anyone has done this successfully. Can you send me code that does this??...
0
1684
by: Gary Herron | last post by:
Hi list, Can someone who has built the SpreadModule on a windows machine please send me the results of the build (or just point me an a binary distribution). Here's why: I'm starting to...
1
5047
by: Volkan YAZICI | last post by:
hi all, i've been working on a communication server to talk with an io controller device. i've written a quick and dirty php script a while ago and now i need to port it to python (for a real...
1
2562
by: Christian Otteneuer | last post by:
Hello NG, How can I send an email with an attachment from a python script? My code so far: mailhost = container.mailHost mMsg = 'MessageText' mTo = 'you@you.com' mFrom = 'me@me.com' mSubj...
3
4757
by: ouz as | last post by:
hi, I want to transfer 0 bit and 1 bit in order with pyserial.But pyserial only send string data. Can anyone help me? Sorry about my english.. ...
40
11773
by: ian | last post by:
Hi, I'm a newbie (oh no I can here you say.... another one...) How can I get Python to send emails using the default windows email client (eg outlook express)? I thought I could just do the...
4
9409
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
12
1308
by: Stef Mientki | last post by:
In the example below, "pin" is an object with a number of properties. Now I want 1- an easy way to create objects that contains a number of these "pin" 2- an multiple way to access these "pin",...
4
1987
by: chris | last post by:
I need to maintain a list of subscribers to an email list for a "newsletter" that will be sent via a web form probably once a month. I anticipate low numbers--tens to maybe one hundred subscribers...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
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,...
1
6741
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...
0
6956
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...
0
5342
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,...
1
4783
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...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
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...

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.