473,394 Members | 2,168 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,394 software developers and data experts.

Dynamic objects

Hello,

I have implemented a series of classes representing a Building, its
respective Equipment, and then various Components of that equipment
like so (as you'll be able to tell, I'm a newbie):

class Building:
equipment = {}
def AddEquipment( name, data ):
equipment[ name ] = Equipment( data )

class Equipment:
components = {}
def AddComponent( name, data ):
components[ name ] = Component( data )

class Component:
data = ""

These classes are used like so:

test = Building()
test.AddEquipment( "equipment 1", data )
test.AddEquipment( "equipment 2", data )
test.equipment["equipment 1"].AddComponent( "component 1", data )
test.equipment["equipment 1"].AddComponent( "component 2", data )
test.equipment["equipment 2"].AddComponent( "component 3", data )

But it appears as though the instance of "equipment 1" has ALL of the
components in its components dictionary. I was hoping that the
test.equipment["equipment 1"].components dictionary would only have
those components that were assigned to "equipment 1".

I have implemented __init__ functions for all of the classes, but all
they do is initialize some data that I haven't shown here.

I think I'm trying to use a C++ way of doing this (without the new
operator) so if anyone would be so kind as to help with the Python way
of doing this sort of thing I will be eternally grateful.

Cheers,

Mark Shewfelt

Aug 17 '06 #1
3 1570
Tim


Mark Shewfelt wrote:
Hello,

I have implemented a series of classes representing a Building, its
respective Equipment, and then various Components of that equipment
like so (as you'll be able to tell, I'm a newbie):

class Building:
equipment = {}
def AddEquipment( name, data ):
equipment[ name ] = Equipment( data )

class Equipment:
components = {}
def AddComponent( name, data ):
components[ name ] = Component( data )

class Component:
data = ""

These classes are used like so:

test = Building()
test.AddEquipment( "equipment 1", data )
test.AddEquipment( "equipment 2", data )
test.equipment["equipment 1"].AddComponent( "component 1", data )
test.equipment["equipment 1"].AddComponent( "component 2", data )
test.equipment["equipment 2"].AddComponent( "component 3", data )

But it appears as though the instance of "equipment 1" has ALL of the
components in its components dictionary. I was hoping that the
test.equipment["equipment 1"].components dictionary would only have
those components that were assigned to "equipment 1".

I have implemented __init__ functions for all of the classes, but all
they do is initialize some data that I haven't shown here.

I think I'm trying to use a C++ way of doing this (without the new
operator) so if anyone would be so kind as to help with the Python way
of doing this sort of thing I will be eternally grateful.

Cheers,

Mark Shewfelt

I don't see how your examples could work, helps if you post the actual code.
Try these classes, I think they accomplish what your trying to do.

class Building:
def __init__(self, data = ''):
self.data = data
self.equipment = {}
def AddEquipment(self, name, data ):
self.equipment[ name ] = Equipment( data )

class Equipment:
def __init__(self, data = ''):
self.data = data
self.components = {}
def AddComponent(self, name, data ):
self.components[ name ] = Component( data )

class Component:
def __init__(self, data):
self.data = data

Aug 17 '06 #2
Thanks a lot Tim!

My __init__ functions didn't set the dictionaries like you did below
(e.g. self.equipment = {} ).

Newbie mistake - won't make that one again.

Thanks again,

Mark
Tim wrote:
Mark Shewfelt wrote:
Hello,

I have implemented a series of classes representing a Building, its
respective Equipment, and then various Components of that equipment
like so (as you'll be able to tell, I'm a newbie):

class Building:
equipment = {}
def AddEquipment( name, data ):
equipment[ name ] = Equipment( data )

class Equipment:
components = {}
def AddComponent( name, data ):
components[ name ] = Component( data )

class Component:
data = ""

These classes are used like so:

test = Building()
test.AddEquipment( "equipment 1", data )
test.AddEquipment( "equipment 2", data )
test.equipment["equipment 1"].AddComponent( "component 1", data )
test.equipment["equipment 1"].AddComponent( "component 2", data )
test.equipment["equipment 2"].AddComponent( "component 3", data )

But it appears as though the instance of "equipment 1" has ALL of the
components in its components dictionary. I was hoping that the
test.equipment["equipment 1"].components dictionary would only have
those components that were assigned to "equipment 1".

I have implemented __init__ functions for all of the classes, but all
they do is initialize some data that I haven't shown here.

I think I'm trying to use a C++ way of doing this (without the new
operator) so if anyone would be so kind as to help with the Python way
of doing this sort of thing I will be eternally grateful.

Cheers,

Mark Shewfelt
I don't see how your examples could work, helps if you post the actual code.
Try these classes, I think they accomplish what your trying to do.

class Building:
def __init__(self, data = ''):
self.data = data
self.equipment = {}
def AddEquipment(self, name, data ):
self.equipment[ name ] = Equipment( data )

class Equipment:
def __init__(self, data = ''):
self.data = data
self.components = {}
def AddComponent(self, name, data ):
self.components[ name ] = Component( data )

class Component:
def __init__(self, data):
self.data = data
Aug 17 '06 #3
Mark Shewfelt wrote:
Hello,

I have implemented a series of classes representing a Building, its
respective Equipment, and then various Components of that equipment
like so (as you'll be able to tell, I'm a newbie):

class Building:
equipment = {}
def AddEquipment( name, data ):
equipment[ name ] = Equipment( data )

class Equipment:
components = {}
def AddComponent( name, data ):
components[ name ] = Component( data )

class Component:
data = ""

These classes are used like so:

test = Building()
test.AddEquipment( "equipment 1", data )
test.AddEquipment( "equipment 2", data )
test.equipment["equipment 1"].AddComponent( "component 1", data )
test.equipment["equipment 1"].AddComponent( "component 2", data )
test.equipment["equipment 2"].AddComponent( "component 3", data )

But it appears as though the instance of "equipment 1" has ALL of the
components in its components dictionary. I was hoping that the
test.equipment["equipment 1"].components dictionary would only have
those components that were assigned to "equipment 1".

I have implemented __init__ functions for all of the classes, but all
they do is initialize some data that I haven't shown here.

I think I'm trying to use a C++ way of doing this (without the new
operator) so if anyone would be so kind as to help with the Python way
of doing this sort of thing I will be eternally grateful.

Cheers,

Mark Shewfelt
With the way you defined Building multiple buildings would share
equipment dictionary as it is defined as a class variable and you
want an instance variable (I'm pretty sure). You probably wanted
(not tested):

class Building:
def __init__(self):
self.equipment = {}

def AddEquipment(name, data):
equipment[name]=Equipment(data)

same for Equipment class and Component class.

class Equipment:
def __init__(self):
self.components={}

def AddComponent(name, data):
components[name]=Component(data)
class Component:
def __init__(self, data)
self.data=data
-Larry Bates
Aug 17 '06 #4

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

Similar topics

4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
1
by: Chris Curvey | last post by:
I'm trying to track down a performance issue in my Windows code, and hotshot is telling me that the most time and calls are spent in these methods ncalls tottime percall cumtime percall...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
0
by: Alexandre Bergel | last post by:
Dear colleague, Please, note that after the workshop, best papers will be selected, and a second deadline will then be set regarding preparation of the Electronic Communications of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...

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.