Skip to content

Commit c39f8ba

Browse files
committed
doc, typing
1 parent 3c5523f commit c39f8ba

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
lines changed

graphtik/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import abc
66
import logging
7-
from typing import Mapping, Union
7+
from typing import Collection, Mapping, Union
8+
9+
Items = Union[Collection, str, None]
810

911
log = logging.getLogger(__name__)
1012

graphtik/netop.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import logging
66
import re
77
from collections import abc
8-
from typing import Any, Callable, Collection, Mapping
8+
from typing import Any, Callable, Mapping
99

1010
import networkx as nx
1111
from boltons.setutils import IndexedSet as iset
1212

13-
from .base import Plotter, aslist, astuple, jetsam
13+
from .base import Items, Plotter, aslist, astuple, jetsam
1414
from .modifiers import optional, sideffect
1515
from .network import Network, yield_operations
1616
from .op import FunctionalOperation, Operation, reparse_operation_data
@@ -98,8 +98,8 @@ def __repr__(self):
9898

9999
def narrow(
100100
self,
101-
inputs: Collection = None,
102-
outputs: Collection = None,
101+
inputs: Items = None,
102+
outputs: Items = None,
103103
name=None,
104104
predicate: Callable[[Any, Mapping], bool] = None,
105105
) -> "NetworkOperation":
@@ -264,8 +264,8 @@ def compose(
264264
name,
265265
op1,
266266
*operations,
267-
needs=None,
268-
provides=None,
267+
needs: Items = None,
268+
provides: Items = None,
269269
merge=False,
270270
node_props=None,
271271
method=None,
@@ -277,13 +277,11 @@ def compose(
277277
278278
:param str name:
279279
A optional name for the graph being composed by this object.
280-
281280
:param op1:
282281
syntactically force at least 1 operation
283282
:param operations:
284283
Each argument should be an operation instance created using
285284
``operation``.
286-
287285
:param bool merge:
288286
If ``True``, this compose object will attempt to merge together
289287
``operation`` instances that represent entire computation graphs.
@@ -303,7 +301,6 @@ def compose(
303301
if ``"parallel"``, launches multi-threading.
304302
Set when invoking a composed graph or by
305303
:meth:`~NetworkOperation.set_execution_method()`.
306-
307304
:param overwrites_collector:
308305
(optional) a mutable dict to be fillwed with named values.
309306
If missing, values are simply discarded.

graphtik/network.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
import networkx as nx
8888
from boltons.setutils import IndexedSet as iset
8989

90-
from .base import Plotter, aslist, astuple, jetsam
90+
from .base import Items, Plotter, aslist, astuple, jetsam
9191
from .modifiers import optional, sideffect
9292
from .op import Operation
9393

@@ -138,7 +138,7 @@ def __repr__(self):
138138

139139
class _EvictInstruction(str):
140140
"""
141-
Execution step to evict a computed value from the `solution`.
141+
A step in the ExecutionPlan to evict a computed value from the `solution`.
142142
143143
It's a step in :attr:`ExecutionPlan.steps` for the data-node `str` that
144144
frees its data-value from `solution` after it is no longer needed,
@@ -153,7 +153,7 @@ def __repr__(self):
153153

154154
class _PinInstruction(str):
155155
"""
156-
Execution step to overwrite a computed value in the `solution` from the inputs,
156+
A step in the ExecutionPlan to overwrite a computed value in the `solution` from the inputs,
157157
158158
and to store the computed one in the ``overwrites`` instead
159159
(both `solution` & ``overwrites`` are local-vars in :meth:`~Network.compute()`).
@@ -266,9 +266,7 @@ def __repr__(self):
266266
"".join(steps),
267267
)
268268

269-
def validate(
270-
self, inputs: Optional[abc.Collection], outputs: Optional[abc.Collection]
271-
):
269+
def validate(self, inputs: Items, outputs: Items):
272270
"""
273271
Scream on invalid inputs, outputs or no operations in graph.
274272
@@ -674,8 +672,8 @@ def _apply_graph_predicate(self, graph, predicate):
674672

675673
def _prune_graph(
676674
self,
677-
inputs: Optional[Collection],
678-
outputs: Optional[Collection],
675+
inputs: Items,
676+
outputs: Items,
679677
predicate: Callable[[Any, Mapping], bool] = None,
680678
) -> Tuple[nx.DiGraph, Collection, Collection, Collection]:
681679
"""
@@ -687,7 +685,6 @@ def _prune_graph(
687685
688686
:param inputs:
689687
The names of all given inputs.
690-
691688
:param outputs:
692689
The desired output names. This can also be ``None``, in which
693690
case the necessary steps are all graph nodes that are reachable
@@ -793,8 +790,8 @@ def _prune_graph(
793790

794791
def pruned(
795792
self,
796-
inputs: Collection = None,
797-
outputs: Collection = None,
793+
inputs: Items = None,
794+
outputs: Items = None,
798795
predicate: Callable[[Any, Mapping], bool] = None,
799796
) -> "Network":
800797
"""
@@ -824,7 +821,7 @@ def pruned(
824821
return Network(graph=pruned_dag)
825822

826823
def _build_execution_steps(
827-
self, pruned_dag, inputs: Optional[Collection], outputs: Optional[Collection]
824+
self, pruned_dag, inputs: Collection, outputs: Optional[Collection]
828825
) -> List:
829826
"""
830827
Create the list of operation-nodes & *instructions* evaluating all
@@ -927,11 +924,7 @@ def add_step_once(step):
927924

928925
return steps
929926

930-
def compile(
931-
self,
932-
inputs: Union[Collection, str] = None,
933-
outputs: Union[Collection, str] = None,
934-
) -> ExecutionPlan:
927+
def compile(self, inputs: Items = None, outputs: Items = None) -> ExecutionPlan:
935928
"""
936929
Create or get from cache an execution-plan for the given inputs/outputs.
937930

graphtik/op.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import abc
66
import logging
77
from collections import abc as cabc
8-
from typing import Callable, Collection, Mapping, Tuple, Union
8+
from typing import Callable, Mapping, Tuple, Union
99

1010
from boltons.setutils import IndexedSet as iset
1111

12-
from .base import Plotter, aslist, astuple, jetsam
12+
from .base import Items, Plotter, aslist, astuple, jetsam
1313
from .modifiers import optional, sideffect, vararg, varargs
1414

1515
log = logging.getLogger(__name__)
@@ -71,8 +71,8 @@ def __init__(
7171
self,
7272
fn: Callable,
7373
name,
74-
needs: Union[Collection, str] = None,
75-
provides: Union[Collection, str] = None,
74+
needs: Items = None,
75+
provides: Items = None,
7676
*,
7777
parents: Tuple = None,
7878
node_props: Mapping = None,
@@ -93,6 +93,10 @@ def __init__(
9393
but also kept for equality/hash check.
9494
:param node_props:
9595
added as-is into NetworkX graph
96+
:param returns_dict:
97+
if true, it means the `fn` returns a dictionary with all `provides`,
98+
and no further processing is done on them
99+
(i.e. the returned output-values are not zipped with `provides`)
96100
"""
97101
## Set op-data early, for repr() to work on errors.
98102
self.fn = fn
@@ -288,6 +292,8 @@ class operation:
288292
elements must be returned
289293
:param bool returns_dict:
290294
if true, it means the `fn` returns a dictionary with all `provides`,
295+
and no further processing is done on them
296+
(i.e. the returned output-values are not zipped with `provides`)
291297
:param node_props:
292298
added as-is into NetworkX graph
293299
@@ -319,8 +325,8 @@ def __init__(
319325
fn: Callable = None,
320326
*,
321327
name=None,
322-
needs=None,
323-
provides=None,
328+
needs: Items = None,
329+
provides: Items = None,
324330
returns_dict=None,
325331
node_props: Mapping = None,
326332
):
@@ -334,10 +340,10 @@ def __init__(
334340
def withset(
335341
self,
336342
*,
337-
fn=None,
343+
fn: Callable = None,
338344
name=None,
339-
needs=None,
340-
provides=None,
345+
needs: Items = None,
346+
provides: Items = None,
341347
returns_dict=None,
342348
node_props: Mapping = None,
343349
) -> "operation":
@@ -358,11 +364,11 @@ def withset(
358364

359365
def __call__(
360366
self,
361-
fn=None,
367+
fn: Callable = None,
362368
*,
363369
name=None,
364-
needs=None,
365-
provides=None,
370+
needs: Items = None,
371+
provides: Items = None,
366372
returns_dict=None,
367373
node_props: Mapping = None,
368374
) -> FunctionalOperation:

graphtik/plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import logging
88
import os
9-
from typing import Any, Callable, List, Mapping, Optional, Tuple, Union
9+
from typing import Any, Callable, List, Mapping, Tuple, Union
1010

1111
import pydot
1212

@@ -79,6 +79,7 @@ def _dot2svg(dot):
7979
.. TODO:
8080
Render in jupyter cells fullly on client-side without SVG, using lib:
8181
https://visjs.github.io/vis-network/docs/network/#importDot
82+
Or with plotly https://plot.ly/~empet/14007.embed
8283
8384
"""
8485
pan_zoom_json, element_styles, container_styles = _parse_jupyter_render(dot)
@@ -324,7 +325,6 @@ def render_pydot(dot: pydot.Dot, filename=None, show=False, jupyter_render: str
324325
325326
See :meth:`.Plotter.plot()` for sample code.
326327
"""
327-
# TODO: research https://plot.ly/~empet/14007.embed
328328
# Save plot
329329
#
330330
if filename:
@@ -359,7 +359,7 @@ def render_pydot(dot: pydot.Dot, filename=None, show=False, jupyter_render: str
359359
return dot
360360

361361

362-
def legend(filename=None, show=None, jupyter_render: Optional[Mapping] = None):
362+
def legend(filename=None, show=None, jupyter_render: Mapping = None):
363363
"""Generate a legend for all plots (see :meth:`.Plotter.plot()` for args)"""
364364

365365
_monkey_patch_for_jupyter(pydot)

0 commit comments

Comments
 (0)