Saving / Restoring data in a program
Lonnie Princehouse
fnord at u.washington.edu
Fri Nov 21 00:46:32 EST 2003
It's not entirely clear what you're trying to do, but it sounds like
you want to save and restore the state of a subset of local namespace.
First off, rethink this whole scheme. Consider writing a class for
your saved data. Much more Pythonic.
But if you're determined, consider this snippet:
# make some variables
a = 5
b = 10
c = 'foo'
# Names of variables to save
save_vars = ['a','b','c']
# Save them by first dumping their values into a list with
# a list comprehension
saved['store1'] = [(key,locals()[key]) for key in save_vars]
# Restore variables later by injecting them into the
# local namespace dictionary
# (probably considered very bad form!)
for key,value in saved['store1']:
locals()[key] = value
On a side note, it _is_ possible to use pickle without a proper file.
In your case, it doesn't sound like there's any reason to do this, but
it does come in handy upon occasion. Pickle only wants something that
behaves like a file, so you can use a StringIO instead:
import pickle
from StringIO import StringIO
foo = "something that needs to be pickled"
fake_file = StringIO()
# Pickle it
pickle.dump(foo, fake_file)
# Unpickle it
fake_file.seek(0)
bar = pickle.load(fake_file)
Bob van der Poel <bvdpoel at kootenay.com> wrote in message news:<3fbd0127_1 at dns.sd54.bc.ca>...
> 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).
>
> Right now, I'm doing something like:
>
> name = 'Store1'
> saved[name] = {
> 'NAMES' = names[:],
> 'CHORDS' = stuff.copy(),
> .... }
>
> and, then when I want to restore I:
>
> s=saved[name]
> names=s['NAMES']
> stuff = s['CHORDS']
> ...
>
> this is turning into a bit of a mess. It's error prone since I have to
> make sure that I restore everything I save and I have to make sure I
> usethe same spellings. And, I have to be aware of the data types so I
> can decide if [:] or copy() or even copy.deepcopy() is needed.
>
> 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.
>
> 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???
>
> I should mention that the items are part of a class. So, I'm really doing:
>
> saved[name] = self.foo[:] ... etc.
>
> I'm thinking that if I were to put all the stuff into a master-class,
> then I could do something like:
>
> saved[name]= copy.deepcopy(self.masterclass)
>
> but, then I'd have to make sure that I use the 'masterclass' in all my
> references to the data ... and that might just create more confusions
> (well, more typing) and slowdowns.
>
> BTW, whoever said that cre''.join([chr(ord(c)+23) for c in 'UXWWRN)VJPRLYNJLNOJ[V\x17LXV'])
ating proper data types BEFORE writing a
> program is a good thing was correct. Just wish I'd listened better :)
-----------------------------------------------------
-ljp email: eval("''.join([chr(ord(c)+23) for c in
'UXWWRN)VJPRLYNJLNOJ[V\x17LXV'])")
More information about the Python-list
mailing list