<jupyter_start><jupyter_text>basic usethe next cells show how the %cache magic should be used Note: This notebook requires the the packages scikit-learn, numpy and cache_magic to be installed<jupyter_code>import cache_magic

# delete everthing currently cached
%cache --reset 

# store a new value for a
%cache a = "111"

# fetch the cached value for a
%cache a = "111"
# an examle for an actual use-case
import cache_magic
import numpy as np
from sklearn import svm

%cache --reset

%timeit -n 1 -r 5 %cache -v 1 clf = svm.LinearSVC().fit(np.random.randint(5, size=(5000, 40)), np.random.randint(5, size=(5000)))

# the following 4 cases use the same version

%cache -r

# without explicit version, the expression (=right hand site of assignment) is used as version
%cache a = 0

# if parameter is an integer, it will be the version
%cache -v 0 a = 1 

# if parameter is a variable name, it's value is used as version
my_version = 0
%cache -v my_version a = 1

# new and old version are converted into a string before comparing them
my_version_2 = "0"
%cache -v my_version_2 a = 1
# show everything, that is cached
%cache

# generate some variables
%cache b=3
def fun(x):
    return x+1
%cache c = fun(b)
%cache -v c d = fun(1.1)

# show the new cache
%cache<jupyter_output><empty_output><jupyter_text>power usethe next cells show how the %cache magic can be used<jupyter_code>import cache_magic
import numpy as np
from sklearn import svm
%cache --reset

# even if the expression changes, but not the version, the old value will still be loaded 
# in which case there will be a warning
%cache -v 1 clf = svm.LinearSVC().fit(np.random.randint(5, size=(1000, 40)), np.random.randint(5, size=(1000)))
%cache -v 1 clf = "not a classifier"
print(clf.predict(np.random.randint(5,size=(1,40)))[0])

# without an expression, it will always try to reload the cached value
del clf
%cache -v 1 clf
print(clf.predict(np.random.randint(5,size=(1,40)))[0])

# you can store the current value of a var without an actual statement by assigning it to itself
clf="not a classifier"
%cache -v 2 clf=clf
print(clf)

# while the cache still exists in the file system, the cell can be executed alone
import cache_magic

%cache clf
print(clf)

# the cache is stored in the directory where the kernel was first started in 

import cache_magic
import os

%cache -r
%cache a=1
%cache b=1
%cache c=1

%cache
for root, dirs, files in os.walk(".cache_magic"):
    # there is one folder per cache variable
    print(root)

# if the working dir changes, the .cache-dir stays where it is
%cd ..
%cache
for root, dirs, files in os.walk(".cache_magic"):
    # no output, because no .cache-dir
    print(root)
    

%cd -
%cache
for root, dirs, files in os.walk(".cache_magic"):
    # now we see the cache directory againg
    print(root)
       
# always store a new value and never read from cache
%cache -r a=1

# remove a single variable from cache
%cache -r a

# Error:
%cache a

# load last value if possible, and store new value on miss
%cache a = a

# load last value if possible, but don't store new value on miss
import cache_magic

del a
%cache a
# You can use this magic-module as a regular python module

from cache_magic import CacheCall
cache = CacheCall(get_ipython().kernel.shell)

# setting all parameter by name
cache(
    version="*",
    reset=False,
    var_name="aaa",
    var_value="1+1",
    show_all=False,
    set_debug=True)

# setting all parameter by ordering
cache("1",False,"bbb","1+1",False, False)

# setting parameter selectivly
cache(show_all=True)<jupyter_output>creating new value for variable 'aaa'
creating new value for variable 'bbb'<jupyter_text>development teststhe next cells show how the %cache magic should not be usedthese examples are for debug-purposes only<jupyter_code>#testing successfull calls

import cache_magic

# Dev-Note: use reload, so you don't have to restart the kernel everytime you change the 
from imp import reload
reload(cache_magic)

my_version = 3
%cache --reset

print("   exptecting: new values")
%cache -v 2 a = "ex3"
%cache -v my_version c = "ex3"
%cache --version my_version sadsda = "ex3"
%cache -v 3 a=""
%cache -v 3 -r a=""
%cache -v 3 -r a=""


print("   exptecting: warnings")
%cache -v 3 a= " _ "
%cache -v 3 sadsda = "ex4"

print("   exptecting: stored values")
%cache -v my_version sadsda = "ex3"
%cache -v 3 sadsda = "ex3"

# testing errors

import cache_magic
reload(cache_magic)

%cache -v "a" a = "ex3"
%cache -v a 1=a
# testing loading without storing

import cache_magic
reload(cache_magic)

%cache --reset 
a=1
del a

# error: 
%cache a

%cache a=1
del a
%cache a
del a

# error:
%cache -v '1' a

# error
%cache -v 1 a

%cache --reset 
a=1
del a

# Error
%cache -v 0 a

%cache -v * a=1

# Error:
%cache -v * a

%cache -v "1" a


%cache -v 213 a = "1"
# get stored version via error message
%cache -v * a

# testing debug flag '-d'
%cache -d -v 1 -r a = "1"
%cache -d a = "1"

import cache_magic
from imp import reload
reload(cache_magic)

%cache -r a=1
%cache -r a
%cache -r a=1
import cache_magic
from imp import reload
reload(cache_magic)

%cache -r a = 1
%cache a
print (a)
import cache_magic
from imp import reload
reload(cache_magic)

def foo(x):
    return x+1

%cache --reset
%cache -v * a= foo(3)
%cache -v * a= foo(3)
%cache -v * a= 2

#!pip install -e .<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10450551504954197, 'fraction_numerical': 0.020190689848569827, 'mean_word_length': 3.868061874431301, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '508414', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1855, 'n_words': 769, 'path': 'example.ipynb', 'repo_name': 'dbeach24/ipython-cache', 'size': '5349'}