forked from rossant/ipycache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipycache.py
More file actions
281 lines (233 loc) · 9.87 KB
/
ipycache.py
File metadata and controls
281 lines (233 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8 -*-
"""Defines a %%cache cell magic in the notebook to persistent-cache results of
long-lasting computations.
"""
#------------------------------------------------------------------------------
# Imports
#------------------------------------------------------------------------------
# Stdlib
import inspect, os, sys, textwrap, cPickle
# Our own
from IPython.config.configurable import Configurable
from IPython.core import magic_arguments
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
from IPython.utils.traitlets import Unicode
from IPython.utils.io import capture_output, CapturedIO
#------------------------------------------------------------------------------
# Six utility functions for Python 2/3 compatibility
#------------------------------------------------------------------------------
# Author: "Benjamin Peterson <benjamin@python.org>"
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
_iteritems = "items"
exec_ = getattr(moves.builtins, "exec")
else:
_iteritems = "iteritems"
def exec_(_code_, _globs_=None, _locs_=None):
"""Execute code in a namespace."""
if _globs_ is None:
frame = sys._getframe(1)
_globs_ = frame.f_globals
if _locs_ is None:
_locs_ = frame.f_locals
del frame
elif _locs_ is None:
_locs_ = _globs_
exec("""exec _code_ in _globs_, _locs_""")
def iteritems(d, **kw):
"""Return an iterator over the (key, value) pairs of a dictionary."""
return iter(getattr(d, _iteritems)(**kw))
#------------------------------------------------------------------------------
# Functions
#------------------------------------------------------------------------------
def conditional_eval(var, variables):
""" Evaluates the variable string if it starts with $. """
if var[0] == '$':
return variables.get(var[1:], var)
return var
def clean_var(var):
"""Clean variable name, removing accidental commas, etc."""
return var.strip().replace(',', '')
def clean_vars(vars):
"""Clean variable names, removing accidental commas, etc."""
return sorted(map(clean_var, vars))
def do_save(path, force=False, read=False):
"""Return True or False whether the variables need to be saved or not."""
if force and read:
raise ValueError(("The 'force' and 'read' options are "
"mutually exclusive."))
# Execute the cell and save the variables.
return force or (not read and not os.path.exists(path))
def load_vars(path, vars):
"""Load variables from a cPickle file.
Arguments:
* path: the path to the pickle file.
* vars: a list of variable names.
Returns:
* cache: a dictionary {var_name: var_value}.
"""
with open(path, 'rb') as f:
# Load the variables from the cache.
try:
cache = cPickle.load(f)
except EOFError as e:
raise IOError(e.message)
# Check that all requested variables could be loaded successfully
# from the cache.
missing_vars = sorted(set(vars) - set(cache.keys()))
if missing_vars:
raise ValueError(("The following variables could not be loaded "
"from the cache: {0:s}").format(
', '.join(["'{0:s}'".format(var) for var in missing_vars])))
return cache
def save_vars(path, vars_d):
"""Save variables into a cPickle file.
Arguments:
* path: the path to the pickle file.
* vars_d: a dictionary {var_name: var_value}.
"""
with open(path, 'wb') as f:
cPickle.dump(vars_d, f)
#------------------------------------------------------------------------------
# CapturedIO
#------------------------------------------------------------------------------
def save_captured_io(io):
return dict(
stdout=io._stdout,
stderr=io._stderr,
outputs=getattr(io, '_outputs', []), # Only IPython master has this
)
def load_captured_io(captured_io):
try:
return CapturedIO(captured_io.get('stdout', None),
captured_io.get('stderr', None),
outputs=captured_io.get('outputs', []),
)
except TypeError:
return CapturedIO(captured_io.get('stdout', None),
captured_io.get('stderr', None),
)
#------------------------------------------------------------------------------
# %%cache Magics
#------------------------------------------------------------------------------
def cache(cell, path, vars=[],
# HACK: this function implementing the magic's logic is testable
# without IPython, by giving mock functions here instead of IPython
# methods.
ip_user_ns={}, ip_run_cell=None, ip_push=None,
force=False, read=False, verbose=True):
if not path:
raise ValueError("The path needs to be specified as a first argument.")
path = os.path.abspath(path)
if do_save(path, force=force, read=read):
# Capture the outputs of the cell.
with capture_output() as io:
ip_run_cell(cell)
# Create the cache from the namespace.
try:
cache = {var: ip_user_ns[var] for var in vars}
except KeyError:
vars_missing = set(vars) - set(ip_user_ns.keys())
vars_missing_str = ', '.join(["'{0:s}'".format(_)
for _ in vars_missing])
raise ValueError(("Variable(s) {0:s} could not be found in the "
"interactive namespace").format(vars_missing_str))
# Save the outputs in the cache.
cache['_captured_io'] = save_captured_io(io)
# Save the cache in the pickle file.
save_vars(path, cache)
if verbose:
print("[Saved variables {0:s} to file '{1:s}'.]".format(
', '.join(vars), path))
# If the cache file exists, and no --force mode, load the requested
# variables from the specified file into the interactive namespace.
else:
# Load the variables from cache in inject them in the namespace.
cache = load_vars(path, vars)
# Handle the outputs separately.
io = load_captured_io(cache.get('_captured_io', {}))
# Push the remaining variables in the namespace.
ip_push(cache)
if verbose:
print(("[Skipped the cell's code and loaded variables {0:s} "
"from file '{1:s}'.]").format(', '.join(vars), path))
# Display the outputs, whether they come from the cell's execution
# or the pickle file.
io()
@magics_class
class CacheMagics(Magics, Configurable):
"""Variable caching.
Provides the %cache magic."""
cachedir = Unicode('', config=True)
def __init__(self, shell=None):
Magics.__init__(self, shell)
Configurable.__init__(self, config=shell.config)
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'to', nargs=1, type=str,
help="Path to the file containing the cached variables."
)
@magic_arguments.argument(
'vars', nargs='*', type=str,
help="Variables to save."
)
@magic_arguments.argument(
'-s', '--silent', action='store_true', default=False,
help="Do not display information when loading/saving variables."
)
@magic_arguments.argument(
'-d', '--cachedir',
help="Cache directory as an absolute or relative path."
)
@magic_arguments.argument(
'-f', '--force', action='store_true', default=False,
help="Force the cell's execution and save the variables."
)
@magic_arguments.argument(
'-r', '--read', action='store_true', default=False,
help=("Always read from the file and prevent the cell's execution, "
"raising an error if the file does not exist.")
)
@cell_magic
def cache(self, line, cell):
"""Cache user variables in a file, and skip the cell if the cached
variables exist.
Usage:
%%cache myfile.pkl var1 var2
# If myfile.pkl doesn't exist, this cell is executed and
# var1 and var2 are saved in this file.
# Otherwise, the cell is skipped and these variables are
# injected from the file to the interactive namespace.
var1 = ...
var2 = ...
"""
ip = self.shell
args = magic_arguments.parse_argstring(self.cache, line)
code = cell if cell.endswith('\n') else cell+'\n'
vars = clean_vars(args.vars)
path = conditional_eval(args.to[0], ip.user_ns)
cachedir_from_path = os.path.split(path)[0]
# The cachedir can be specified with --cachedir or inferred from the
# path or in ipython_config.py
cachedir = args.cachedir or cachedir_from_path or self.cachedir
# If path is relative, use the user-specified cache cachedir.
if not os.path.isabs(path) and cachedir:
# Try to create the cachedir if it does not already exist.
if not os.path.exists(cachedir):
try:
os.mkdir(cachedir)
print("[Created cachedir '{0:s}'.]".format(cachedir))
except:
pass
path = os.path.join(cachedir, path)
cache(cell, path, vars=vars,
force=args.force, verbose=not args.silent, read=args.read,
# IPython methods
ip_user_ns=ip.user_ns,
ip_run_cell=ip.run_cell,
ip_push=ip.push,
)
def load_ipython_extension(ip):
"""Load the extension in IPython."""
ip.register_magics(CacheMagics)