Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Thoughts on object representation in a dictionary

Reply
Thread Tools

Thoughts on object representation in a dictionary

 
 
py
Guest
Posts: n/a
 
      12-09-2005
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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      12-09-2005
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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-10-2005
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.

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      12-10-2005
"py" <> 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',... .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary concept - thoughts on how to do plz! =?Utf-8?B?RGFuIE5hc2g=?= ASP .Net 0 11-08-2004 12:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57