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

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:{engine_id:engine_object, body_id:body_object}}....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 1147
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:{engine_id:engine_object, body_id:body_object}}....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.initialisation_data = data
process(data)

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

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

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*******@gmail.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
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...
7
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...
26
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...
3
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...
1
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...
4
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...
21
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...
4
by: laxmikiran.bachu | last post by:
Can we have change a unicode string Type object to a Tuple type object.. If so how ????
275
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.