473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I return an array from C to python

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 2727
bartonc
6,596 Expert 4TB
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
#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(PyObject *self, PyObject *args)
{
int i,n;
PyObject *input;
PyArrayObject *array;
char *aptr;

if (!PyArg_ParseTuple(args, "O", &input ))
return NULL;
array = (PyArrayObject *) PyArray_ContiguousFromObject(input, PyArray_INT, 0, 3);
if (array == NULL)
return NULL;

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

aptr=(char*)(array->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
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>
  6.  
  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;
  16.  
  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;
  22.  
  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];
  30.  
  31. aptr=(char*)(array->data);
  32.  
  33. // Do Your Array Operation(s)
  34.  
  35. // Return the result
  36. return PyArray_Return(array);
  37. }
  38.  
  39.  
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
card
10
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
bartonc
6,596 Expert 4TB
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

2
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 =...
47
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()) #...
8
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 =...
6
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...
1
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 =...
5
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...
2
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
8
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...
10
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.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.