Updated README.rst and set list as default output
This commit is contained in:
parent
dfc2483e3e
commit
3b1f2ced67
2 changed files with 67 additions and 33 deletions
82
README.rst
82
README.rst
|
|
@ -43,35 +43,72 @@ tutorial
|
||||||
[42, 69]
|
[42, 69]
|
||||||
|
|
||||||
|
|
||||||
wild
|
wild
|
||||||
========
|
========
|
||||||
|
|
||||||
We also have a `wild` mode that treats the given `key` as a case insensitive
|
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.
|
substring of all the keys in the document and returns any values which match.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from nested_lookup import nested_lookup
|
from nested_lookup import nested_lookup
|
||||||
|
|
||||||
my_document = {
|
my_document = {
|
||||||
'name' : 'Russell Ballestrini',
|
'name' : 'Russell Ballestrini',
|
||||||
'email_address' : 'test1@example.com',
|
'email_address' : 'test1@example.com',
|
||||||
'other' : {
|
'other' : {
|
||||||
'secondary_email' : 'test2@example.com',
|
'secondary_email' : 'test2@example.com',
|
||||||
'EMAIL_RECOVERY' : 'test3@example.com',
|
'EMAIL_RECOVERY' : 'test3@example.com',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
results = nested_lookup(
|
results = nested_lookup(
|
||||||
key = 'mail',
|
key = 'mail',
|
||||||
document = my_document
|
document = my_document,
|
||||||
wild = True,
|
wild = True
|
||||||
)
|
)
|
||||||
|
|
||||||
print(results)
|
print(results)
|
||||||
['test1@example.com', 'test2@example.com', 'test3@example.com']
|
['test1@example.com', 'test2@example.com', 'test3@example.com']
|
||||||
|
|
||||||
|
|
||||||
|
output
|
||||||
|
========
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from nested_lookup import nested_lookup
|
||||||
|
|
||||||
|
my_document = {
|
||||||
|
'name' : 'Russell Ballestrini',
|
||||||
|
'email_address' : 'test1@example.com',
|
||||||
|
'other' : {
|
||||||
|
'secondary_email' : 'test2@example.com',
|
||||||
|
'EMAIL_RECOVERY' : 'test3@example.com',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
results = nested_lookup(
|
||||||
|
key = 'mail',
|
||||||
|
document = my_document,
|
||||||
|
wild = True,
|
||||||
|
output = 'dict'
|
||||||
|
)
|
||||||
|
|
||||||
|
print(results)
|
||||||
|
{'email_address': 'test1@example.com',
|
||||||
|
'secondary_email': 'test2@example.com',
|
||||||
|
'EMAIL_RECOVERY': 'test3@example.com'}
|
||||||
|
|
||||||
|
|
||||||
misc
|
misc
|
||||||
|
|
@ -88,4 +125,3 @@ misc
|
||||||
* http://russell.ballestrini.net
|
* http://russell.ballestrini.net
|
||||||
* http://douglasmiranda.com
|
* http://douglasmiranda.com
|
||||||
* https://gist.github.com/douglasmiranda/5127251
|
* https://gist.github.com/douglasmiranda/5127251
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
def nested_lookup(key, document, wild=False, output='dict'):
|
def nested_lookup(key, document, wild=False, output='list'):
|
||||||
"""Lookup a key in a nested document, return a list of values"""
|
"""Lookup a key in a nested document, return a list of values"""
|
||||||
if output == 'dict':
|
if output == 'dict':
|
||||||
return dict(_nested_lookup(key, document, wild=wild, output=output))
|
return dict(_nested_lookup(key, document, wild=wild, output=output))
|
||||||
elif output == 'list':
|
|
||||||
return list(_nested_lookup(key, document, wild=wild, output=output))
|
|
||||||
else:
|
else:
|
||||||
raise ValueError('Output is of unknown type '+output)
|
return list(_nested_lookup(key, document, wild=wild, output=output))
|
||||||
|
|
||||||
def _nested_lookup(key, document, wild=False, output='dict'):
|
def _nested_lookup(key, document, wild=False, output='list'):
|
||||||
"""Lookup a key in a nested document, yield a value"""
|
"""Lookup a key in a nested document, yield a value"""
|
||||||
if isinstance(document, list):
|
if isinstance(document, list):
|
||||||
for d in document:
|
for d in document:
|
||||||
|
|
@ -21,7 +19,7 @@ def _nested_lookup(key, document, wild=False, output='dict'):
|
||||||
if key == k or (wild and key.lower() in k.lower()):
|
if key == k or (wild and key.lower() in k.lower()):
|
||||||
if output == 'dict':
|
if output == 'dict':
|
||||||
yield k, v
|
yield k, v
|
||||||
elif output == 'list':
|
else:
|
||||||
yield v
|
yield v
|
||||||
elif isinstance(v, dict):
|
elif isinstance(v, dict):
|
||||||
for result in _nested_lookup(key, v, wild=wild, output=output):
|
for result in _nested_lookup(key, v, wild=wild, output=output):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue