Saving / Restoring data in a program

Miki Tebeka mikit at
Mon Nov 24 08:32:44 CET 2003

Hello Bob,

> I've got an application that needs to store (internally) a bunch of data 
>   consisting of lists and some dicts. None of the items are very large, 
> and the entire set (for a single save) is probably less that 1K of data, 
> or a total of several hundred individual objects (mostly short, 4 to 16 
> element, lists of integers).

> So, is there better way? I looked at pickle and it appears to me that 
> this will only work if I store to disk, which I don't really want to do.
Have you looked at pickle's "dumps" and "loads" which store data in

> Guess what I'd like is something like:
>      saved[name] = wonderfulSave(names, stuff, foo,
>                           more_of_my_items/lists)
> and then have it all restored with:
>      wonderfulRestore(saved[name])
> Anything like that around???
As suggested in earlier post, use a class to store all the attributes
and then save it.
class Groove:
    def __init__(self, names, stuff, foo, *extra):
        self.names = names
        self.stuff = stuff = foo
        self.extra = extra

And then save a instance of it using pickle.

> I should mention that the items are part of a class. So, I'm really doing:
>        saved[name] =[:] ... etc.
Another way is to create a dictionary with subset of __dict__
save = {}
for attr in ["names", "stuff", "foo", "extra"]:
    save[attr] = self.__dict__[attr]
saved_attrs = dumps(save)

And save this dictionary with pickle.
Later on...
args = loads(saved_attrs)

> BTW, whoever said that creating proper data types BEFORE writing a 
> program is a good thing was correct. Just wish I'd listened better :)
The important thing is to learn from your mistakes:
"I've missed more than 9,000 shots in my career. I've lost more than
300 games. Twenty-six times I've been trusted to take the game-winning
shot -- and missed. I've failed over and over and over again in my
life.  And that is why I succeed."
Michael Jordan


More information about the Python-list mailing list