tags:

views:

89

answers:

2

Hello,

I want to save all the variables in my current python environment. It seems one option is to use the 'pickle' module. However, I don't want to do this for 2 reasons:

1) I have to call pickle.dump() for each variable
2) When I want to retrieve the variables, I must remember the order in which I saved the variables, and then do a pickle.load() to retrieve each variable.

I am looking for some command which would save the entire session, so that when I load this saved session, all my variables are restored. Is this possible?

Thanks a lot!
Gaurav

Edit: I guess I don't mind calling pickle.dump() for each variable that I would like to save, but remembering the exact order in which the variables were saved seems like a big restriction. I want to avoid that.

+2  A: 

What you're trying to do is to hibernate your process. This was discussed already. The conclusion is that there are several hard-to-solve problems exist while trying to do so. For example with restoring open file descriptors.

It is better to think about serialization/deserialization subsystem for your program. It is not trivial in many cases, but is far better solution in long-time perspective.

Although if I've exaggerated the problem. You can try to pickle your global variables dict. Use globals() to access the dictionary. Since it is varname-indexed you haven't to bother about the order.

nailxx
Nopes. I am not trying to hibernate the process. I have an interactive python shell on which I run several scripts and commands. I want to save the output (variables) of some of these commands, so that in future whenever I need access to the output, I can just fire up a python shell and load all these variables.
gveda
So, pickle the dictionary var_name -> var_value
nailxx
Thanks for your answer!
gveda
+2  A: 

If you use shelve, you do not have to remember the order in which the objects are pickled, since shelve gives you a dictionary-like object:

To shelve your work:

import shelve

T='Hiya'
val=[1,2,3]

filename='/tmp/shelve.out'
my_shelf = shelve.open(filename,'n') # 'n' for new

for key in dir():
    try:
        my_shelf[key] = globals()[key]
    except TypeError:
        #
        # __builtins__, my_shelf, and imported modules can not be shelved.
        #
        print('ERROR shelving: {0}'.format(key))
my_shelf.close()

To restore:

my_shelf = shelve.open(filename)
for key in my_shelf:
    globals()[key]=my_shelf[key]
my_shelf.close()

print(T)
# Hiya
print(val)
# [1, 2, 3]
unutbu
Perfect. This is what I was looking for. BTW, I find this sentence in your post super funny: "To shelve your work" :)
gveda
And here I thought "pickles" were funny! :) http://en.wikipedia.org/wiki/Inherently_funny_word
unutbu