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 = []
|
||||
|
||||
def recrusion(dictionary):
|
||||
for key, value in iteritems(dictionary):
|
||||
if isinstance(value, dict):
|
||||
result_list.append(key)
|
||||
recrusion(dictionary=value)
|
||||
elif isinstance(value, list):
|
||||
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:
|
||||
def recrusion(document):
|
||||
if isinstance(document, list):
|
||||
for list_items in document:
|
||||
recrusion(document=list_items)
|
||||
elif isinstance(document, dict):
|
||||
for key, value in iteritems(document):
|
||||
result_list.append(key)
|
||||
recrusion(document=value)
|
||||
return
|
||||
|
||||
recrusion(dictionary=dictionary)
|
||||
recrusion(document=dictionary)
|
||||
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):
|
||||
result = get_all_keys(self.sample1)
|
||||
|
|
@ -242,6 +252,16 @@ class TestGetAllKeys(TestCase):
|
|||
for key in keys_to_verify:
|
||||
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):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue