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

Improve traverse, calling the callback with the full path key instead of the last one #49

Open
yurj opened this issue Jan 18, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@yurj
Copy link

yurj commented Jan 18, 2021

Sometimes in traverse api it can be useful to have the full path in the callback, for example to find a particular node in the nested dict.

Something like:

def _traverse_collection(d, path, callback):
    if type_util.is_dict(d):
        _traverse_dict(d, path, callback)
    elif type_util.is_list_or_tuple(d):
        _traverse_list(d, path, callback)

def _traverse_dict(d, path, callback):
    keys = list(d.keys())
    for key in keys:
        value = d.get(key, None)
        callback(d, key, path, value)
        path.append(key)
        _traverse_collection(value, path, callback)
        path.pop()


def _traverse_list(ls, path, callback):
    items = list(enumerate(ls))
    for index, value in items:
        callback(ls, index, path, value)
        path.append(index)
        _traverse_collection(value, path, callback)
        path.pop()

def traverse(d, callback):
    if not callable(callback):
        raise ValueError('callback argument must be a callable.')
    _traverse_collection(d, [], callback)

example code:

>>> d = {'dict1': [{'foo': 1, 'bar': 2}], 'dict2': {'baz': 3, 'quux': 4}}
>>> def traverse_item(dct, key, path, value):
...    print('key: {} - path: {} - value: {}'.format(key, path, value))
... 
>>> d.traverse(traverse_item)
key: dict1 - path: [] - value: [{'foo': 1, 'bar': 2}]
key: 0 - path: ['dict1'] - value: {'foo': 1, 'bar': 2}
key: foo - path: ['dict1', 0] - value: 1
key: bar - path: ['dict1', 0] - value: 2
key: dict2 - path: [] - value: {'baz': 3, 'quux': 4}
key: baz - path: ['dict2'] - value: 3
key: quux - path: ['dict2'] - value: 4
Fund with Polar
@yurj yurj added the enhancement New feature or request label Jan 18, 2021
@yurj
Copy link
Author

yurj commented Jan 18, 2021

I've updated the request, there were a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants