walk_and_modify

asdf.treeutil.walk_and_modify(top, callback, ignore_implicit_conversion=False, postorder=True, _context=None)[source]

Modify a tree by walking it with a callback function. It also has the effect of doing a deep copy.

Parameters:
topobject

The root of the tree. May be a dict, list or other Python object.

callbackcallable

A function to call at each node in the tree. It takes either one or two arguments:

  • an instance from the tree

  • a json id (optional)

It may return a different instance in order to modify the tree. If the singleton instance _RemoveNode is returned, the node will be removed from the tree.

The json id is the context under which any relative URLs should be resolved. It may be None if no ids are in the file

The tree is traversed depth-first, with order specified by the postorder argument.

postorderbool

Determines the order in which the callable is invoked on nodes of the tree. If True, the callable will be invoked on children before their parents. If False, the callable is invoked on the parents first. Defaults to True.

ignore_implicit_conversionbool

Controls whether warnings should be issued when implicitly converting a given type instance in the tree into a serializable object. The primary case for this is currently namedtuple.

Defaults to False.

Returns:
treeobject

The modified tree.