Skip to content

Commit

Permalink
feat(py): implement iter on ToNode (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q authored Aug 8, 2024
1 parent 3609736 commit e88910b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions hugr-py/src/hugr/node_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def __getitem__(
def out_port(self) -> OutPort:
return OutPort(self.to_node(), 0)

def outputs(self) -> Iterator[OutPort]:
"""Returns an iterator over the output ports of this node."""
return self[:]

def __iter__(self) -> Iterator[OutPort]:
return self.outputs()

def inp(self, offset: PortOffset) -> InPort:
"""Generate an input port for this node.
Expand Down
5 changes: 4 additions & 1 deletion hugr-py/tests/test_hugr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
def test_stable_indices():
h = Hugr(ops.DFG([]))

nodes = [h.add_node(Not) for _ in range(3)]
nodes = [h.add_node(Not, num_outs=1) for _ in range(3)]
assert len(h) == 4
assert list(iter(h)) == [Node(i) for i in range(4)]
assert all(data is not None for node, data in h.nodes())

assert len(list(nodes[0].outputs())) == 1
assert list(nodes[0]) == list(nodes[0].outputs())

h.add_link(nodes[0].out(0), nodes[1].inp(0))
assert h.children() == nodes

Expand Down

0 comments on commit e88910b

Please sign in to comment.