Skip to content

Instantly share code, notes, and snippets.

@Ddedalus
Created January 4, 2019 23:33
Show Gist options
  • Select an option

  • Save Ddedalus/d20c7a6bfd0993060b9c457e8e7f6ac9 to your computer and use it in GitHub Desktop.

Select an option

Save Ddedalus/d20c7a6bfd0993060b9c457e8e7f6ac9 to your computer and use it in GitHub Desktop.
Dump all variables to a json
__loc__ = locals().copy()
def __list_env__(loc):
import sys
import json
from numpy import ndarray
from types import ModuleType
def serialize(x):
if isinstance(x, ndarray):
return str(x.tolist())
else:
return str(x)
ignores = ['In', 'Out', 'get_ipython', 'exit', 'quit', 'variables']
loc = {n:v for (n,v) in loc.items() if not n.startswith("_") and not n.endswith("_")
and n not in ignores and not isinstance(v, ModuleType)}
with open(sys.argv[1] if len(sys.argv) > 1 else "current_vars.json", "w") as file_out:
json.dump(loc, file_out, indent=2, default= serialize)
__list_env__(__loc__)
del __loc__
del __list_env__
# execution in a notebook:
# %run -i list_environment.py custom_dump.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment