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
165 lines (131 loc) · 5.48 KB
/
operations.py
File metadata and controls
165 lines (131 loc) · 5.48 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
# 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
from .core import opmethod, Operation, func_op
class placeholder(Operation): # pylint: disable=C0103,R0903
"""
Placeholder that needs to be given in the context to be evaluated.
"""
def __init__(self, name=None):
super(placeholder, self).__init__(name=name)
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
self.evaluate_dependencies(context)
predicate, x, y = self.args # pylint: disable=E0632,C0103
# Evaluate the predicate and pick the right operation
predicate = self.evaluate_operation(predicate, context)
if callback:
with callback(self, context):
context[self] = value = self.evaluate_operation(x if predicate else y, context)
else:
context[self] = value = self.evaluate_operation(x if predicate else y, context)
return value
@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)
class Logger:
"""
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