Fix for issue #19 (get_all_keys fails if dict are part of list)
This commit is contained in:
parent
4f37d265c8
commit
fdce9e685a
2 changed files with 29 additions and 13 deletions
|
|
@ -54,21 +54,17 @@ def get_all_keys(dictionary):
|
||||||
"""
|
"""
|
||||||
result_list = []
|
result_list = []
|
||||||
|
|
||||||
def recrusion(dictionary):
|
def recrusion(document):
|
||||||
for key, value in iteritems(dictionary):
|
if isinstance(document, list):
|
||||||
if isinstance(value, dict):
|
for list_items in document:
|
||||||
result_list.append(key)
|
recrusion(document=list_items)
|
||||||
recrusion(dictionary=value)
|
elif isinstance(document, dict):
|
||||||
elif isinstance(value, list):
|
for key, value in iteritems(document):
|
||||||
result_list.append(key)
|
|
||||||
for list_items in value:
|
|
||||||
# Make sure the items inside the list is iterable
|
|
||||||
if hasattr(list_items, 'items'):
|
|
||||||
recrusion(dictionary=list_items)
|
|
||||||
else:
|
|
||||||
result_list.append(key)
|
result_list.append(key)
|
||||||
|
recrusion(document=value)
|
||||||
|
return
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
recrusion(document=dictionary)
|
||||||
return result_list
|
return result_list
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -193,6 +193,16 @@ class TestGetAllKeys(TestCase):
|
||||||
}]
|
}]
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
self.sample5 = [{
|
||||||
|
"listings": [{
|
||||||
|
"name": "title",
|
||||||
|
"postcode": "postcode",
|
||||||
|
"full_address": "fulladdress",
|
||||||
|
"city": "city",
|
||||||
|
"lat": "latitude",
|
||||||
|
"lng": "longitude"
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
|
||||||
def test_sample_data1(self):
|
def test_sample_data1(self):
|
||||||
result = get_all_keys(self.sample1)
|
result = get_all_keys(self.sample1)
|
||||||
|
|
@ -242,6 +252,16 @@ class TestGetAllKeys(TestCase):
|
||||||
for key in keys_to_verify:
|
for key in keys_to_verify:
|
||||||
self.assertIn(key, result)
|
self.assertIn(key, result)
|
||||||
|
|
||||||
|
def test_sample_data5(self):
|
||||||
|
result = get_all_keys(self.sample5)
|
||||||
|
self.assertEqual(7, len(result))
|
||||||
|
keys_to_verify = [
|
||||||
|
'listings', 'name', 'postcode', 'full_address', 'city',
|
||||||
|
'lat', 'lng'
|
||||||
|
]
|
||||||
|
for key in keys_to_verify:
|
||||||
|
self.assertIn(key, result)
|
||||||
|
|
||||||
|
|
||||||
class TestGetOccurrence(TestCase):
|
class TestGetOccurrence(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue