I'm working inside the Python REPL, and I want to save my work periodically. Does anybody have a script to dump all the variables I have defined? I'm looking for something like this:
for o in dir():
f=open(o)
pickle(o_as_object, f)
This seems like something other people have done, so I wanted to ask before re-inventing the wheel.
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk.
To use pickle, start by importing it in Python. To pickle this dictionary, you first need to specify the name of the file you will write it to, which is dogs in this case. Note that the file does not have an extension. To open the file for writing, simply use the open() function.
Pickle on the other hand is slow, insecure, and can be only parsed in Python. The only real advantage to pickle is that it can serialize arbitrary Python objects, whereas both JSON and MessagePack have limits on the type of data they can write out.
I think you want Dill:
In addition to pickling python objects, dill provides the ability to save the state of an interpreter session in a single command. Hence, it would be feasable to save a interpreter session, close the interpreter, ship the pickled file to another computer, open a new interpreter, unpickle the session and thus continue from the 'saved' state of the original interpreter session.
Note that it's still in alpha, so don't rely on it to store critical data.
Of course, some things can't be restored, such as an open file handle. But you can get quite close.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With