Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asdf.treeutil.walk_and_modify is not depth first #1718

Open
braingram opened this issue Dec 21, 2023 · 0 comments
Open

asdf.treeutil.walk_and_modify is not depth first #1718

braingram opened this issue Dec 21, 2023 · 0 comments

Comments

@braingram
Copy link
Contributor

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

@braingram braingram mentioned this issue Jan 3, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant