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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Seberino

    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

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

    Christian Seberino wrote:
    [color=blue]
    > In c extension what is easiest way to build a (PyObject) list from an
    > array of doubles?[/color]

    No. Check out the array module.

    Paul Prescod



    Comment

    • seberino@spawar.navy.mil

      #3
      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:[color=blue]
      > Christian Seberino wrote:
      >[color=green]
      > >In c extension what is easiest way to build a (PyObject) list from an
      > >array of doubles?[/color]
      >
      > No. Check out the array module.
      >
      > Paul Prescod
      >
      >[/color]

      --
      _______________ _______________ _________

      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
      _______________ _______________ _________

      Comment

      • Andrew MacIntyre

        #4
        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:
        [color=blue]
        > How do you build a list/tuple/array/ from a C array of numbers?[/color]

        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@bullsey e.apana.org.au (pref) | Snail: PO Box 370
        andymac@pcug.or g.au (alt) | Belconnen ACT 2616
        Web: http://www.andymac.org/ | Australia

        Comment

        • seberino@spawar.navy.mil

          #5
          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:[color=blue]
          > On Tue, 3 Feb 2004 seberino@spawar .navy.mil wrote:
          >[color=green]
          > > How do you build a list/tuple/array/ from a C array of numbers?[/color]
          >
          > 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@bullsey e.apana.org.au (pref) | Snail: PO Box 370
          > andymac@pcug.or g.au (alt) | Belconnen ACT 2616
          > Web: http://www.andymac.org/ | Australia
          >[/color]

          --
          _______________ _______________ _________

          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
          _______________ _______________ _________

          Comment

          • Skip Montanaro

            #6
            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

            Comment

            Working...