saving variables from/for interactive python sessions

Hi Everyone,

This week I started looking into saving my work to a pickled file so I could reload them later and retain their names. In Matlab, all or some variables in a workspace can be saved to a .mat file, and it seems possible to do this with Python. comp.lang.python gave me some ideas, and I wonder if this would be useful to matplotlib.

The primary problems, as I see them, are:

1) all objects are not picklable
2) a variable's class must be defined before instantiating by unpickling
3) others that I am not aware of (well, there's always that)

Have you guys looked into a way to save variables from an interactive session? I'm sure there is an easier way; my not-too-Pythonic solution looks like:

import shelve
myvars = {}
for key in vars().keys():
    myvar = vars()[key]
    if str(type(myvar)) == "<type 'module'>": print "can't pickle <type 'module'>"
    elif: str(type(myvar)) == "<type 'function'>": print "can't pickle <type 'function'>"
    elif: str(type(myvar)) == "<type 'unpicklable'>": print "can't pickle <type 'module'>"
    else: myvars.update({key:myvar})

s ='myvars.mpl',protocol=-1)

and reloading the data:

import shelve
s ='myvars.mpl')
for key in s.keys():
    print type(s[key]), ' is not currently defined.'

What do you think? A try/except PicklingError might be possiblem, but according to the pickling docs, an unspecified number of bytes may have been written to the file before the PicklingError exception is raised.

Also, is there any interest in saving figures as pickled objects, to be reloaded like Matlab's .fig files?



Darren S. Dale

PGP public key available