|
14 | 14 | from collections import abc as cabc |
15 | 15 | from typing import Callable, List, Mapping, Union |
16 | 16 |
|
| 17 | +from boltons.setutils import IndexedSet as iset |
| 18 | + |
17 | 19 | from .base import UNSET, Items, Operation, PlotArgs, Plottable, RenArgs, aslist, jetsam |
18 | 20 | from .modifiers import dep_renamed |
19 | 21 |
|
@@ -54,6 +56,64 @@ def _id_tristate_bool(b): |
54 | 56 | return 3 if b is None else (hash(bool(b)) + 1) |
55 | 57 |
|
56 | 58 |
|
| 59 | +def build_network( |
| 60 | + operations, |
| 61 | + rescheduled=None, |
| 62 | + endured=None, |
| 63 | + parallel=None, |
| 64 | + marshalled=None, |
| 65 | + node_props=None, |
| 66 | + renamer=None, |
| 67 | +): |
| 68 | + """ |
| 69 | + The :term:`network` factory that does :term:`operation merging` before constructing it. |
| 70 | +
|
| 71 | + :param nest: |
| 72 | + see same-named param in :func:`.compose` |
| 73 | + """ |
| 74 | + kw = { |
| 75 | + k: v for k, v in locals().items() if v is not None and k not in ("operations") |
| 76 | + } |
| 77 | + |
| 78 | + def proc_op(op, parent=None): |
| 79 | + """clone FuncOperation with certain props changed""" |
| 80 | + ## Convey any node-props specified in the pipeline here |
| 81 | + # to all sub-operations. |
| 82 | + # |
| 83 | + from .op import FunctionalOperation |
| 84 | + |
| 85 | + if kw: |
| 86 | + op_kw = kw.copy() |
| 87 | + |
| 88 | + if node_props: |
| 89 | + op_kw["node_props"] = {**op.node_props, **node_props} |
| 90 | + |
| 91 | + if callable(renamer): |
| 92 | + |
| 93 | + def parent_wrapper(ren_args: RenArgs) -> str: |
| 94 | + # Provide RenArgs.parent. |
| 95 | + return renamer(ren_args._replace(parent=parent)) |
| 96 | + |
| 97 | + op_kw["renamer"] = parent_wrapper |
| 98 | + op = op.withset(**op_kw) |
| 99 | + |
| 100 | + return op |
| 101 | + |
| 102 | + merge_set = iset() # Preseve given node order. |
| 103 | + for op in operations: |
| 104 | + if isinstance(op, Pipeline): |
| 105 | + merge_set.update(proc_op(s, op) for s in op.ops) |
| 106 | + else: |
| 107 | + merge_set.add(proc_op(op)) |
| 108 | + merge_set = iset(i for i in merge_set if not isinstance(i, NULL_OP)) |
| 109 | + |
| 110 | + assert all(bool(n) for n in merge_set) |
| 111 | + |
| 112 | + from .network import Network # Imported here not to affect locals() at the top. |
| 113 | + |
| 114 | + return Network(*merge_set) |
| 115 | + |
| 116 | + |
57 | 117 | class Pipeline(Operation, Plottable): |
58 | 118 | """ |
59 | 119 | An operation that can :term:`compute` a network-graph of operations. |
@@ -96,7 +156,6 @@ def __init__( |
96 | 156 |
|
97 | 157 | *Operations may only be added once, ...* |
98 | 158 | """ |
99 | | - from .network import build_network |
100 | 159 | from .op import reparse_operation_data |
101 | 160 |
|
102 | 161 | ## Set data asap, for debugging, although `net.withset()` will reset them. |
|
0 commit comments