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

[model] add recursive traversal of model nodes into objectmodel.Node #241

Open
apalala opened this issue Dec 30, 2021 · 1 comment
Open

Comments

@apalala
Copy link
Collaborator

apalala commented Dec 30, 2021

     def _children_of_type(self, atype: Type = Node, recurse: bool = False, seen: set|None = None) -> Iterable[Node]:
        if recurse and seen is None:
            seen = set()

        for child in self._children():
            if isinstance(child, atype):
                yield child
            if recurse and isinstance(child, Node) and id(child) not in seen:
                seen.add(id(child))
                yield from child._children_of_type(atype=atype, recurse=True, seen=seen)
@apalala
Copy link
Collaborator Author

apalala commented Oct 15, 2023

def _children_of_type(self, atype: Type = Node, recurse: bool = False, seen: set|None = None) -> Iterable[Node]:
    if seen is None:
        seen = {id(self)}

    for child in self._children():
        if id(child) in seen:
            continue
        else:
            seen.add(id(child))

        if isinstance(child, atype):
            yield child

        if recurse and isinstance(child, Node):
            yield from child._children_of_type(atype=atype, recurse=True, seen=seen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant