-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08f6385
commit 145e498
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from dataclasses import fields | ||
from typing import Any, List, Set | ||
|
||
|
||
class NodeIterator: | ||
def __init__(self, root): | ||
self._iter_position: int = 0 | ||
self._uuid_visited: Set[str] = set(str(root.uuid)) | ||
self._stack: List[Any] = [root] | ||
self._depth_first(root) | ||
|
||
def _depth_first(self, node) -> None: | ||
"""Helper function that does the traversal in depth first order and stores result in stack""" | ||
|
||
for attr_name in fields(node._json_attrs): | ||
attr = getattr(node._json_attrs.attr_name) | ||
try: # Only attribute that have uuid, can be UUID children | ||
uuid = str(attr.uuid) | ||
except AttributeError: | ||
pass # Ignore non-uuid nodes | ||
else: | ||
if uuid not in self._uuid_visited: # break cycles | ||
self._stack.append(attr) | ||
self._uuid_visited.add(uuid) | ||
# Recursive call to add all children | ||
self._add_children(attr) | ||
|
||
def __next__(self): | ||
if self._iter_position >= len(self._stack): | ||
raise StopIteration | ||
self._iter_position += 1 | ||
return self._stack[self._iter_position - 1] | ||
|
||
def __iter__(self): | ||
self._iter_position = 0 | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters