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 = []
|
result_list = []
|
||||||
|
|
||||||
def recrusion(dictionary):
|
def recrusion(dictionary):
|
||||||
for key, value in iteritems(dictionary):
|
if hasattr(dictionary, 'items'):
|
||||||
if isinstance(value, dict):
|
for key, value in iteritems(dictionary):
|
||||||
result_list.append(key)
|
if isinstance(value, dict):
|
||||||
recrusion(dictionary=value)
|
result_list.append(key)
|
||||||
elif isinstance(value, list):
|
recrusion(dictionary=value)
|
||||||
result_list.append(key)
|
elif isinstance(value, list):
|
||||||
for list_items in value:
|
result_list.append(key)
|
||||||
recrusion(dictionary=list_items)
|
for list_items in value:
|
||||||
else:
|
recrusion(dictionary=list_items)
|
||||||
result_list.append(key)
|
else:
|
||||||
|
result_list.append(key)
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
recrusion(dictionary=dictionary)
|
||||||
return result_list
|
return result_list
|
||||||
|
|
@ -104,16 +105,17 @@ def _get_occurrence(dictionary, item, keyword):
|
||||||
occurrence = [0]
|
occurrence = [0]
|
||||||
|
|
||||||
def recrusion(dictionary):
|
def recrusion(dictionary):
|
||||||
if item == 'key':
|
if hasattr(dictionary, 'items'):
|
||||||
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
if item == 'key':
|
||||||
elif keyword in dictionary.values():
|
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
||||||
occurrence[0] += dictionary.values().count(keyword)
|
elif keyword in dictionary.values():
|
||||||
for key, value in iteritems(dictionary):
|
occurrence[0] += dictionary.values().count(keyword)
|
||||||
if isinstance(value, dict):
|
for key, value in iteritems(dictionary):
|
||||||
recrusion(dictionary=value)
|
if isinstance(value, dict):
|
||||||
elif isinstance(value, list):
|
recrusion(dictionary=value)
|
||||||
for list_items in value:
|
elif isinstance(value, list):
|
||||||
recrusion(dictionary=list_items)
|
for list_items in value:
|
||||||
|
recrusion(dictionary=list_items)
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
recrusion(dictionary=dictionary)
|
||||||
return occurrence[0]
|
return occurrence[0]
|
||||||
|
|
|
||||||
|
|
@ -184,6 +184,25 @@ class TestGetAllKeys(TestCase):
|
||||||
"memory": "16 GB",
|
"memory": "16 GB",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.sample4 = {
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"checks": [
|
||||||
|
{
|
||||||
|
"monitoring_zones": [
|
||||||
|
"mzdfw",
|
||||||
|
"mzfra",
|
||||||
|
"mzhkg",
|
||||||
|
"mziad",
|
||||||
|
"mzlon",
|
||||||
|
"mzord",
|
||||||
|
"mzsyd"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
def test_sample_data1(self):
|
def test_sample_data1(self):
|
||||||
result = get_all_keys(self.sample1)
|
result = get_all_keys(self.sample1)
|
||||||
|
|
@ -222,6 +241,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_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):
|
class TestGetOccurrence(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue