Skip to content

Commit

Permalink
Merge pull request #233 from c0fec0de/214-rendertree-levelorderiter-e…
Browse files Browse the repository at this point in the history
…tc-access-more-child-attributes-than-necessary-when-restricting-maxlevel

fix #221, avoid children attribute access on maxlevel
  • Loading branch information
c0fec0de committed Oct 11, 2023
2 parents 38a5ef1 + 2e803f6 commit b671357
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
15 changes: 9 additions & 6 deletions anytree/iterators/levelorderiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ def _iter(children, filter_, stop, maxlevel):
level = 1
while children:
next_children = []
for child in children:
if filter_(child):
yield child
next_children += AbstractIter._get_children(child.children, stop)
children = next_children
level += 1
if AbstractIter._abort_at_level(level, maxlevel):
break
for child in children:
if filter_(child):
yield child
else:
for child in children:
if filter_(child):
yield child
next_children += AbstractIter._get_children(child.children, stop)
children = next_children
13 changes: 7 additions & 6 deletions anytree/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ def __iter__(self):

def __next(self, node, continues, level=0):
yield RenderTree.__item(node, continues, self.style)
children = node.children
level += 1
if children and (self.maxlevel is None or level < self.maxlevel):
children = self.childiter(children)
for child, is_last in _is_last(children):
for grandchild in self.__next(child, continues + (not is_last,), level=level):
yield grandchild
if self.maxlevel is None or level < self.maxlevel:
children = node.children
if children:
children = self.childiter(children)
for child, is_last in _is_last(children):
for grandchild in self.__next(child, continues + (not is_last,), level=level):
yield grandchild

@staticmethod
def __item(node, continues, style):
Expand Down

0 comments on commit b671357

Please sign in to comment.