forked from yahoo/graphkit
/
base.py
353 lines (280 loc) · 11.8 KB
/
base.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Copyright 2016, Yahoo Inc.
# Licensed under the terms of the Apache License, Version 2.0. See the LICENSE file associated with the project for terms.
"""Generic or specific utilities"""
import abc
import logging
import random
from collections import defaultdict
from typing import Any, Collection, List, Mapping, Union
Items = Union[Collection, str, None]
log = logging.getLogger(__name__)
class MultiValueError(ValueError):
def __str__(self):
"""Assuming it has been called with ``MultiValueError(msg, ex1, ...) #"""
return str(self.args[0]) # pylint: disable=unsubscriptable-object
class Token(str):
"""Guarantee equality, not(!) identity, across processes."""
__slots__ = ("hashid",)
def __new__(cls, s):
return super().__new__(cls, f"<{s}>")
def __init__(self, *args):
self.hashid = random.randint(-(2 ** 32), 2 ** 32 - 1)
def __eq__(self, other):
return self.hashid == getattr(other, "hashid", None)
def __hash__(self):
return self.hashid
def __getstate__(self):
return self.hashid
def __setstate__(self, state):
self.hashid = state
def __copy__(self):
return self
def __deepcopy__(self, memo):
return self
def __bool__(self):
"""Always `True`, even if empty string."""
return True
def __repr__(self):
"""Avoid 'ticks' around repr."""
return self.__str__()
#: When an operation function returns this special value,
#: it implies operation has no result at all,
#: (otherwise, it would have been a single result, ``None``).`
NO_RESULT = Token("NO_RESULT")
UNSET = Token("UNSET")
def aslist(i, argname, allowed_types=list):
"""Utility to accept singular strings as lists, and None --> []."""
if not i:
return i if isinstance(i, allowed_types) else []
if isinstance(i, str):
i = [i]
elif not isinstance(i, allowed_types):
try:
i = list(i)
except Exception as ex:
raise ValueError(f"Cannot list-ize {argname}({i!r}) due to: {ex}") from None
return i
def astuple(i, argname, allowed_types=tuple):
if not i:
return i if isinstance(i, allowed_types) else ()
if isinstance(i, str):
i = (i,)
elif not isinstance(i, allowed_types):
try:
i = tuple(i)
except Exception as ex:
raise ValueError(
f"Cannot tuple-ize {argname}({i!r}) due to: {ex}"
) from None
return i
def jetsam(ex, locs, *salvage_vars: str, annotation="jetsam", **salvage_mappings):
"""
Annotate exception with salvaged values from locals() and raise!
:param ex:
the exception to annotate
:param locs:
``locals()`` from the context-manager's block containing vars
to be salvaged in case of exception
ATTENTION: wrapped function must finally call ``locals()``, because
*locals* dictionary only reflects local-var changes after call.
:param annotation:
the name of the attribute to attach on the exception
:param salvage_vars:
local variable names to save as is in the salvaged annotations dictionary.
:param salvage_mappings:
a mapping of destination-annotation-keys --> source-locals-keys;
if a `source` is callable, the value to salvage is retrieved
by calling ``value(locs)``.
They take precendance over`salvage_vars`.
:raises:
any exception raised by the wrapped function, annotated with values
assigned as attributes on this context-manager
- Any attributes attached on this manager are attached as a new dict on
the raised exception as new ``jetsam`` attribute with a dict as value.
- If the exception is already annotated, any new items are inserted,
but existing ones are preserved.
**Example:**
Call it with managed-block's ``locals()`` and tell which of them to salvage
in case of errors::
try:
a = 1
b = 2
raise Exception()
exception Exception as ex:
jetsam(ex, locals(), "a", b="salvaged_b", c_var="c")
raise
And then from a REPL::
import sys
sys.last_value.jetsam
{'a': 1, 'salvaged_b': 2, "c_var": None}
** Reason:**
Graphs may become arbitrary deep. Debugging such graphs is notoriously hard.
The purpose is not to require a debugger-session to inspect the root-causes
(without precluding one).
Naively salvaging values with a simple try/except block around each function,
blocks the debugger from landing on the real cause of the error - it would
land on that block; and that could be many nested levels above it.
"""
## Fail EARLY before yielding on bad use.
#
assert isinstance(ex, Exception), ("Bad `ex`, not an exception dict:", ex)
assert isinstance(locs, dict), ("Bad `locs`, not a dict:", locs)
assert all(isinstance(i, str) for i in salvage_vars), (
"Bad `salvage_vars`!",
salvage_vars,
)
assert salvage_vars or salvage_mappings, "No `salvage_mappings` given!"
assert all(isinstance(v, str) or callable(v) for v in salvage_mappings.values()), (
"Bad `salvage_mappings`:",
salvage_mappings,
)
## Merge vars-mapping to save.
for var in salvage_vars:
if var not in salvage_mappings:
salvage_mappings[var] = var
try:
annotations = getattr(ex, annotation, None)
if not isinstance(annotations, dict):
annotations = {}
setattr(ex, annotation, annotations)
## Salvage those asked
for dst_key, src in salvage_mappings.items():
try:
salvaged_value = src(locs) if callable(src) else locs.get(src)
annotations.setdefault(dst_key, salvaged_value)
except Exception as ex:
log.warning(
"Suppressed error while salvaging jetsam item (%r, %r): %r"
% (dst_key, src, ex)
)
except Exception as ex2:
log.warning("Suppressed error while annotating exception: %r", ex2, exc_info=1)
raise ex2
## Defined here, to avoid subclasses importing `plot` module.
class Plotter(abc.ABC):
"""
Classes wishing to plot their graphs should inherit this and ...
implement property ``plot`` to return a "partial" callable that somehow
ends up calling :func:`.plot.render_pydot()` with the `graph` or any other
args bound appropriately.
The purpose is to avoid copying this function & documentation here around.
"""
def plot(
self,
filename=None,
show=False,
jupyter_render: Union[None, Mapping, str] = None,
**kws,
):
"""
Entry-point for plotting ready made operation graphs.
:param str filename:
Write diagram into a file.
Common extensions are ``.png .dot .jpg .jpeg .pdf .svg``
call :func:`plot.supported_plot_formats()` for more.
:param show:
If it evaluates to true, opens the diagram in a matplotlib window.
If it equals `-1`, it plots but does not open the Window.
:param inputs:
an optional name list, any nodes in there are plotted
as a "house"
:param outputs:
an optional name list, any nodes in there are plotted
as an "inverted-house"
:param solution:
an optional dict with values to annotate nodes, drawn "filled"
(currently content not shown, but node drawn as "filled").
It extracts more infos from a :class:`.Solution` instance, such as,
if `solution` has an ``executed`` attribute, operations contained in it
are drawn as "filled".
:param title:
an optional string to display at the bottom of the graph
:param node_props:
an optional nested dict of Graphviz attributes for certain nodes
:param edge_props:
an optional nested dict of Graphviz attributes for certain edges
:param clusters:
an optional mapping of nodes --> cluster-names, to group them
:param jupyter_render:
a nested dictionary controlling the rendering of graph-plots in Jupyter cells,
if `None`, defaults to :data:`jupyter_render` (you may modify it in place
and apply for all future calls).
:param legend_url:
a URL to the *graphtik* legend; if it evaluates to false, none is added.
:return:
a `pydot.Dot <https://pypi.org/project/pydot/>`_ instance
(for for API reference visit:
https://pydotplus.readthedocs.io/reference.html#pydotplus.graphviz.Dot)
.. Tip::
The :class:`pydot.Dot` instance returned is rendered directly
in *Jupyter/IPython* notebooks as SVG images.
You may increase the height of the SVG cell output with
something like this::
netop.plot(jupyter_render={"svg_element_styles": "height: 600px; width: 100%"})
Check :data:`.default_jupyter_render` for defaults.
Note that the `graph` argument is absent - Each Plotter provides
its own graph internally; use directly :func:`.render_pydot()` to provide
a different graph.
.. image:: images/GraphtikLegend.svg
:alt: Graphtik Legend
*NODES:*
oval
function
egg
subgraph operation
house
given input
inversed-house
asked output
polygon
given both as input & asked as output (what?)
square
intermediate data, neither given nor asked.
red frame
evict-instruction, to free up memory.
filled
data node has a value in `solution` OR function has been executed.
thick frame
function/data node in execution `steps`.
*ARROWS*
solid black arrows
dependencies (source-data *need*-ed by target-operations,
sources-operations *provides* target-data)
dashed black arrows
optional needs
blue arrows
sideffect needs/provides
wheat arrows
broken dependency (``provide``) during pruning
green-dotted arrows
execution steps labeled in succession
To generate the **legend**, see :func:`.legend()`.
**Sample code:**
>>> from graphtik import compose, operation
>>> from graphtik.modifiers import optional
>>> from operator import add
>>> netop = compose("netop",
... operation(name="add", needs=["a", "b1"], provides=["ab1"])(add),
... operation(name="sub", needs=["a", optional("b2")], provides=["ab2"])(lambda a, b=1: a-b),
... operation(name="abb", needs=["ab1", "ab2"], provides=["asked"])(add),
... )
>>> netop.plot(show=True); # plot just the graph in a matplotlib window # doctest: +SKIP
>>> inputs = {'a': 1, 'b1': 2}
>>> solution = netop(**inputs) # now plots will include the execution-plan
>>> netop.plot('plot1.svg', inputs=inputs, outputs=['asked', 'b1'], solution=solution); # doctest: +SKIP
>>> dot = netop.plot(solution=solution); # just get the `pydot.Dot` object, renderable in Jupyter
>>> print(dot)
digraph G {
fontname=italic;
label=<netop>;
<a> [fillcolor=wheat, shape=invhouse, style=filled, tooltip=1];
...
"""
from .plot import render_pydot
dot = self._build_pydot(**kws)
return render_pydot(
dot, filename=filename, show=show, jupyter_render=jupyter_render
)
@abc.abstractmethod
def _build_pydot(self, **kws):
pass