forked from spotify/pythonflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.py
More file actions
292 lines (237 loc) · 9.73 KB
/
operations.py
File metadata and controls
292 lines (237 loc) · 9.73 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
282
283
284
285
286
287
288
289
290
291
292
# pylint: disable=missing-docstring
# pylint: enable=missing-docstring
# Copyright 2017 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import logging
import pickle
import sys
from .core import opmethod, Operation, func_op, hash_
from .util import _noop_callback, deprecated
class placeholder(Operation): # pylint: disable=C0103,R0903
"""
Placeholder that needs to be given in the context to be evaluated.
"""
def __init__(self, name=None, **kwargs):
super(placeholder, self).__init__(name=name, **kwargs)
def _evaluate(self): # pylint: disable=W0221
raise ValueError("missing value for placeholder '%s'" % self.name)
def __repr__(self):
return "<pf.placeholder '%s'>" % self.name
class conditional(Operation): # pylint: disable=C0103,W0223
"""
Return `x` if `predicate` is `True` and `y` otherwise.
Note that the conditional operation will only execute one branch of the computation graph
depending on `predicate`.
"""
def __init__(self, predicate, x, y=None, *, length=None, name=None, dependencies=None): # pylint: disable=W0235
super(conditional, self).__init__(predicate, x, y,
length=length, name=name, dependencies=dependencies)
def evaluate(self, context, callback=None):
# Evaluate all dependencies first
callback = callback or _noop_callback
self.evaluate_dependencies(context, callback)
predicate, x, y = self.args # pylint: disable=E0632,C0103
# Evaluate the predicate and pick the right operation
predicate = self.evaluate_operation(predicate, context, callback=callback)
with callback(self, context):
value = self.evaluate_operation(x if predicate else y, context, callback=callback)
context[self] = value
return value
class try_(Operation): # pylint: disable=C0103,W0223
"""
Try to evaluate `operation`, fall back to alternative operations in `except_`, and ensure that
`finally_` is evaluated.
Note that the alternative operations will only be executed if the target operation fails.
Parameters
----------
operation : Operation
Operation to evaluate.
except_ : list[(type, Operation)]
List of exception types and corresponding operation to evaluate if it occurs.
finally_ : Operation
Operation to evaluate irrespective of whether `operation` fails.
"""
def __init__(self, operation, except_=None, finally_=None, **kwargs):
except_ = except_ or []
super(try_, self).__init__(operation, except_, finally_, **kwargs)
def evaluate(self, context, callback=None):
# Evaluate all dependencies first
callback = callback or _noop_callback
self.evaluate_dependencies(context, callback=callback)
operation, except_, finally_ = self.args # pylint: disable=E0632,C0103
with callback(self, context):
try:
value = self.evaluate_operation(operation, context, callback=callback)
context[self] = value
return value
except:
# Check the exceptions
_, ex, _ = sys.exc_info()
for type_, alternative in except_:
if isinstance(ex, type_):
value = self.evaluate_operation(alternative, context, callback=callback)
context[self] = value
return value
raise
finally:
if finally_:
self.evaluate_operation(finally_, context)
def cache(operation, get, put, key=None):
"""
Cache the values of `operation`.
Parameters
----------
operation : Operation
Operation to cache.
get : callable(object)
Callable to retrieve an item from the cache. Should throw `KeyError` or `FileNotFoundError`
if the item is not in the cache.
put : callable(object, object)
Callable that adds an item to the cache. The first argument is the key, the seconde the
value.
key : Operation
Key for looking up an item in the cache. Defaults to a simple `hash` of the arguments of
`operation`.
Returns
-------
cached_operation : Operation
Cached operation.
"""
if not key:
dependencies = operation.args + tuple(operation.kwargs.values())
key = hash_(dependencies)
return try_(
func_op(get, key), [
((KeyError, FileNotFoundError),
identity(operation, dependencies=[func_op(put, key, operation)])) # pylint: disable=unexpected-keyword-arg
]
)
def _pickle_load(filename):
with open(filename, 'rb') as fp: # pylint: disable=invalid-name
return pickle.load(fp)
def _pickle_dump(value, filename):
with open(filename, 'wb') as fp: # pylint: disable=invalid-name
pickle.dump(value, fp)
def cache_file(operation, filename_template, load=None, dump=None, key=None):
"""
Cache the values of `operation` in a file.
Parameters
----------
operation : Operation
Operation to cache.
filename_template : str
Template for the filename taking a single `key` parameter.
load : callable(str)
Callable to retrieve an item from a given file. Should throw `FileNotFoundError` if the file
does not exist.
dump : callable(object, str)
Callable to save the item to a file. The order of arguments differs from the `put` argument
of `cache` to be compatible with `pickle.dump`, `numpy.save`, etc.
key : Operation
Key for looking up an item in the cache. Defaults to a simple `hash` of the arguments of
`operation`.
Returns
-------
cached_operation : Operation
Cached operation.
"""
load = load or _pickle_load
dump = dump or _pickle_dump
return cache(
operation, lambda key_: load(filename_template % key_),
lambda key_, value: dump(value, filename_template % key_), key)
@opmethod
def identity(value):
"""
Operation returning the input value.
"""
return value
# Short hand for the identity
constant = identity # pylint: disable=invalid-name
@opmethod
def assert_(condition, message=None, *args, value=None): # pylint: disable=keyword-arg-before-vararg
"""
Return `value` if the `condition` is satisfied and raise an `AssertionError` with the specified
`message` and `args` if not.
"""
if message:
assert condition, message % args
else:
assert condition
return value
@opmethod
def str_format(format_string, *args, **kwargs):
"""
Use python's advanced string formatting to convert the format string and arguments.
References
----------
https://www.python.org/dev/peps/pep-3101/
"""
return format_string.format(*args, **kwargs)
@deprecated
class Logger: # pragma: no cover
"""
Wrapper for a standard python logging channel with the specified `logger_name`.
Parameters
----------
logger_name : str
Name of the underlying standard python logger.
Attributes
----------
logger : logging.Logger
Underlying standard python logger.
"""
def __init__(self, logger_name=None):
self.logger = logging.getLogger(logger_name)
@functools.wraps(logging.Logger.log)
def log(self, level, message, *args, **kwargs): # pylint: disable=missing-docstring
if isinstance(level, str):
level = getattr(logging, level.upper())
return func_op(self.logger.log, level, message, *args, **kwargs)
@functools.wraps(logging.Logger.debug)
def debug(self, message, *args, **kwargs): # pylint: disable=missing-docstring
return func_op(self.logger.debug, message, *args, **kwargs)
@functools.wraps(logging.Logger.info)
def info(self, message, *args, **kwargs): # pylint: disable=missing-docstring
return func_op(self.logger.info, message, *args, **kwargs)
@functools.wraps(logging.Logger.warning)
def warning(self, message, *args, **kwargs): # pylint: disable=missing-docstring
return func_op(self.logger.warning, message, *args, **kwargs)
@functools.wraps(logging.Logger.error)
def error(self, message, *args, **kwargs): # pylint: disable=missing-docstring
return func_op(self.logger.error, message, *args, **kwargs)
@functools.wraps(logging.Logger.critical)
def critical(self, message, *args, **kwargs): # pylint: disable=missing-docstring
return func_op(self.logger.critical, message, *args, **kwargs)
class lazy_constant(Operation): # pylint: disable=invalid-name
"""
Operation that returns the output of `target` lazily.
Parameters
----------
target : callable
Function to evaluate when the operation is evaluated.
kwargs : dict
Keyword arguments passed to the constructor of `Operation`.
"""
def __init__(self, target, **kwargs):
super(lazy_constant, self).__init__(**kwargs)
self.target = target
if not callable(self.target):
raise ValueError("`target` must be callable")
self.value = None
def _evaluate(self): # pylint: disable=W0221
if self.value is None:
self.value = self.target()
return self.value