Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gwangjinkim/4579898848b0242ed76ed551db037661 to your computer and use it in GitHub Desktop.
Save gwangjinkim/4579898848b0242ed76ed551db037661 to your computer and use it in GitHub Desktop.
How to save entire Python sessions, single objects or only a session's code?
"""
Lisp languages support an image-based development cycle at the REPL.
R - having its roots in Lisp - it was first implemented in Scheme - has overtaken this.
R's base command `save.image(filepath)` can save the state of an entire REPL session in a file,
and next time you start R, you can start where you stopped, by hitting `load(filepath)`.
I was asking myself, since Python's REPL is ipython, what supports
similar functionailities for pythonistas.
Although this is not an exact equivalent of Lisp's Lisp-images,
the effect is similar: You can more or less "freeze" by these measures your session
and go on more or less where you stopped without having to rerun an entire notebook or session.
Which saves tons of time!
The package `dill` seems to be the closest solution.
For single objects to save, the most universal method is pickling.
But JSON-ing is faster - however not all python objects can be JSON-dumped.
The advantage of JSON is that you can import JSONed objects also into other languages.
For more complex objects (from classes), one can use jsonpickle.
And to gain even more speed, msgpack-ing is the fastest solution to save
any JSON-able objects in Python.
"""
###################################################
# save and load session state
###################################################
pip install dill
import dill
filepath = "session.pkl"
dill.dump_session(filepath)
dill.load_session(filepath)
###################################################
# more general solution for linux (can save any terminal session states)
###################################################
# or more universal in linux
# $ pstree -p                # to find PID
# open next to your ipython job
sudo criu dump -t PID --images-dir ~/tmp/imgs --log-file dump.log -v4 --shell-job
sudo criu restore --images-dir ~/tmp/imgs/ --log-file restore.log -v4 --shell-job
###################################################
# JSON dump single objects in python# the advantage of JSON is one can import to other languages, too.# the disadvantage is, not all Python objects can be represented by JSON.# Pickling can save much more python objects, but pickling is Python-specific, moreover# depending on which pickling protocol even python-version-specific.
###################################################
import json
with open("myfile.json", "w") as outfile:
    json.dump(data, outfile) # indent=4 for pretty-printing
with open("myfile.json") as json_file:
    data = json.load(json_file)
##################################################
# JSON-pickling data
##################################################
pip install jsonpickle
pip install git+https://github.com/jsonpickle/jsonpickle.git
# numpy
import jsonpickle.ext.numpy as jnpy
jnpy.register_handlers()
import
###################################################
# pickling single objects
###################################################
import pickle
hp = pickle.HIGHEST_PROTOCOL
dp = pickle.DEFAULT_PROTOCOL
with open(fpath, "wb") as fout:
    pickle.dump(obj, fout, protocol=hp)
with open(fpath, "rb") as fin:
    obj = pickle.load(fpath, protocol=hp)
###################################################
# msgpack is faster # but only JSONable objects
###################################################
# https://www.benfrederickson.com/dont-pickle-your-data/
pip install msgpack
import msgpack
with open(fpath, "wb") as fout:
    fout.write(msgpack.packb(obj))
with open(fpath, "rb") as fin:
    obj = fin.read()
##################################################
# Save and load ipython session's code only
##################################################
%save mysession # append -a appends to existing file # lines: 10-20 23
%load mysession
ipython -i mysession.py
import readline
readline.write_history_file(fpath)
readline.read_history_file(fpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment