Package: cache-magic
cache-magicThis package adds%cacheline-magic to Jupyter notebooks.Warning!!!The author of this Python package makes no commitment to maintain it. It
was forked fromthis
projectand
you’re probably better off using that! That said, if you like the tweaks
I made (added compression, bug fixes, style preferences, etc.), feel
free to use it how you see fit. Just be sure to respect the original
author’s license (see LICENSE copiedherefor your convenience).QuickstartThe pip-package is calledcache-magicThe python module is calledcache_magicThe magic is called%cacheSo you can run the magic by entering this into an Jupyter cell:!pipinstallcache-magicimportcache_magic%cachea=1+1%cacheinstallationinstall directly from notebookopen jupyter notebookcreate new cellenter!pip installcache-magicexecuteinstall into conda-environmentcondacreate-ntestsourceactivatetestcondainstall-cpyythoncache-magicjupyternotebookusageActivate the magic by loading the module like any other module. Write
into a cellimport cache_magicand excecute it.When you want to apply the magic to a line, just prepend the line with%cacheexample%cache myVar = someSlowCalculation(some, "parameters")This will calculatesomeSlowCalculation(some, "parameters")once.
And in subsequent calls it restores myVar from storage.The magic turns this example into something like this (if there was no
ipython-kernel and no versioning):try:withopen("myVar.pkl.gz",'rb')asfp:myVar=pickle.loads(zlib.decompress(fp.read()))except:myVar=someSlowCalculation(some,"parameters")withopen("myVar.pkl.gz",'wb')asfp:fp.write(zlib.compress(pickle.dumps(myVar)))general form%cache <variable> = <expression>Variable: This Variable’s value will be fetched from cache.Expression: This will only be excecuted once and the result will be
stored to disk.full form%cache [--version <version>] [--reset] [--debug] variable [= <expression>]-v or –version: either a variable name or an integer. Whenever this
changes, a new value is calculated (instead of returning an old value
from the cache).if version is ‘*’ or omitted, the hashed expression is used as version,
so whenever the expression changes, a new value is cached.-r or –reset: delete the cached value for this variable. Forces
recalculation, if<expression>is present-d or –debug: additional loggingshow cache%cacheshows all variables in cache as html-tablefull reset%cache-r%cache--resetdeletes all cached values for all variableswhere is the cache stored?In the directory where the kernel was started (usually where the
notebook is located) in a subfolder called.cachedeveloper Notespush to pypiprepare environment:gedit~/.pypircchmod600~/.pypircsudoaptinstallpandocupload changes to test and production:pandoc-oREADME.rstREADME.mdrestview--pypi-strictREADME.rst# update version in setup.pyrm-rdistpythonsetup.pysdisttwineuploaddist/*-rtestpypifirefoxhttps://testpypi.python.org/pypi/cache-magictwineuploaddist/*test install from testpypipipinstall--index-urlhttps://test.pypi.org/simple/--extra-index-urlhttps://pypi.org/simplecache-magic--no-cache-dir--usertest installationsudopipinstallcache-magic--no-cache-dir--usereditable importInstall into environment with-e:!pipinstall-e.reload after each change:importcache_magicfromimpimportreloadreload(cache_magic)Alternatively (if you don’t want to install python, jupyter & co), you
can use the docker-compose.yml for development:cdcache-magicdocker-composeupcreate Conda Packetrequires the bash with latest anaconda on pathbashmkdirtest&&cdtestcondaskeletonpypicache-magiccondaconfig--setanaconda_uploadyesconda-buildcache-magic-cconda-forgerunning testsbashcondaremove--nametest--allcondaenvcreate-ftest/environment.ymlsourceactivatetestcondaremovecache-magicpipuninstallcache-magicpipinstall-e../test/run_example.pyIf there is any error, it will be printed to stderr and the script
fails.the output can be found in “test/temp”.