473,898 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I return an array from C to python

17 New Member
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
Mar 19 '07 #1
5 2759
6,596 Recognized Expert Expert
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python
You'll have to remind me which tool you are using. Also, please post an example of what you are working on.
Mar 19 '07 #2
17 New Member
#include <Python.h>
#include <Numeric/arrayobject.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

#define IDENTITY_DOC "\
This method simply unpacks the Array (so that it could be modified)\
and then returns it unchanged."
static PyObject *identity(PyObj ect *self, PyObject *args)
int i,n;
PyObject *input;
PyArrayObject *array;
char *aptr;

if (!PyArg_ParseTu ple(args, "O", &input ))
return NULL;
array = (PyArrayObject *) PyArray_Contigu ousFromObject(i nput, PyArray_INT, 0, 3);
if (array == NULL)
return NULL;

// Compute Size of Array
if(array->nd == 0)
n = 1;
else {
n = 1;
n = n * array->dimensions[i];

aptr=(char*)(ar ray->data);

// Do Your Array Operation(s)

// Return the result
return PyArray_Return( array);

this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in python

And also i had Python2.5
Mar 19 '07 #3
17 New Member
Now i need information about interface c and python.Now i call c function from python but i dont know how to return a array from c to python

this is my samplecode
Expand|Select|Wrap|Line Numbers
  1. #include <Python.h>
  2. #include <Numeric/arrayobject.h>
  3. #include <sys/types.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  7. #define IDENTITY_DOC "\
  8. This method simply unpacks the Array (so that it could be modified)\
  9. and then returns it unchanged."
  10. static PyObject *identity(PyObject *self, PyObject *args)
  11. {
  12. int i,n;
  13. PyObject *input;
  14. PyArrayObject *array;
  15. char *aptr;
  17. if (!PyArg_ParseTuple(args, "O", &input ))
  18. return NULL;
  19. array = (PyArrayObject *) PyArray_ContiguousFromObject(input, PyArray_INT, 0, 3);
  20. if (array == NULL)
  21. return NULL;
  23. // Compute Size of Array 
  24. if(array->nd == 0)
  25. n = 1;
  26. else {
  27. n = 1;
  28. for(i=0;i<array->nd;i++) 
  29. n = n * array->dimensions[i];
  31. aptr=(char*)(array->data);
  33. // Do Your Array Operation(s)
  35. // Return the result
  36. return PyArray_Return(array);
  37. }
this is my wrapper code in c.in this code finally i was return a array (pyArray_Return (array).so how can i get the return value in python

And also i had Python2.5
Mar 19 '07 #4
10 New Member
Did you ever figure this out? I'm new to the forum, but I have the answer if you still need it.
Jun 14 '07 #5
6,596 Recognized Expert Expert
Did you ever figure this out? I'm new to the forum, but I have the answer if you still need it.
Hello, card. Welcome to the Python Forum on TheScripts.

Please feel free to update any posts that may assist others in the future.
We appreciate any contribution and hope to be able to assist you with you queries.

Thanks for joining.
Jun 14 '07 #6

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

Similar topics

by: MangoMan | last post by:
Hi there, Currently i am having a problem with python's array class. I am reading 3 arrays from a file (self.buffer32, self.buffer16, self.buffer8 from self.fs) : # ... self.Allocated32 = offset16 self.Allocated16 = offset8-offset16
by: Martin DeMello | last post by:
It seems to be a fairly common pattern for an object-modifying method to return None - however, this is often quite inconvenient. For instance def f(lst1, lst2): g((lst1 + lst2).reverse()) # doesn't work! you need to say
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, char*buf); Users will get a Numeric Array object and can change its values (and actually change the underlying C array).
by: Java and Swing | last post by:
Hi, I have been posting about writing a C extension for Python...so far, so good. At least for the "simple" functions that I need to wrap. Ok, my c function looks like... MY_NUM *doNumberStuff(const char *in, const char *x) { ... } MY_NUM is defined as, typedef unsigned long MY_NUM; (not sure if that matters, or can i just create a wrapper which handles longs?)
by: Grzegorz Smith | last post by:
Hi all. I'm trying get data from text field in MySQl 5.0 with my National characters. Data are stored in utf8 encodings. Here is the script: import MySQLdb, MySQLdb.cursors conn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='profile_locale') c = conn.cursor(MySQLdb.cursors.DictCursor) c.execute("SET CHARACTER SET utf8") c.execute("SELECT string_value FROM lang_pl_pl WHERE id=8") row = c.fetchone() print row
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the 'list/array' and to compare the existing list information to new lists i'm not sure if i need to import modules, or if the base python install i have is sufficient. an example, or pointer to examples would be good...
by: Boris Mok | last post by:
Hi all, I'm doing a function which needs return an arrary -- or more specially a dictionary data type. I have a sample like this def AFC(): v = 1 return v
by: Santiago Romero | last post by:
Hi :) First of all, I must apologize for my poor english :) I'm starting with python and pygame and for testing (and learning) purposes I wrote an small "Map Editor" for a small game project I'm going to start next month. The tilemap editor is working fine, but reading Guido's Van Rossum PYTHON TUTORIAL I found that my game map is "wasting" memory by using
by: J. Peng | last post by:
what's the difference between an array and a list in python? I see list has all features of array in C or perl. so please tell me.thanks.
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...
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...
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...
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...
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();...
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...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.