Saving / Restoring data in a program

Bob van der Poel bvdpoel at kootenay.com
Thu Nov 20 12:57:57 EST 2003


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 creating proper data types BEFORE writing a 
program is a good thing was correct. Just wish I'd listened better :)


-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at kootenay.com
WWW:   http://www.kootenay.com/~bvdpoel





More information about the Python-list mailing list