Reverted PEP8 correction
This commit is contained in:
parent
bb7ab509eb
commit
c1ca4c1e1e
2 changed files with 26 additions and 39 deletions
|
|
@ -2,26 +2,20 @@ from six import iteritems
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
def nested_lookup(key, document, wild=False, with_keys=False):
|
def nested_lookup(key, document, wild=False, with_keys=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"""
|
||||||
if with_keys:
|
if with_keys:
|
||||||
d = defaultdict(list)
|
d = defaultdict(list)
|
||||||
for k, v in _nested_lookup(
|
for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
|
||||||
key, document, wild=wild, with_keys=with_keys
|
|
||||||
):
|
|
||||||
d[k].append(v)
|
d[k].append(v)
|
||||||
return d
|
return d
|
||||||
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
|
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
|
||||||
|
|
||||||
|
|
||||||
def _nested_lookup(key, document, wild=False, with_keys=False):
|
def _nested_lookup(key, document, wild=False, with_keys=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(
|
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
|
||||||
key, d, wild=wild, with_keys=with_keys
|
|
||||||
):
|
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
if isinstance(document, dict):
|
if isinstance(document, dict):
|
||||||
|
|
@ -32,15 +26,11 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
|
||||||
else:
|
else:
|
||||||
yield v
|
yield v
|
||||||
elif isinstance(v, dict):
|
elif isinstance(v, dict):
|
||||||
for result in _nested_lookup(
|
for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
|
||||||
key, v, wild=wild, with_keys=with_keys
|
|
||||||
):
|
|
||||||
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(
|
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
|
||||||
key, d, wild=wild, with_keys=with_keys
|
|
||||||
):
|
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,14 @@ from nested_lookup import nested_lookup, get_all_keys
|
||||||
class TestNestedLookup(TestCase):
|
class TestNestedLookup(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.subject_dict = {'a': 1, 'b': {'d': 100}, 'c': {'d': 200}}
|
self.subject_dict = {'a':1,'b':{'d':100},'c':{'d':200}}
|
||||||
self.subject_dict2 = {
|
self.subject_dict2 = {
|
||||||
'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',
|
||||||
'email_address': 'test4@example.com',
|
'email_address' : 'test4@example.com',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,34 +22,34 @@ class TestNestedLookup(TestCase):
|
||||||
self.assertEqual(2, len(results))
|
self.assertEqual(2, len(results))
|
||||||
self.assertIn(100, results)
|
self.assertIn(100, results)
|
||||||
self.assertIn(200, results)
|
self.assertIn(200, results)
|
||||||
self.assertSetEqual({100, 200}, set(results))
|
self.assertSetEqual({100,200}, set(results))
|
||||||
|
|
||||||
def test_nested_lookup_wrapped_in_list(self):
|
def test_nested_lookup_wrapped_in_list(self):
|
||||||
results = nested_lookup('d', [{}, self.subject_dict, {}])
|
results = nested_lookup('d', [{}, self.subject_dict, {}])
|
||||||
self.assertEqual(2, len(results))
|
self.assertEqual(2, len(results))
|
||||||
self.assertIn(100, results)
|
self.assertIn(100, results)
|
||||||
self.assertIn(200, results)
|
self.assertIn(200, results)
|
||||||
self.assertSetEqual({100, 200}, set(results))
|
self.assertSetEqual({100,200}, set(results))
|
||||||
|
|
||||||
def test_nested_lookup_wrapped_in_list_in_dict_in_list(self):
|
def test_nested_lookup_wrapped_in_list_in_dict_in_list(self):
|
||||||
results = nested_lookup('d', [{}, {'H': [self.subject_dict]}])
|
results = nested_lookup('d', [{}, {'H' : [self.subject_dict]} ])
|
||||||
self.assertEqual(2, len(results))
|
self.assertEqual(2, len(results))
|
||||||
self.assertIn(100, results)
|
self.assertIn(100, results)
|
||||||
self.assertIn(200, results)
|
self.assertIn(200, results)
|
||||||
self.assertSetEqual({100, 200}, set(results))
|
self.assertSetEqual({100,200}, set(results))
|
||||||
|
|
||||||
def test_nested_lookup_wrapped_in_list_in_list(self):
|
def test_nested_lookup_wrapped_in_list_in_list(self):
|
||||||
results = nested_lookup('d', [{}, [self.subject_dict, {}]])
|
results = nested_lookup('d', [ {}, [self.subject_dict, {}] ])
|
||||||
self.assertEqual(2, len(results))
|
self.assertEqual(2, len(results))
|
||||||
self.assertIn(100, results)
|
self.assertIn(100, results)
|
||||||
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):
|
def test_wild_nested_lookup(self):
|
||||||
results = nested_lookup(
|
results = nested_lookup(
|
||||||
key='mail',
|
key = 'mail',
|
||||||
document=self.subject_dict2,
|
document = self.subject_dict2
|
||||||
wild=True,
|
wild = True,
|
||||||
)
|
)
|
||||||
self.assertEqual(4, len(results))
|
self.assertEqual(4, len(results))
|
||||||
self.assertIn('test1@example.com', results)
|
self.assertIn('test1@example.com', results)
|
||||||
|
|
@ -58,26 +58,23 @@ class TestNestedLookup(TestCase):
|
||||||
|
|
||||||
def test_wild_with_keys_nested_lookup(self):
|
def test_wild_with_keys_nested_lookup(self):
|
||||||
matches = nested_lookup(
|
matches = nested_lookup(
|
||||||
key='mail',
|
key = 'mail',
|
||||||
document=self.subject_dict2,
|
document = self.subject_dict2,
|
||||||
wild=True,
|
wild = True,
|
||||||
with_keys=True,
|
with_keys = True,
|
||||||
)
|
)
|
||||||
self.assertEqual(3, len(matches))
|
self.assertEqual(3, len(matches))
|
||||||
self.assertIn('email_address', matches)
|
self.assertIn('email_address', matches)
|
||||||
self.assertIn('secondary_email', matches)
|
self.assertIn('secondary_email', matches)
|
||||||
self.assertIn('EMAIL_RECOVERY', matches)
|
self.assertIn('EMAIL_RECOVERY', matches)
|
||||||
self.assertSetEqual(
|
self.assertSetEqual({'test1@example.com','test4@example.com'}, set(matches['email_address']))
|
||||||
{'test1@example.com', 'test4@example.com'},
|
|
||||||
set(matches['email_address'])
|
|
||||||
)
|
|
||||||
self.assertIn('test2@example.com', matches['secondary_email'])
|
self.assertIn('test2@example.com', matches['secondary_email'])
|
||||||
|
|
||||||
def test_nested_lookup_with_keys(self):
|
def test_nested_lookup_with_keys(self):
|
||||||
matches = nested_lookup('d', self.subject_dict, with_keys=True)
|
matches = nested_lookup('d', self.subject_dict, with_keys=True)
|
||||||
self.assertIn('d', matches)
|
self.assertIn('d', matches)
|
||||||
self.assertEqual(2, len(matches['d']))
|
self.assertEqual(2, len(matches['d']))
|
||||||
self.assertSetEqual({100, 200}, set(matches['d']))
|
self.assertSetEqual({100,200}, set(matches['d']))
|
||||||
|
|
||||||
|
|
||||||
class TestGetAllKeys(TestCase):
|
class TestGetAllKeys(TestCase):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue