Skip to content

Commit 2e91476

Browse files
kszucscpcloud
authored andcommitted
feat(common): expose node.__children__ property to access the flattened list of children of a node
1 parent 15acf7d commit 2e91476

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

ibis/common/graph.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def __args__(self) -> tuple[Any, ...]:
128128
def __argnames__(self) -> tuple[str, ...]:
129129
"""Sequence of argument names."""
130130

131+
@property
132+
def __children__(self) -> tuple[Node, ...]:
133+
"""Sequence of children nodes."""
134+
return tuple(_flatten_collections(self.__args__))
135+
131136
def __rich_repr__(self):
132137
"""Support for rich reprerentation of the node."""
133138
return zip(self.__argnames__, self.__args__)
@@ -228,7 +233,7 @@ def find_topmost(self, pat: type, context: Optional[dict] = None) -> list[Node]:
228233
if pat.match(node, ctx) is not NoMatch:
229234
result.append(node)
230235
else:
231-
queue.extend(_flatten_collections(node.__args__))
236+
queue.extend(node.__children__)
232237
seen.add(node)
233238
else:
234239
# fast path for locating a specific type
@@ -237,7 +242,7 @@ def find_topmost(self, pat: type, context: Optional[dict] = None) -> list[Node]:
237242
if isinstance(node, pat):
238243
result.append(node)
239244
else:
240-
queue.extend(_flatten_collections(node.__args__))
245+
queue.extend(node.__children__)
241246
seen.add(node)
242247

243248
return result
@@ -454,7 +459,7 @@ def traverse(
454459

455460
if control is not halt:
456461
if control is proceed:
457-
children = tuple(_flatten_collections(node.__args__))
462+
children = node.__children__
458463
elif isinstance(control, Iterable):
459464
children = control
460465
else:
@@ -488,7 +493,7 @@ def bfs(root: Node) -> Graph:
488493

489494
while queue:
490495
if (node := queue.popleft()) not in graph:
491-
children = tuple(_flatten_collections(node.__args__))
496+
children = node.__children__
492497
graph[node] = children
493498
queue.extend(children)
494499

@@ -524,7 +529,7 @@ def bfs_while(root: Node, filter: Optional[Any] = None) -> Graph:
524529
if (node := queue.popleft()) not in graph:
525530
children = tuple(
526531
child
527-
for child in _flatten_collections(node.__args__)
532+
for child in node.__children__
528533
if filter.match(child, {}) is not NoMatch
529534
)
530535
graph[node] = children
@@ -555,7 +560,7 @@ def dfs(root: Node) -> Graph:
555560

556561
while stack:
557562
if (node := stack.pop()) not in graph:
558-
children = tuple(_flatten_collections(node.__args__))
563+
children = node.__children__
559564
graph[node] = children
560565
stack.extend(children)
561566

@@ -591,7 +596,7 @@ def dfs_while(root: Node, filter: Optional[Any] = None) -> Graph:
591596
if (node := stack.pop()) not in graph:
592597
children = tuple(
593598
child
594-
for child in _flatten_collections(node.__args__)
599+
for child in node.__children__
595600
if filter.match(child, {}) is not NoMatch
596601
)
597602
graph[node] = children

ibis/common/tests/test_graph.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_nested_children():
117117
b = MyNode(name="b", children=[a])
118118
c = MyNode(name="c", children=[])
119119
d = MyNode(name="d", children=[])
120-
e = MyNode(name="e", children=[[b, c], d])
120+
e = MyNode(name="e", children=[[b, c], {"d": d}])
121121
assert bfs(e) == {
122122
e: (b, c, d),
123123
b: (a,),
@@ -126,6 +126,12 @@ def test_nested_children():
126126
a: (),
127127
}
128128

129+
assert a.__children__ == ()
130+
assert b.__children__ == (a,)
131+
assert c.__children__ == ()
132+
assert d.__children__ == ()
133+
assert e.__children__ == (b, c, d)
134+
129135

130136
@pytest.mark.parametrize("func", [bfs_while, dfs_while, Graph.from_bfs, Graph.from_dfs])
131137
def test_traversals_with_filter(func):

0 commit comments

Comments
 (0)