Skip to content

Commit

Permalink
Merge pull request #1225 from jmishra01/memory-efficient
Browse files Browse the repository at this point in the history
Use generator instead of list expand or add method
  • Loading branch information
erezsh authored Dec 5, 2022
2 parents b962bb6 + 652b92c commit c3b2996
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lark/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,16 @@ def _pretty_label(self):
return self.data

def _pretty(self, level, indent_str):
yield f'{indent_str*level}{self._pretty_label()}'
if len(self.children) == 1 and not isinstance(self.children[0], Tree):
return [indent_str*level, self._pretty_label(), '\t', '%s' % (self.children[0],), '\n']

l = [indent_str*level, self._pretty_label(), '\n']
for n in self.children:
if isinstance(n, Tree):
l += n._pretty(level+1, indent_str)
else:
l += [indent_str*(level+1), '%s' % (n,), '\n']

return l
yield f'\t{self.children[0]}\n'
else:
yield '\n'
for n in self.children:
if isinstance(n, Tree):
yield from n._pretty(level+1, indent_str)
else:
yield f'{indent_str*(level+1)}{n}\n'

def pretty(self, indent_str: str=' ') -> str:
"""Returns an indented string representation of the tree.
Expand Down Expand Up @@ -149,13 +148,15 @@ def iter_subtrees_topdown(self):
Iterates over all the subtrees, return nodes in order like pretty() does.
"""
stack = [self]
stack_append = stack.append
stack_pop = stack.pop
while stack:
node = stack.pop()
node = stack_pop()
if not isinstance(node, Tree):
continue
yield node
for child in reversed(node.children):
stack.append(child)
stack_append(child)

def find_pred(self, pred: 'Callable[[Tree[_Leaf_T]], bool]') -> 'Iterator[Tree[_Leaf_T]]':
"""Returns all nodes of the tree that evaluate pred(node) as true."""
Expand Down

0 comments on commit c3b2996

Please sign in to comment.