Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Graph.__getitem__ to shorthand for get_cursor #380

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bonobo/structs/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def __len__(self):
"""
return len(self.nodes)

def __getitem__(self, key):
return self.nodes[key]
def __getitem__(self, ref):
return self.get_cursor(ref)

def __enter__(self):
return self.get_cursor().__enter__()
Expand Down Expand Up @@ -313,7 +313,7 @@ def graphviz(self):
for i in self.outputs_of(BEGIN):
g.edge("BEGIN", str(i))
for ix in self.topologically_sorted_indexes:
g.node(str(ix), label=get_name(self[ix]))
g.node(str(ix), label=get_name(self.nodes[ix]))
for iy in self.outputs_of(ix):
g.edge(str(ix), str(iy))
self._graphviz = g
Expand All @@ -331,5 +331,5 @@ def _repr_html_(self):

def _get_graphviz_node_id(graph, i):
escaped_index = str(i)
escaped_name = json.dumps(get_name(graph[i]))
escaped_name = json.dumps(get_name(graph.nodes[i]))
return "{{{} [label={}]}}".format(escaped_index, escaped_name)
10 changes: 5 additions & 5 deletions tests/structs/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ def test_graph_topological_sort():
g.add_chain(a1, a2, a3, _input=None, _output=None)

assert g.topologically_sorted_indexes == (0, 1, 2)
assert g[0] == a1
assert g[1] == a2
assert g[2] == a3
assert g.nodes[0] == a1
assert g.nodes[1] == a2
assert g.nodes[2] == a3

g.add_chain(b1, b2, _output=a2)

assert g.topologically_sorted_indexes[-2:] == (1, 2)
assert g.topologically_sorted_indexes.index(3) < g.topologically_sorted_indexes.index(4)
assert g[3] == b1
assert g[4] == b2
assert g.nodes[3] == b1
assert g.nodes[4] == b2


def test_connect_two_chains():
Expand Down
4 changes: 2 additions & 2 deletions tests/structs/test_graphs_new_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_get_cursor():

def test_get_cursor_in_a_vacuum():
g = Graph()
cursor = g.get_cursor(None)
cursor = g[None]

assert cursor.graph is g
assert cursor.first is None
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_cursor_to_fork_a_graph():

g = Graph()
g >> a >> b >> c
g.get_cursor(b) >> d >> e
g[b] >> d >> e

assert len(g) == 5
assert g.outputs_of(BEGIN) == {g.index_of(a)}
Expand Down