array of class attributes

Discussion in 'Python' started by beliavsky@aol.com, Apr 5, 2004.

  1. Guest

    Suppose I define a trivial class composed of x and y coordinates and
    create a Numeric array of instances of that class.

    class xy:
    def __init__(self,x=0.0,y=0.0):
    self.x = x
    self.y = y
    def __repr__(self):
    return ("%6.2f" % self.x) + (",%6.2f" % self.y)

    from xy import xy
    from Numeric import zeros,array,PyObject
    n = 3
    aa = array([xy() for i in xrange(n)], PyObject)
    aa[1] = xy(5.0,25.0)
    print aa
    # not legal -- print aa.x

    The output of this program is
    [ 0.00, 0.00 5.00, 25.00 0.00, 0.00 ] .

    I would like to be able to get an array [0.00 5.00 0.00] corresponding
    to the x component of array aa using the syntax aa.x, but this is not
    correct Python. Array slices of components would also be nice. I
    could write a function to return such an array, but I doubt that this
    would be efficient in CPU time or memory. I wonder if this capability
    could be added to Numarray, the successor of Numeric. Otherwise, it
    may be better to write numerical codes using parallel arrays (one for
    x, one for y) instead of classes, and the benefits of classes are
    lost.

    Below is a program showing how this is done in Fortran 95.

    module xy_mod
    implicit none
    type, public :: xy
    real :: x = 0.0, y = 0.0
    end type xy
    end module xy_mod

    program xxy
    use xy_mod, only: xy
    implicit none
    integer, parameter :: n = 3
    type(xy) :: aa(n)
    character (len=*), parameter :: fmt_r="(100(2f6.2,4x))"
    aa(2) = xy(5.0,25.0)
    print fmt_r,aa
    print fmt_r,aa%x
    print fmt_r,aa(1:2)%x
    end program xxy

    The output is

    0.00 0.00 5.00 25.00 0.00 0.00
    0.00 5.00 0.00
    0.00 5.00
     
    , Apr 5, 2004
    #1
    1. Advertising

  2. wrote in message news:<>...
    > Suppose I define a trivial class composed of x and y coordinates and
    > create a Numeric array of instances of that class.
    >
    > class xy:
    > def __init__(self,x=0.0,y=0.0):
    > self.x = x
    > self.y = y
    > def __repr__(self):
    > return ("%6.2f" % self.x) + (",%6.2f" % self.y)
    >
    > from xy import xy
    > from Numeric import zeros,array,PyObject
    > n = 3
    > aa = array([xy() for i in xrange(n)], PyObject)
    > aa[1] = xy(5.0,25.0)
    > print aa
    > # not legal -- print aa.x
    >
    > The output of this program is
    > [ 0.00, 0.00 5.00, 25.00 0.00, 0.00 ] .
    >
    > I would like to be able to get an array [0.00 5.00 0.00] corresponding
    > to the x component of array aa using the syntax aa.x, but this is not
    > correct Python. Array slices of components would also be nice. I
    > could write a function to return such an array, but I doubt that this
    > would be efficient in CPU time or memory. I wonder if this capability
    > could be added to Numarray, the successor of Numeric. Otherwise, it
    > may be better to write numerical codes using parallel arrays (one for
    > x, one for y) instead of classes, and the benefits of classes are
    > lost.


    Here's a way to kinda get what you want:


    from numarray import zeros, Float

    class space(object):
    def __init__(self, n=3):
    self.points = zeros((n,2), Float)
    def __getitem__(self, i):
    return xy(self.points)
    def __setitem__(self, i, (x,y)):
    self.points[i,0] = x
    self.points[i,1] = y
    def __repr__(self):
    return repr(self.points)
    # or, '\n'.join([repr(p) for p in self.points])
    x = property(lambda self: self.points[:,0])
    y = property(lambda self: self.points[:,1])

    class xy(object):
    def __init__(self, loc):
    self.loc = loc
    def __repr__(self):
    return '%2.6f, %2.6f' % (self.x, self.y)

    def _get_x(self): return self.loc[0]
    def _set_x(self, value): self.loc[0] = value
    x = property(_get_x, _set_x)

    def _get_y(self): return self.loc[1]
    def _set_y(self): self.loc[1] = value
    y = property(_get_y, _set_y)

    n = 3
    aa = space(n)
    aa[1] = (5.0, 25.0)
    print aa
    print aa.x

    ---

    Using the array slicing, "array[:,i]" seems to be pretty efficient to
    me. You can also use that syntax for setting values. The xy class
    now is just a convenience wrapper/pointer to its position in space...

    One improvement on this might be to allow slicing of space so you can
    do something like:

    >>> a,b,c = aa[100:103] # to get 3 discreet points
    >>> aa[50:55] = [(1.,2.), (2.,3.), (3.,4.), (4.,5.), (5.,6.)] # to

    set multiple points

    --T
     
    Tracy Ruggles, Apr 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  2. Max
    Replies:
    1
    Views:
    515
    Joe Kesselman
    Sep 22, 2006
  3. P4trykx
    Replies:
    2
    Views:
    1,890
    bruce barker
    Jan 31, 2007
  4. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    412
    Bruno Desthuilliers
    Jun 20, 2007
  5. Kyle Schmitt
    Replies:
    3
    Views:
    225
    Kyle Schmitt
    Jul 24, 2007
Loading...

Share This Page