@@ -12,60 +12,72 @@ This package adds `%cache` line-magic to ipython kernels in jupyter notebooks.
12
12
13
13
So you can run the magic by entering this into an ipython-cell:
14
14
15
- !pip install ipython-cache
16
- import cache_magic
17
- %cache a = 1+1
18
- %cache
15
+ ``` python
16
+ ! pip install ipython- cache
17
+ import cache_magic
18
+ % cache a = 1 + 1
19
+ % cache
20
+ ```
19
21
20
- ## installation
22
+ # installation
21
23
22
- ### install directly from notebook
24
+ ## install directly from notebook
23
25
24
26
1 . open jupyter notebook
25
27
2 . create new cell
26
28
3 . enter ` !pip install cache-magic `
27
29
4 . execute
28
30
29
- ### install into conda-environment
31
+ ## install into conda-environment
30
32
31
- conda create -n test
32
- source activate test
33
- conda install -c juergens ipython-cache
34
- jupyter notebook
33
+ ``` bash
34
+ conda create -n test
35
+ source activate test
36
+ conda install -c juergens ipython-cache
37
+ jupyter notebook
38
+ ```
35
39
36
- ## usage
40
+ # usage
37
41
38
42
Activate the magic by loading the module like any other module. Write into a cell ` import cache_magic ` and excecute it.
39
43
40
44
When you want to apply the magic to a line, just prepend the line with ` %cache `
41
45
42
- ### example
46
+ ## example
43
47
44
- %cache myVar = someSlowCalculation(some, "parameters")
48
+ ``` python
49
+ % cache myVar = someSlowCalculation(some, " parameters" )
50
+ ```
45
51
46
52
This will calculate ` someSlowCalculation(some, "parameters") ` once. And in subsequent calls it restores myVar from storage.
47
53
48
54
The magic turns this example into something like this (if there was no ipython-kernel and no versioning):
49
55
50
- try:
51
- with open("myVar.txt", 'rb') as fp:
52
- myVar = pickle.loads(fp.read())
53
- except:
54
- myVar = someSlowCalculation(some, "parameters")
55
- with open("myVar.txt", 'wb') as fp:
56
- pickle.dump(myVar, fp)
56
+ ``` python
57
+ try :
58
+ with open (" myVar.txt" , ' rb' ) as fp:
59
+ myVar = pickle.loads(fp.read())
60
+ except :
61
+ myVar = someSlowCalculation(some, " parameters" )
62
+ with open (" myVar.txt" , ' wb' ) as fp:
63
+ pickle.dump(myVar, fp)
64
+ ```
57
65
58
- ### general form
66
+ ## general form
59
67
60
- %cache <variable> = <expression>
68
+ ``` python
69
+ % cache < variable> = < expression>
70
+ ```
61
71
62
72
** Variable** : This Variable's value will be fetched from cache.
63
73
64
74
** Expression** : This will only be excecuted once and the result will be stored to disk.
65
75
66
- ### full form
76
+ ## full form
67
77
68
- %cache [--version <version>] [--reset] [--debug] variable [= <expression>]
78
+ ``` python
79
+ % cache [-- version < version> ] [-- reset] [-- debug] variable [= < expression> ]
80
+ ```
69
81
70
82
** -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).
71
83
@@ -75,16 +87,20 @@ if version is '\*' or omitted, the hashed expression is used as version, so when
75
87
76
88
** -d or --debug** : additional logging
77
89
78
- ### show cache
90
+ ## show cache
79
91
80
- %cache
92
+ ``` python
93
+ % cache
94
+ ```
81
95
82
96
shows all variables in cache as html-table
83
97
84
- ### full reset
98
+ ## full reset
85
99
86
- %cache -r
87
- %cache --reset
100
+ ``` python
101
+ % cache - r
102
+ % cache -- reset
103
+ ```
88
104
89
105
deletes all cached values for all variables
90
106
@@ -99,68 +115,84 @@ In the directory where the kernel was started (usually where the notebook is loc
99
115
100
116
prepare environment:
101
117
102
- gedit ~/.pypirc
103
- chmod 600 ~/.pypirc
104
- sudo apt install pandoc
118
+ ``` bash
119
+ gedit ~ /.pypirc
120
+ chmod 600 ~ /.pypirc
121
+ sudo apt install pandoc
122
+ ```
105
123
106
124
upload changes to test and production:
107
125
108
- pandoc -o README.rst README.md
109
- restview --pypi-strict README.rst
110
- # update version in setup.py
111
- rm -r dist
112
- python setup.py sdist
113
- twine upload dist/* -r testpypi
114
- firefox https://testpypi.python.org/pypi/ipython-cache
115
- twine upload dist/*
126
+ ``` bash
127
+ pandoc -o README.rst README.md
128
+ restview --pypi-strict README.rst
129
+ # update version in setup.py
130
+ rm -r dist
131
+ python setup.py sdist
132
+ twine upload dist/* -r testpypi
133
+ firefox https://testpypi.python.org/pypi/ipython-cache
134
+ twine upload dist/*
135
+ ```
116
136
117
137
test install from testpypi
118
138
119
- pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple ipython_cache --no-cache-dir --user
139
+ ``` bash
140
+ pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple ipython_cache --no-cache-dir --user
141
+ ```
120
142
121
143
test installation
122
144
123
- sudo pip install ipython_cache --no-cache-dir --user
124
-
145
+ ``` bash
146
+ sudo pip install ipython_cache --no-cache-dir --user
147
+ ```
125
148
126
149
## editable import
127
150
128
151
Install into environment with ` -e ` :
129
152
130
- !pip install -e .
153
+ ``` python
154
+ ! pip install - e .
155
+ ```
131
156
132
157
reload after each change:
133
158
134
- import cache_magic
135
- from imp import reload
136
- reload(cache_magic)
159
+ ``` bash
160
+ import cache_magic
161
+ from imp import reload
162
+ reload(cache_magic)
163
+ ```
137
164
138
165
Alternatively (if you don't want to install python, jupyter & co), you can use the docker-compose.yml for development:
139
166
140
- cd ipython-cache
141
- docker-compose up
142
-
167
+ ``` bash
168
+ cd ipython-cache
169
+ docker-compose up
170
+ ```
143
171
144
172
## create Conda Packet
145
173
146
174
requires the bash with latest anaconda on path
147
175
148
- bash
149
- mkdir test && cd test
150
- conda skeleton pypi ipython-cache
151
- conda-build ipython-cache -c conda-forge
152
- anaconda upload /home/juergens/anaconda3/conda-bld/linux-64/ipython-cache-*
176
+ ``` bash
177
+ bash
178
+ mkdir test && cd test
179
+ conda skeleton pypi ipython-cache
180
+ conda-build ipython-cache -c conda-forge
181
+ anaconda upload /home/juergens/anaconda3/conda-bld/linux-64/ipython-cache-*
182
+ ```
153
183
154
184
## running tests
155
185
156
- bash
157
- conda remove --name test --all
158
- conda env create -f test/environment.yml
159
- source activate test
160
- conda remove ipython-cache
161
- pip uninstall ipython_cache
162
- pip install -e .
163
- ./test/run_example.py
186
+ ``` bash
187
+ bash
188
+ conda remove --name test --all
189
+ conda env create -f test/environment.yml
190
+ source activate test
191
+ conda remove ipython-cache
192
+ pip uninstall ipython_cache
193
+ pip install -e .
194
+ ./test/run_example.py
195
+ ```
164
196
165
197
If there is any error, it will be printed to stderr and the script fails.
166
198
0 commit comments