473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thoughts on object representation in a dictionary

py
Say I have classes which represent parts of a car such as Engine, Body,
etc. Now I want to represent a Car in a nested dictionary like...

{string_id:{eng ine_id:engine_o bject, body_id:body_ob ject}}....ok?

Well the other thing is that I am allowed to store strings in this
dictionary...so I can't just store the Engine and Body object and later
use them. ....this is just a requirement (which i dont understand
either)...but its what I have to do.

So my question is there a good way of storing this information in a
nested dictionary such that if I receive this dictionary I could easily
pull it apart and create Engine and Body objects with the information?
Any suggestions at all? keep in mind I am limited to using a
dictionary of strings...thats it.

Thanks for any input.

Dec 9 '05 #1
3 1155
py wrote:
....
Well the other thing is that I am allowed to store strings in this
dictionary...so I can't just store the Engine and Body object and later
use them. ....this is just a requirement (which i dont understand
either)...but its what I have to do.
Homework?
So my question is there a good way of storing this information in a
nested dictionary such that if I receive this dictionary I could easily
pull it apart and create Engine and Body objects with the information?
Any suggestions at all? keep in mind I am limited to using a
dictionary of strings...thats it.


You're describing a task that is named "serialization" . Google for
"python serialization" and you'll get lots of useful and interesting
background to help you out.

-Peter

Dec 9 '05 #2
On Fri, 09 Dec 2005 11:37:30 -0800, py wrote:
Say I have classes which represent parts of a car such as Engine, Body,
etc. Now I want to represent a Car in a nested dictionary like...

{string_id:{eng ine_id:engine_o bject, body_id:body_ob ject}}....ok?

Well the other thing is that I am allowed to store strings in this
dictionary...so I can't just store the Engine and Body object and later
use them. ....this is just a requirement (which i dont understand
either)...but its what I have to do.

So my question is there a good way of storing this information in a
nested dictionary such that if I receive this dictionary I could easily
pull it apart and create Engine and Body objects with the information?
Any suggestions at all? keep in mind I am limited to using a
dictionary of strings...thats it.

Cry and beg and hold your breath until you turn blue unless your
boss/client allows you to store general objects in the dictionary?

Or at least find out *why* they will only let you store strings in the
dictionary. If they won't tell you, tell them that it makes the job
(random big number) harder, which corresponds to costing the client $$$.

Failing that, perhaps you can pickle the objects before you store them in
the dictionary, although I daresay that will hit performance *hard*.
Perhaps use cPickle for speed?

Another possibility would be to build a light-weight serializer into the
engine class itself:

class Engine:
def __init__(self, data):
self.initialisa tion_data = data
process(data)

def __repr__(self):
return "Engine(%s) " % self.initialisa tion_data
# can't get more lightweight than that

engine_object = Engine(data)
s = repr(engine_obj ect)

so that eval(s) recreates the engine_object. Do the same for all the other
classes, and then you don't even need to know what each string means, you
just eval() it and turns into the right sort of object.
(If you use eval, make sure you are aware of the security implications.)

--
Steven.

Dec 10 '05 #3
"py" <co*******@gmai l.com> writes:
Well the other thing is that I am allowed to store strings in this
dictionary...so I can't just store the Engine and Body object and later
use them. ....this is just a requirement (which i dont understand
either)...but its what I have to do.
Probably so that the object store can later be moved offline, with the
shelve module or DB API or something like that.
So my question is there a good way of storing this information in a
nested dictionary such that if I receive this dictionary I could easily
pull it apart and create Engine and Body objects with the information?
Any suggestions at all? keep in mind I am limited to using a
dictionary of strings...thats it.


The spirit of the thing would be serialize the objects, maybe with
cPickle, as Steven suggests. A kludgy way to thwart the policy
might be to store the objects in a list instead of a dictionary:
x[0], x[1], etc. Then have a dictionary mapping keys to subscripts
in the list. The subscripts would be stringified ints: '0','1',... .
Dec 10 '05 #4

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

Similar topics

39
3160
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am sure there are many discussions on the "problems" mentioned here. But I had this thoughts without looking into any forums or anything... it is kind of feedback.
7
5829
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member variables. Similarly I'd like it to be able to populate an XML string from the values of its member...
26
4055
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to the dictionary object. After some tinkering, I whittled it down to the following (complete) ASP... <%@ CodePage=65001 Language="VBScript"%>
3
1738
by: Anders Borum | last post by:
Hello Jon, et all. I am working on a framework with context bound objects (models). The objects expose common functionality, such as the ability to get a serialized Xml representation of an object (and its hierarchy if available). In order to guarantee that wellformed Xml, I use an XmlTextWriter on top of a StringWriter. I didn't even look at a custom serialization service, as that would be like reinventing the wheel (atleast thats how...
1
9250
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
4
2321
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my original dictionary also gets changed and that was not the intention, can someone explain to me why it behaves that way and how do I avoid it, så I van have different dictionary objects? Thanks Betina
21
3746
by: abcd | last post by:
In my code I am debating whether or not to validate the types of data being passed to my functions. For example def sayHello(self, name): if not name: rasie "name can't be null" if not isinstance(name, str): raise "name must be a string" print "Hello " + name
4
2252
by: laxmikiran.bachu | last post by:
Can we have change a unicode string Type object to a Tuple type object.. If so how ????
275
12280
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8603
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,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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
8861
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...
0
5860
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
4369
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.