Package: ipython-cache
cache magicThis package adds%cacheline-magic to ipython kernels in jupyter
notebooks.QuickstartThe pip-package is calledipython-cacheThe python module is calledcache_magicThe magic is called%cacheSo you can run the magic by entering this into an ipython-cell:!pipinstallipython-cacheimportcache_magic%cachea=1+1%cacheinstallationinstall directly from notebookopen jupyter notebookcreate new cellenter!pip installcache-magicexecuteinstall into conda-environmentcondacreate-ntestsourceactivatetestcondainstall-cjuergensipython-cachejupyternotebookusageActivate 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.txt",'rb')asfp:myVar=pickle.loads(fp.read())except:myVar=someSlowCalculation(some,"parameters")withopen("myVar.txt",'wb')asfp:pickle.dump(myVar,fp)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.cache_magicdeveloper 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/ipython-cachetwineuploaddist/*test install from testpypipipinstall--index-urlhttps://test.pypi.org/simple/--extra-index-urlhttps://pypi.org/simpleipython_cache--no-cache-dir--usertest installationsudopipinstallipython_cache--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:cdipython-cachedocker-composeupcreate Conda Packetrequires the bash with latest anaconda on pathbashmkdirtest&&cdtestcondaskeletonpypiipython-cacheconda-buildipython-cache-cconda-forgeanacondaupload/home/juergens/anaconda3/conda-bld/linux-64/ipython-cache-*running testsbashcondaremove--nametest--allcondaenvcreate-ftest/environment.ymlsourceactivatetestcondaremoveipython-cachepipuninstallipython_cachepipinstall-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”.