Fixed issue where get_all_keys or _get_occurence choked on strings. (#9)
This commit is contained in:
parent
08967c817b
commit
7513169ab4
2 changed files with 51 additions and 20 deletions
|
|
@ -49,16 +49,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:
|
||||
recrusion(dictionary=list_items)
|
||||
else:
|
||||
result_list.append(key)
|
||||
if hasattr(dictionary, 'items'):
|
||||
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:
|
||||
recrusion(dictionary=list_items)
|
||||
else:
|
||||
result_list.append(key)
|
||||
|
||||
recrusion(dictionary=dictionary)
|
||||
return result_list
|
||||
|
|
@ -104,16 +105,17 @@ def _get_occurrence(dictionary, item, keyword):
|
|||
occurrence = [0]
|
||||
|
||||
def recrusion(dictionary):
|
||||
if item == 'key':
|
||||
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
||||
elif keyword in dictionary.values():
|
||||
occurrence[0] += dictionary.values().count(keyword)
|
||||
for key, value in iteritems(dictionary):
|
||||
if isinstance(value, dict):
|
||||
recrusion(dictionary=value)
|
||||
elif isinstance(value, list):
|
||||
for list_items in value:
|
||||
recrusion(dictionary=list_items)
|
||||
if hasattr(dictionary, 'items'):
|
||||
if item == 'key':
|
||||
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
||||
elif keyword in dictionary.values():
|
||||
occurrence[0] += dictionary.values().count(keyword)
|
||||
for key, value in iteritems(dictionary):
|
||||
if isinstance(value, dict):
|
||||
recrusion(dictionary=value)
|
||||
elif isinstance(value, list):
|
||||
for list_items in value:
|
||||
recrusion(dictionary=list_items)
|
||||
|
||||
recrusion(dictionary=dictionary)
|
||||
return occurrence[0]
|
||||
|
|
|
|||
|
|
@ -184,6 +184,25 @@ class TestGetAllKeys(TestCase):
|
|||
"memory": "16 GB",
|
||||
}
|
||||
}
|
||||
self.sample4 = {
|
||||
"values": [
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"monitoring_zones": [
|
||||
"mzdfw",
|
||||
"mzfra",
|
||||
"mzhkg",
|
||||
"mziad",
|
||||
"mzlon",
|
||||
"mzord",
|
||||
"mzsyd"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
def test_sample_data1(self):
|
||||
result = get_all_keys(self.sample1)
|
||||
|
|
@ -222,6 +241,16 @@ class TestGetAllKeys(TestCase):
|
|||
for key in keys_to_verify:
|
||||
self.assertIn(key, result)
|
||||
|
||||
def test_sample_data4(self):
|
||||
result = get_all_keys(self.sample4)
|
||||
self.assertEqual(3, len(result))
|
||||
keys_to_verify = [
|
||||
"values",
|
||||
"checks",
|
||||
"monitoring_zones"
|
||||
]
|
||||
for key in keys_to_verify:
|
||||
|
||||
|
||||
class TestGetOccurrence(TestCase):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue