pg.object_utils.traverse

Accessible via pg.object_utils.traverse.

traverse(value, preorder_visitor_fn=None, postorder_visitor_fn=None, root_path=None)[source]

Traverse a (maybe) hierarchical value.

Example:

def preorder_visit(path, value):
  print(path)

tree = {'a': [{'c': [1, 2]}, {'d': {'g': (3, 4)}}], 'b': 'foo'}
pg.object_utils.traverse(tree, preorder_visit)

# Should print:
# 'a'
# 'a[0]'
# 'a[0].c'
# 'a[0].c[0]'
# 'a[0].c[1]'
# 'a[1]'
# 'a[1].d'
# 'a[1].d.g'
# 'b'
Return type:

bool

Parameters:
  • value – A maybe hierarchical value to traverse.

  • preorder_visitor_fn – Preorder visitor function. Function signature is (path, value) -> should_continue.

  • postorder_visitor_fn – Postorder visitor function. Function signature is (path, value) -> should_continue.

  • root_path – The key path of the root value.

Returns:

Whether visitor function returns True on all nodes.