From 3441584eaa1b9adcc99d5121cc81d794deb199f4 Mon Sep 17 00:00:00 2001 From: Ramesh RV Date: Fri, 2 Nov 2018 21:38:13 +0530 Subject: [PATCH] Fix for issue #10 (#11) * Fix for issue #10 * Renamed the test module --- README.rst | 2 +- nested_lookup/nested_lookup.py | 43 +++++++++--------- ...nested_loopkup.py => test_nested_lookup.py | 44 ++++++++++++------- 3 files changed, 51 insertions(+), 38 deletions(-) rename test_nested_loopkup.py => test_nested_lookup.py (91%) diff --git a/README.rst b/README.rst index 4a0b839..a46e2ec 100644 --- a/README.rst +++ b/README.rst @@ -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: diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index d38dd47..86e264c 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -49,17 +49,18 @@ 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) - recrusion(dictionary=value) - elif isinstance(value, list): - result_list.append(key) - for list_items in value: + 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: - result_list.append(key) + else: + result_list.append(key) recrusion(dictionary=dictionary) return result_list @@ -105,17 +106,19 @@ 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(): - 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: + 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: + 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] diff --git a/test_nested_loopkup.py b/test_nested_lookup.py similarity index 91% rename from test_nested_loopkup.py rename to test_nested_lookup.py index 3d617bc..cc344b0 100644 --- a/test_nested_loopkup.py +++ b/test_nested_lookup.py @@ -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