473,386 Members | 1,720 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,386 software developers and data experts.

member array in c passed to python?

Hi,

I would like to use the Python C API to use python to access functions
originally written in C. The objects that I would like to pass to python
have multi-dimensional arrays.

How do I include arrays as object member? And how is it declared in the
PyMemberDef array?

If you can not use C arrays, I thought perhaps then I should use Python
tuples or lists, but I don't see how to fill the tuple from the
multi-dimensional arrays. Does anyone have an example of this?

Thanks,
William Hanlon

Jul 18 '05 #1
1 2104
William Hanlon wrote:
Hi,

I would like to use the Python C API to use python to access functions
originally written in C. The objects that I would like to pass to python
have multi-dimensional arrays.

How do I include arrays as object member? And how is it declared in the
PyMemberDef array?
You can, if you wish, construct and expose a new "array" type from
within your extension -- or reuse those already constructed for you
by the Numeric package.

If you can not use C arrays, I thought perhaps then I should use Python
tuples or lists, but I don't see how to fill the tuple from the
multi-dimensional arrays. Does anyone have an example of this?


You have to know the number of dimensions and proceed accordingly,
For example, for a 2-dimensional array of integers you could do
something like:

PyObject*
array_to_list_2d(int **array, int N, int M)
{
PyObject * temp;
PyObject * result = PyList_New(N);
for(i = 0; i < N; i++) {
PyList_SET_ITEM(result, i, temp = PyList_NEW(M));
for(j = 0; j < M; j++)
PyList_SET_ITEM(temp, j, PyInt_FromLong(array[i][j]));
}
return result;
}

However, this does need a lot of data copying and object allocation,
which you might obviate if you were to use your own data type (or
reuse Numeric's) to wrap your existing arrays.
Alex

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
12
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is...
9
by: Haobin | last post by:
Hi everyone, I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a string. How do I access each element in...
9
by: gk245 | last post by:
I have something like this: struct block { int x; int y; float z; }
3
by: Sambo | last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
6
by: Peter Wuertz | last post by:
Hi, I'm writing a C module for python, that accesses a special usb camera. This module is supposed to provide python with data (alot of data). Then SciPy is used to fit the data. My question...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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
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...

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.