asdf-format/asdf

`asdf.treeutil.walk_and_modify` is not depth first

Opened this issue · 0 comments

Description of the problem

The walk_and_modify docs state:

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

However the ordering is not as expected for a depth-first search. See the below example.

For postorder=False the walk appears to be breadth-first.

For postorder=True the leaf nodes are walked breadth-first down the tree (not postorder) then the containers are walked up the tree (postorder).

Example of the problem

import asdf

tree = {
    'l0v0': 0,
    'l0v1': 1,
    'a': {
        'l1v0': 10,
        'l1v1': 11,
        'b': {
            'l1v0': 100,
            'l1v1': 101,
        },
    },
}



def callback(obj):
    print(obj)
    return obj


asdf.treeutil.walk_and_modify(tree, callback, postorder=False)
print("== postorder ==")
asdf.treeutil.walk_and_modify(tree, callback, postorder=True)

outputs

{'l0v0': 0, 'l0v1': 1, 'a': {'l1v0': 10, 'l1v1': 11, 'b': {'l1v0': 100, 'l1v1': 101}}}
0
1
{'l1v0': 10, 'l1v1': 11, 'b': {'l1v0': 100, 'l1v1': 101}}
10
11
{'l1v0': 100, 'l1v1': 101}
100
101
== postorder ==
0
1
10
11
100
101
{'l1v0': 100, 'l1v1': 101}
{'l1v0': 10, 'l1v1': 11, 'b': {'l1v0': 100, 'l1v1': 101}}
{'l0v0': 0, 'l0v1': 1, 'a': {'l1v0': 10, 'l1v1': 11, 'b': {'l1v0': 100, 'l1v1': 101}}}

System information

asdf version: main
python version: 3.10
operating system: mac osx