473,799 Members | 2,985 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 2752
bartonc
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
shashi59
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;
for(i=0;i<array->nd;i++)
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
shashi59
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>
  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 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
bartonc
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

2
6781
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
47
3649
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
8
3868
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).
6
6324
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?)
1
2217
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
5
2288
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...
2
28990
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
2091
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
10
11239
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
9686
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
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
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
10222
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
10026
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
9068
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
7564
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
5463
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...
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.