parent
4402da08fe
commit
3441584eaa
3 changed files with 51 additions and 38 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
nested_lookup
|
nested_lookup
|
||||||
#############
|
#############
|
||||||
|
|
||||||
.. image:: https://img.shields.io/badge/pypi-0.1.7-green.svg
|
.. image:: https://img.shields.io/badge/pypi-0.1.8-green.svg
|
||||||
:target: https://pypi.python.org/pypi/nested-lookup
|
:target: https://pypi.python.org/pypi/nested-lookup
|
||||||
|
|
||||||
A small Python library which enables:
|
A small Python library which enables:
|
||||||
|
|
|
||||||
|
|
@ -49,17 +49,18 @@ def get_all_keys(dictionary):
|
||||||
result_list = []
|
result_list = []
|
||||||
|
|
||||||
def recrusion(dictionary):
|
def recrusion(dictionary):
|
||||||
if hasattr(dictionary, 'items'):
|
for key, value in iteritems(dictionary):
|
||||||
for key, value in iteritems(dictionary):
|
if isinstance(value, dict):
|
||||||
if isinstance(value, dict):
|
result_list.append(key)
|
||||||
result_list.append(key)
|
recrusion(dictionary=value)
|
||||||
recrusion(dictionary=value)
|
elif isinstance(value, list):
|
||||||
elif isinstance(value, list):
|
result_list.append(key)
|
||||||
result_list.append(key)
|
for list_items in value:
|
||||||
for list_items in value:
|
# Make sure the items inside the list is iterable
|
||||||
|
if hasattr(list_items, 'items'):
|
||||||
recrusion(dictionary=list_items)
|
recrusion(dictionary=list_items)
|
||||||
else:
|
else:
|
||||||
result_list.append(key)
|
result_list.append(key)
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
recrusion(dictionary=dictionary)
|
||||||
return result_list
|
return result_list
|
||||||
|
|
@ -105,17 +106,19 @@ def _get_occurrence(dictionary, item, keyword):
|
||||||
occurrence = [0]
|
occurrence = [0]
|
||||||
|
|
||||||
def recrusion(dictionary):
|
def recrusion(dictionary):
|
||||||
if hasattr(dictionary, 'items'):
|
if item == 'key':
|
||||||
if item == 'key':
|
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
||||||
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
elif keyword in dictionary.values():
|
||||||
elif keyword in dictionary.values():
|
occurrence[0] += dictionary.values().count(keyword)
|
||||||
occurrence[0] += dictionary.values().count(keyword)
|
for key, value in iteritems(dictionary):
|
||||||
for key, value in iteritems(dictionary):
|
if isinstance(value, dict):
|
||||||
if isinstance(value, dict):
|
recrusion(dictionary=value)
|
||||||
recrusion(dictionary=value)
|
elif isinstance(value, list):
|
||||||
elif isinstance(value, list):
|
for list_items in value:
|
||||||
for list_items in value:
|
if hasattr(list_items, 'items'):
|
||||||
recrusion(dictionary=list_items)
|
recrusion(dictionary=list_items)
|
||||||
|
elif list_items == keyword:
|
||||||
|
occurrence[0] += 1 if item == 'value' else 0
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
recrusion(dictionary=dictionary)
|
||||||
return occurrence[0]
|
return occurrence[0]
|
||||||
|
|
|
||||||
|
|
@ -185,23 +185,13 @@ class TestGetAllKeys(TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.sample4 = {
|
self.sample4 = {
|
||||||
"values": [
|
"values": [{
|
||||||
{
|
"checks": [{
|
||||||
"checks": [
|
"monitoring_zones":
|
||||||
{
|
["mzdfw", "mzfra", "mzhkg", "mziad",
|
||||||
"monitoring_zones": [
|
"mzlon", "mzord", "mzsyd"]
|
||||||
"mzdfw",
|
}]
|
||||||
"mzfra",
|
}]
|
||||||
"mzhkg",
|
|
||||||
"mziad",
|
|
||||||
"mzlon",
|
|
||||||
"mzord",
|
|
||||||
"mzsyd"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_sample_data1(self):
|
def test_sample_data1(self):
|
||||||
|
|
@ -250,6 +240,7 @@ class TestGetAllKeys(TestCase):
|
||||||
"monitoring_zones"
|
"monitoring_zones"
|
||||||
]
|
]
|
||||||
for key in keys_to_verify:
|
for key in keys_to_verify:
|
||||||
|
self.assertIn(key, result)
|
||||||
|
|
||||||
|
|
||||||
class TestGetOccurrence(TestCase):
|
class TestGetOccurrence(TestCase):
|
||||||
|
|
@ -307,6 +298,15 @@ class TestGetOccurrence(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_occurrence_of_key(self.sample1, 'build_version')
|
result = get_occurrence_of_key(self.sample1, 'build_version')
|
||||||
|
|
@ -326,6 +326,16 @@ class TestGetOccurrence(TestCase):
|
||||||
result = get_occurrence_of_value(self.sample3, '4')
|
result = get_occurrence_of_value(self.sample3, '4')
|
||||||
self.assertEqual(3, result)
|
self.assertEqual(3, result)
|
||||||
|
|
||||||
|
def test_sample_data4(self):
|
||||||
|
result = get_occurrence_of_key(self.sample4, 'checks')
|
||||||
|
self.assertEqual(1, result)
|
||||||
|
result = get_occurrence_of_value(self.sample4, 'mziad')
|
||||||
|
self.assertEqual(1, result)
|
||||||
|
# Add one more value in key "monitoring_zones" and verify
|
||||||
|
self.sample4['values'][0]['checks'][0]['monitoring_zones'].append(
|
||||||
|
'mziad')
|
||||||
|
self.assertEqual(2, get_occurrence_of_value(self.sample4, 'mziad'))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pass
|
pass
|
||||||
Loading…
Add table
Add a link
Reference in a new issue