Advertisement
Guest User

Untitled

a guest
Jun 8th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. class Visitor:
  2.     @contextlib.contextmanager
  3.     def __call__(self, node):
  4.         yield node
  5.  
  6. def visit(visitor, node):
  7.     with visitor(node=node):
  8.         for field in node.subnodes:
  9.             subnode = getattr(node, field.name)
  10.             with visitor(node=subnode):
  11.                 pass
  12.  
  13. def walk(root):
  14.     nodes = _collections.deque([root])
  15.     while nodes:
  16.         node = nodes.popleft()
  17.         nodes.extend(node.subnodes)
  18.         yield node
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement