Skip to content

Commit 3419492

Browse files
committed
New syntax: Forks & merge polishing
1 parent caef022 commit 3419492

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

bin/update_apidoc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def get_path(self):
3131
return os.path.join(__path__, apidoc_root, *self.name.split(".")) + ".rst"
3232

3333

34-
3534
bonobo = __import__("bonobo")
3635
assert bonobo.__version__
3736

bonobo/structs/graphs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def get_cursor(self, ref=BEGIN):
104104
"""
105105
return GraphCursor(self, last=self.index_of(ref))
106106

107+
def orphan(self):
108+
"""
109+
Create a `GraphCursor` attached to nothing.
110+
111+
"""
112+
return self.get_cursor(None)
113+
107114
def index_of(self, mixed):
108115
"""
109116
Find the index based on various strategies for a node, probably an input or output of chain. Supported

docs/guide/graphs.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,33 @@ You can also create single nodes, and the api provide the same capability on sin
396396
graph.add_chain(..., _output="foo")
397397
398398
399+
Orphan nodes / chains
400+
:::::::::::::::::::::
401+
402+
The default behaviour of `add_chain` (or `get_cursor`) is to connect the first node to the special `BEGIN` token, which
403+
instruct |bonobo| to call the connected node once without parameter to kickstart the data stream.
404+
405+
This is normally what you want, but there are ways to override it, as you may want to add "orphan" nodes or chains to your graph.
406+
407+
.. code-block:: python
408+
409+
import bonobo
410+
411+
graph = bonobo.Graph()
412+
413+
# using add_node will naturally add a node as "orphan"
414+
graph.add_node(a)
415+
416+
# using add_chain with "None" as the input will create an orphan chain
417+
graph.add_chain(a, b, c, _input=None)
418+
419+
# using the new syntax, you can use either get_cursor(None) or the orphan() shortcut
420+
graph.get_cursor(None) >> a >> b >> c
421+
422+
# ... using the shortcut ...
423+
graph.orphan() >> a >> b >> c
424+
425+
399426
Connecting two nodes
400427
::::::::::::::::::::
401428

tests/structs/test_graphs_new_syntax.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,23 @@ def test_cursor_merge():
120120
assert g.outputs_of(c) == set()
121121

122122
assert c1 == c2
123+
124+
125+
def test_cursor_merge_orphan_in_between():
126+
a, b, c, v, w, x, y = get_pseudo_nodes(*"abcdefg")
127+
g = Graph()
128+
g >> a >> b >> c
129+
assert len(g) == 3
130+
g.orphan() >> v >> w >> b
131+
assert len(g) == 5
132+
g.orphan() >> x >> y >> b
133+
assert len(g) == 7
134+
135+
assert g.outputs_of(BEGIN) == g.indexes_of(a)
136+
assert g.outputs_of(a) == g.indexes_of(b)
137+
assert g.outputs_of(b) == g.indexes_of(c)
138+
assert g.outputs_of(c) == set()
139+
assert g.outputs_of(v) == g.indexes_of(w)
140+
assert g.outputs_of(w) == g.indexes_of(b)
141+
assert g.outputs_of(x) == g.indexes_of(y)
142+
assert g.outputs_of(y) == g.indexes_of(b)

0 commit comments

Comments
 (0)