Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   in c extension what is easiest way to build a (PyObject) list from an array of doubles? (http://www.velocityreviews.com/forums/t327814-in-c-extension-what-is-easiest-way-to-build-a-pyobject-list-from-an-array-of-doubles.html)

Christian Seberino 02-03-2004 06:56 AM

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+

Paul Prescod 02-03-2004 07:04 AM

Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?
 
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




seberino@spawar.navy.mil 02-03-2004 09:50 PM

Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?
 
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: seberino@spawar.navy.mil
_______________________________________


Andrew MacIntyre 02-04-2004 08:21 AM

Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?
 
On Tue, 3 Feb 2004 seberino@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: andymac@bullseye.apana.org.au (pref) | Snail: PO Box 370
andymac@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia


seberino@spawar.navy.mil 02-05-2004 08:06 AM

Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?
 
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 seberino@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: andymac@bullseye.apana.org.au (pref) | Snail: PO Box 370
> andymac@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: seberino@spawar.navy.mil
_______________________________________


Skip Montanaro 02-10-2004 08:51 PM

Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?
 

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



All times are GMT. The time now is 11:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.