Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

When wild=True, getting Attribute error if keys are not strings #23

Closed
RichPlannuh opened this issue Jun 25, 2019 · 4 comments
Closed

Comments

@RichPlannuh
Copy link

RichPlannuh commented Jun 25, 2019

I have complex dict objects that have dictionaries with keys that are sometimes integers. If I do this following over one of these like so it works fine:

nested_results = nested_lookup.nested_lookup(document=my_map, key='amount')

if I change it to:

nested_results = nested_lookup.nested_lookup(document=my_map, key='amount', wild=True)

I get:
File "/usr/local/lib/python3.7/site-packages/nested_lookup/nested_lookup.py", line 29, in _nested_lookup
if key == k or (wild and key.lower() in k.lower()):
AttributeError: 'int' object has no attribute 'lower'

@RichPlannuh
Copy link
Author

Easily fixed:

def _nested_lookup(key, document, wild=False, with_keys=False):
    """Lookup a key in a nested document, yield a value"""
    if isinstance(document, list):
        for d in document:
            for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
                yield result

    if isinstance(document, dict):
        for k, v in iteritems(document):
            if key == k or (wild and hasattr(k, 'lower') and key.lower() in k.lower()):
                if with_keys:
                    yield k, v
                else:
                    yield v
            if isinstance(v, dict):
                for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
                    yield result
            elif isinstance(v, list):
                for d in v:
                    for result in _nested_lookup(
                        key, d, wild=wild, with_keys=with_keys
                    ):
                        yield result

@russellballestrini
Copy link
Owner

russellballestrini commented Jun 25, 2019

@RichPlannuh thanks! I fixed this upstream and pushed to pypi, thanks for the defect report.

Fixes:

@RichPlannuh
Copy link
Author

RichPlannuh commented Jun 25, 2019 via email

@russellballestrini
Copy link
Owner

@RichPlannuh your welcome, I hope it speeds up your progress!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants