[IPython-user] How to save a pylab workspace

Jouni K Seppanen jks at iki.fi
Wed Oct 12 06:22:23 CDT 2005


One Matlab feature I'm missing in pylab is the save and load functions
for saving the workspace to disk. Ipython does have functions called
load and save, but they use an ASCII format, which takes up a lot of
space (and I imagine there would be some loss of accuracy for
floating-point numbers). It also seems to handle only arrays, not e.g. 
dicts. I have some ideas on how to do the equivalent of Matlab's
load/save in pylab, and am writing to ask for the opinions of more
experienced folks: am I missing something obvious, or otherwise doing
something stupid?

What I've been doing is pack up all my variables in a dict, and use
cPickle.dump(the_dict, file, -1) to put it in a file. This seems to
work nicely, but when I load my dict back, I need to type x['foo'] to
get at variable foo. So, I put a trivial class around the dict:

    class struct(object):
        def __init__(self, dict):
            self.__dict__ = dict
        def __getitem__(self, key):
            return self.__dict__[key]

Now I can do

    import cPickle
    from mydefs import struct
    x=cPickle.load(open('mydata.pck'))

and have x.<TAB> work in Ipython (with some clutter from the various
__underscored_names__, but I can live with that). One feature that now
doesn't work is %who/%whos: I can find out that x is a <mydefs.struct
object at 0x...>, but not what's inside. For dicts, %whos is slightly
better in that it shows me as much of the contents as is possible on
one line, but that isn't much. I suppose I should add a __str__ (or
__repr__?) method to struct that displays a summary of the contents
like %whos.

I could of course write a loop that copies the variables to the usual
namespace. However, keeping them bundled in one place seems cleaner to
me, as I can now load several different workspaces into one Python
image without getting problems from name collisions, and if I put all
my important variables inside x, it's easy to save them all back. 
(This is similar to Matlab's x=load('filename') syntax, as opposed to
mere load('filename'), which populates the top-level namespace.)

So, does this seem reasonable, or should I be using some preexisting
functionality in Python or pylab? Would it be useful to incorporate
something like this in pylab?

-- 
Jouni K Seppänen




More information about the IPython-user mailing list