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

array of class attributes

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
Jul 18 '05 #1
1 1856
be*******@aol.com wrote in message news:<30**************************@posting.google. com>...
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[i])
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
Jul 18 '05 #2

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Kris | last post by:
Hi All, I just tried to do something that I thought would be quite simple in C++ and discovered (I think) that it's not possible. I did a bunch of reading and everything that I've seen seems to...
1
by: Dominic Messenger | last post by:
I am writing a generic XML parser to fill a set of classes that map directly to an XML Schema. I am not using the XML Designer as it doesn't handle elements over attributes too well. I have, for...
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
1
by: parag.gadkari | last post by:
Can a name value collection be serialized as attributes to the Xml element of class containing this name value collection? For eg. if a class pControl: public class pControl { ........
1
by: Jim Michaels | last post by:
=> Array ( => UML:CLASS => open => 5 => Array ( => .:00000000000008EC => quiz_batteries => public
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
10
by: ALKASER266 | last post by:
Hey guyz I have a prac and I am beginner and I did this code> Is my code is complete and if is it not complete how i can complete it? and how i can arrange it more? How I can make my driver to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.