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 = shelve.open('myvars.mpl',protocol=-1)
s.update(myvars)
s.close()

and reloading the data:

import shelve
s = shelve.open('myvars.mpl')
for key in s.keys():
  try:
    vars().update({key:s[key]})
  except:
    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

···

--
Darren S. Dale
dd55@...163...

PGP public key available