wild mode: case insesitive key substring nested_lookups
This commit is contained in:
parent
8ba271daf3
commit
d46f683e76
5 changed files with 58 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ __pycache__*
|
|||
build/*
|
||||
dist/*
|
||||
nested_lookup.egg-info/*
|
||||
.cache/
|
||||
|
|
|
|||
32
README.rst
32
README.rst
|
|
@ -45,6 +45,38 @@ tutorial
|
|||
>>> print(nested_lookup('taco', document))
|
||||
[42, 69]
|
||||
|
||||
|
||||
wild
|
||||
========
|
||||
|
||||
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:
|
||||
|
||||
.. 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,
|
||||
)
|
||||
|
||||
print(results)
|
||||
['test1@example.com', 'test2@example.com', 'test3@example.com']
|
||||
|
||||
|
||||
misc
|
||||
========
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +1,25 @@
|
|||
from six import iteritems
|
||||
|
||||
def nested_lookup(key, document):
|
||||
def nested_lookup(key, document, wild=False):
|
||||
"""Lookup a key in a nested document, return a list of values"""
|
||||
return list(_nested_lookup(key, document))
|
||||
return list(_nested_lookup(key, document, wild=wild))
|
||||
|
||||
def _nested_lookup(key, document):
|
||||
def _nested_lookup(key, document, wild=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):
|
||||
for result in _nested_lookup(key, d, wild=wild):
|
||||
yield result
|
||||
|
||||
if isinstance(document, dict):
|
||||
for k, v in iteritems(document):
|
||||
if k == key:
|
||||
if key == k or (wild and key.lower() in k.lower()):
|
||||
yield v
|
||||
elif isinstance(v, dict):
|
||||
for result in _nested_lookup(key, v):
|
||||
for result in _nested_lookup(key, v, wild=wild):
|
||||
yield result
|
||||
elif isinstance(v, list):
|
||||
for d in v:
|
||||
for result in _nested_lookup(key, d):
|
||||
for result in _nested_lookup(key, d, wild=wild):
|
||||
yield result
|
||||
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -13,7 +13,7 @@ with open('requirements.txt', 'r') as f:
|
|||
|
||||
setup(
|
||||
name = 'nested-lookup',
|
||||
version = '0.1.3',
|
||||
version = '0.1.4',
|
||||
description = 'lookup a key in a deeply nested document of dicts and lists',
|
||||
keywords = 'nested document dictionary dict list lookup schema json xml yaml',
|
||||
long_description = open('README.rst').read(),
|
||||
|
|
|
|||
|
|
@ -35,4 +35,20 @@ class TestNestedLookup(TestCase):
|
|||
self.assertIn(200, results)
|
||||
self.assertSetEqual({100,200}, set(results))
|
||||
|
||||
|
||||
def test_wild_nested_lookup(self):
|
||||
results = nested_lookup(
|
||||
key = 'mail',
|
||||
document = {
|
||||
'name' : 'Russell Ballestrini',
|
||||
'email_address' : 'test1@example.com',
|
||||
'other' : {
|
||||
'secondary_email' : 'test2@example.com',
|
||||
'EMAIL_RECOVERY' : 'test3@example.com',
|
||||
},
|
||||
},
|
||||
wild = True,
|
||||
)
|
||||
self.assertEqual(3, len(results))
|
||||
self.assertIn('test1@example.com', results)
|
||||
self.assertIn('test2@example.com', results)
|
||||
self.assertIn('test3@example.com', results)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue