parent
4402da08fe
commit
3441584eaa
3 changed files with 51 additions and 38 deletions
|
|
@ -1,7 +1,7 @@
|
|||
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
|
||||
|
||||
A small Python library which enables:
|
||||
|
|
|
|||
|
|
@ -49,7 +49,6 @@ def get_all_keys(dictionary):
|
|||
result_list = []
|
||||
|
||||
def recrusion(dictionary):
|
||||
if hasattr(dictionary, 'items'):
|
||||
for key, value in iteritems(dictionary):
|
||||
if isinstance(value, dict):
|
||||
result_list.append(key)
|
||||
|
|
@ -57,6 +56,8 @@ def get_all_keys(dictionary):
|
|||
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:
|
||||
result_list.append(key)
|
||||
|
|
@ -105,7 +106,6 @@ def _get_occurrence(dictionary, item, keyword):
|
|||
occurrence = [0]
|
||||
|
||||
def recrusion(dictionary):
|
||||
if hasattr(dictionary, 'items'):
|
||||
if item == 'key':
|
||||
occurrence[0] += 1 if dictionary.get(keyword) else 0
|
||||
elif keyword in dictionary.values():
|
||||
|
|
@ -115,7 +115,10 @@ def _get_occurrence(dictionary, item, keyword):
|
|||
recrusion(dictionary=value)
|
||||
elif isinstance(value, list):
|
||||
for list_items in value:
|
||||
if hasattr(list_items, 'items'):
|
||||
recrusion(dictionary=list_items)
|
||||
elif list_items == keyword:
|
||||
occurrence[0] += 1 if item == 'value' else 0
|
||||
|
||||
recrusion(dictionary=dictionary)
|
||||
return occurrence[0]
|
||||
|
|
|
|||
|
|
@ -185,23 +185,13 @@ class TestGetAllKeys(TestCase):
|
|||
}
|
||||
}
|
||||
self.sample4 = {
|
||||
"values": [
|
||||
{
|
||||
"checks": [
|
||||
{
|
||||
"monitoring_zones": [
|
||||
"mzdfw",
|
||||
"mzfra",
|
||||
"mzhkg",
|
||||
"mziad",
|
||||
"mzlon",
|
||||
"mzord",
|
||||
"mzsyd"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
"values": [{
|
||||
"checks": [{
|
||||
"monitoring_zones":
|
||||
["mzdfw", "mzfra", "mzhkg", "mziad",
|
||||
"mzlon", "mzord", "mzsyd"]
|
||||
}]
|
||||
}]
|
||||
}
|
||||
|
||||
def test_sample_data1(self):
|
||||
|
|
@ -250,6 +240,7 @@ class TestGetAllKeys(TestCase):
|
|||
"monitoring_zones"
|
||||
]
|
||||
for key in keys_to_verify:
|
||||
self.assertIn(key, result)
|
||||
|
||||
|
||||
class TestGetOccurrence(TestCase):
|
||||
|
|
@ -307,6 +298,15 @@ class TestGetOccurrence(TestCase):
|
|||
"memory": "16 GB",
|
||||
}
|
||||
}
|
||||
self.sample4 = {
|
||||
"values": [{
|
||||
"checks": [{
|
||||
"monitoring_zones":
|
||||
["mzdfw", "mzfra", "mzhkg", "mziad",
|
||||
"mzlon", "mzord", "mzsyd"]
|
||||
}]
|
||||
}]
|
||||
}
|
||||
|
||||
def test_sample_data1(self):
|
||||
result = get_occurrence_of_key(self.sample1, 'build_version')
|
||||
|
|
@ -326,6 +326,16 @@ class TestGetOccurrence(TestCase):
|
|||
result = get_occurrence_of_value(self.sample3, '4')
|
||||
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__":
|
||||
pass
|
||||
Loading…
Add table
Add a link
Reference in a new issue