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/*
|
build/*
|
||||||
dist/*
|
dist/*
|
||||||
nested_lookup.egg-info/*
|
nested_lookup.egg-info/*
|
||||||
|
.cache/
|
||||||
|
|
|
||||||
32
README.rst
32
README.rst
|
|
@ -45,6 +45,38 @@ tutorial
|
||||||
>>> print(nested_lookup('taco', document))
|
>>> print(nested_lookup('taco', document))
|
||||||
[42, 69]
|
[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
|
misc
|
||||||
========
|
========
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,25 @@
|
||||||
from six import iteritems
|
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"""
|
"""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"""
|
"""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:
|
||||||
for result in _nested_lookup(key, d):
|
for result in _nested_lookup(key, d, wild=wild):
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
if isinstance(document, dict):
|
if isinstance(document, dict):
|
||||||
for k, v in iteritems(document):
|
for k, v in iteritems(document):
|
||||||
if k == key:
|
if key == k or (wild and key.lower() in k.lower()):
|
||||||
yield v
|
yield v
|
||||||
elif isinstance(v, dict):
|
elif isinstance(v, dict):
|
||||||
for result in _nested_lookup(key, v):
|
for result in _nested_lookup(key, v, wild=wild):
|
||||||
yield result
|
yield result
|
||||||
elif isinstance(v, list):
|
elif isinstance(v, list):
|
||||||
for d in v:
|
for d in v:
|
||||||
for result in _nested_lookup(key, d):
|
for result in _nested_lookup(key, d, wild=wild):
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
|
|
|
||||||
2
setup.py
2
setup.py
|
|
@ -13,7 +13,7 @@ with open('requirements.txt', 'r') as f:
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name = 'nested-lookup',
|
name = 'nested-lookup',
|
||||||
version = '0.1.3',
|
version = '0.1.4',
|
||||||
description = 'lookup a key in a deeply nested document of dicts and lists',
|
description = 'lookup a key in a deeply nested document of dicts and lists',
|
||||||
keywords = 'nested document dictionary dict list lookup schema json xml yaml',
|
keywords = 'nested document dictionary dict list lookup schema json xml yaml',
|
||||||
long_description = open('README.rst').read(),
|
long_description = open('README.rst').read(),
|
||||||
|
|
|
||||||
|
|
@ -35,4 +35,20 @@ class TestNestedLookup(TestCase):
|
||||||
self.assertIn(200, results)
|
self.assertIn(200, results)
|
||||||
self.assertSetEqual({100,200}, set(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