473,394 Members | 1,694 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.

in c extension what is easiest way to build a (PyObject) list from an array of doubles?

In c extension what is easiest way to build a (PyObject) list from an
array of doubles?

I don't think I can do "return Py_BuildValue(...) to make a list from
an array can I???

How else can I build and return a list??

Thanks!

Chris+
Jul 18 '05 #1
5 2524
Christian Seberino wrote:
In c extension what is easiest way to build a (PyObject) list from an
array of doubles?


No. Check out the array module.

Paul Prescod

Jul 18 '05 #2
Paul

Thanks. I agree array is great in Python for processing
homogenous lists but what about in **C code** if you must
return a Python list built from a C array?

How do you build a list/tuple/array/ from a C array of numbers?

Chris

On Mon, Feb 02, 2004 at 11:04:13PM -0800, Paul Prescod wrote:
Christian Seberino wrote:
In c extension what is easiest way to build a (PyObject) list from an
array of doubles?


No. Check out the array module.

Paul Prescod


--
_______________________________________

Christian Seberino, Ph.D.
SPAWAR Systems Center San Diego
Code 2872
49258 Mills Street, Room 158
San Diego, CA 92152-5385
U.S.A.

Phone: (619) 553-9973
Fax : (619) 553-6521
Email: se******@spawar.navy.mil
_______________________________________

Jul 18 '05 #3
On Tue, 3 Feb 2004 se******@spawar.navy.mil wrote:
How do you build a list/tuple/array/ from a C array of numbers?


The API docs are what you need to look at.

To create a tuple, call PyTuple_New().

For each element in your C array, build a Python object then use
PyTuple_SetItem() to insert it into the tuple.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #4
Andrew

Thanks. I did it and it looks like it works too.

Chris

On Wed, Feb 04, 2004 at 07:21:15PM +1100, Andrew MacIntyre wrote:
On Tue, 3 Feb 2004 se******@spawar.navy.mil wrote:
How do you build a list/tuple/array/ from a C array of numbers?


The API docs are what you need to look at.

To create a tuple, call PyTuple_New().

For each element in your C array, build a Python object then use
PyTuple_SetItem() to insert it into the tuple.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia


--
_______________________________________

Christian Seberino, Ph.D.
SPAWAR Systems Center San Diego
Code 2872
49258 Mills Street, Room 158
San Diego, CA 92152-5385
U.S.A.

Phone: (619) 553-9973
Fax : (619) 553-6521
Email: se******@spawar.navy.mil
_______________________________________

Jul 18 '05 #5

Chris> In c extension what is easiest way to build a (PyObject) list
Chris> from an array of doubles?

Chris> I don't think I can do "return Py_BuildValue(...) to make a list
Chris> from an array can I???

If the length of the array is known when you write the code I think
something like this will work:

return Py_BuildValue("[dddd]", a[0], a[1], a[2], a[3]);

Chris> How else can I build and return a list??

Something like this (untested, no error checking):

int alen = sizeof(a)/sizeof(a[0]);
PyObject *list_of_floats = PyList_New(alen);
for (i = 0; i < alen; i++) {
PyList_SET_ITEM(list_of_floats, i, PyInt_FromLong(a[i]));
}
return list_of_floats;

Skip

Jul 18 '05 #6

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

Similar topics

8
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
3
by: Peter Sparago | last post by:
(Sorry in advance for the long post.) Hi, I'm having a great deal of difficulty buiding a Python COM extension. I am using the MSHTML ActiveX control in my application but I need to interact...
3
by: Markus Wintermann | last post by:
hello, i try to write an extension which calls an api. however, i haven´t written very much till now, but i`ve already managed to make a mistake. because when i try to call my function,...
0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
10
by: Java and Swing | last post by:
I need to write an extension for a C function so that I can call it from python. C code (myapp.c) ====== typedef unsigned long MY_LONG; char *DoStuff(char *input, MY_LONG *x) { ... } so...
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...
3
by: rimmer | last post by:
I'm writing an extension module in C in which I'm passing an array of floats from C to python. The code below illustrates a simple C function designed to output an array of floats. ---------...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
0
by: Alexander Eisenhuth | last post by:
Hello Together, Shortly what I'm doing: - Extending python with boost.pthon extension - Using python C-Api for datatypes in the extension - extension has a thread (that can be stopped/started)...
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:
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
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...
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
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...
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...
0
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...

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.