A small Python library which enables key lookups on deeply nested documents.
Documents may be built out of dictionaries (dicts) and/or lists.
Make working with JSON, YAML, and XML document responses fun again!
install from pypi using pip:
pip install nested-lookup
or easy_install:
easy_install nested-lookup
or install from source using:
git clone https://github.com/russellballestrini/nested-lookup.git cd nested-lookup pip install .
>>> from nested_lookup import nested_lookup
>>> document = [ { 'taco' : 42 } , { 'salsa' : [ { 'burrito' : { 'taco' : 69 } } ] } ]
>>> print(nested_lookup('taco', document))
[42, 69]
We also have a wild mode that treats the given key as a case insensitive substring of all the keys in the document and returns any values which match.
For example:
from nested_lookup import nested_lookup
my_document = {
'name' : 'Russell Ballestrini',
'email_address' : '[email protected]',
'other' : {
'secondary_email' : '[email protected]',
'EMAIL_RECOVERY' : '[email protected]',
},
},
results = nested_lookup(
key = 'mail',
document = my_document,
wild = True
)
print(results)
['[email protected]', '[email protected]', '[email protected]']
There are two output modes:
- list: the function returns a list of values corresponding to the matched keys.
- dict: the function returns a dict with the matched keys as keys and their corresponding values as values.
For example:
from nested_lookup import nested_lookup
my_document = {
'name' : 'Russell Ballestrini',
'email_address' : '[email protected]',
'other' : {
'secondary_email' : '[email protected]',
'EMAIL_RECOVERY' : '[email protected]',
},
},
results = nested_lookup(
key = 'mail',
document = my_document,
wild = True,
output = 'dict'
)
print(results)
{'email_address': '[email protected]',
'secondary_email': '[email protected]',
'EMAIL_RECOVERY': '[email protected]'}
license: |
|
---|---|
authors: |
|
web: |