473,715 Members | 6,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,PyO bject
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 1877
be*******@aol.c om wrote in message news:<30******* *************** ****@posting.go ogle.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,PyO bject
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__(sel f, i):
return xy(self.points[i])
def __setitem__(sel f, i, (x,y)):
self.points[i,0] = x
self.points[i,1] = y
def __repr__(self):
return repr(self.point s)
# 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
6357
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
2
10552
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 indicate that it's not possible but I thought I'd throw it out to all you c++ gurus to see what you say. Is there any way to initialize a character array in the initialization list for the constructor a class? If this is not possible, can anyone...
1
5664
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 example, a class that looks like this: public class ApplicantsClass { public ApplicantClass Applicant = new ApplicantClass; public class ApplicantClass
0
3173
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 arrGuids(0) = Guid.NewGuid arrGuids(1) = Guid.NewGuid arrGuids(2) = Guid.NewGuid
1
4023
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 { ..... NameValueCollection _dynamicProperties; .....
1
29781
by: Jim Michaels | last post by:
=> Array ( => UML:CLASS => open => 5 => Array ( => .:00000000000008EC => quiz_batteries => public
12
5524
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 directories and updating a database. No interaction with the GUI. Problem is that the system timers do not have a tag property so I can tie in an object. example (old way):
7
13693
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
1886
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 an array that generates an array of 20 students instead of my way of driver to enter it manually? THANK YOU VERY MUCH I will put the question then I will put my answer It is two codes one is Student which is the main one and the other one is...
0
8823
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.