From 77895304bb117e980a2f733aa8ebe56cdf14aa87 Mon Sep 17 00:00:00 2001 From: Matheus Lins Date: Wed, 29 Jan 2020 09:37:43 -0300 Subject: [PATCH] Rename function --- nested_lookup/__init__.py | 2 +- nested_lookup/nested_lookup.py | 12 ++++++------ test_nested_lookup.py | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/nested_lookup/__init__.py b/nested_lookup/__init__.py index 23bdab3..0840df6 100644 --- a/nested_lookup/__init__.py +++ b/nested_lookup/__init__.py @@ -3,6 +3,6 @@ from .nested_lookup import ( get_all_keys, get_occurrence_of_key, get_occurrence_of_value, - get_occurrence_of_value_with_self_value + get_occurrences_and_values ) from .lookup_api import nested_update, nested_delete, nested_alter diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index 5e77757..8de0b4b 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -83,7 +83,7 @@ def get_occurrence_of_key(dictionary, key): return _get_occurrence(dictionary=dictionary, item="key", keyword=key) -def get_occurrence_of_value_with_self_value(items: list, value: any) -> dict: +def get_occurrences_and_values(items: list, value: any) -> dict: """ Method to get occurrence of a value in a nested list of dictionary @@ -116,7 +116,7 @@ def get_occurrence_of_value_with_self_value(items: list, value: any) -> dict: def _get_occurrence_with_values(dictionary, item, keyword): occurrence = [0] - result_recursion = recursion(dictionary, item, keyword, occurrence, True) + result_recursion = _recursion(dictionary, item, keyword, occurrence, True) global values_list values_list = [] @@ -137,7 +137,7 @@ def get_occurrence_of_value(dictionary, value): return _get_occurrence(dictionary=dictionary, item="value", keyword=value) -def recursion(dictionary, item, keyword, occurrence, with_values=False): +def _recursion(dictionary, item, keyword, occurrence, with_values=False): global values_list @@ -150,11 +150,11 @@ def recursion(dictionary, item, keyword, occurrence, with_values=False): values_list.append(dictionary) for key, value in iteritems(dictionary): if isinstance(value, dict): - recursion(value, item, keyword, occurrence, with_values) + _recursion(value, item, keyword, occurrence, with_values) elif isinstance(value, list): for list_items in value: if hasattr(list_items, "items"): - recursion(list_items, item, keyword, occurrence, with_values) + _recursion(list_items, item, keyword, occurrence, with_values) elif list_items == keyword: occurrence[0] += 1 if item == "value" else 0 @@ -174,7 +174,7 @@ def _get_occurrence(dictionary, item, keyword): Number of occurrence of the given keyword in the dict """ occurrence = [0] - recursion(dictionary, item, keyword, occurrence) + _recursion(dictionary, item, keyword, occurrence) global values_list values_list = [] diff --git a/test_nested_lookup.py b/test_nested_lookup.py index c7064e1..b1bf44b 100644 --- a/test_nested_lookup.py +++ b/test_nested_lookup.py @@ -5,7 +5,7 @@ from nested_lookup import ( get_all_keys, get_occurrence_of_key, get_occurrence_of_value, - get_occurrence_of_value_with_self_value + get_occurrences_and_values ) @@ -418,25 +418,25 @@ class TestGetOccurrence(TestCase): def test_sample_data6(self): value = '4' - result = get_occurrence_of_value_with_self_value(self.sample6, value) + result = get_occurrences_and_values(self.sample6, value) self.assertEqual(2, result[value]['occurrences']) self.assertEqual(2, len(result[value]['values'])) def test_sample_data7(self): value = '2.7 GHz' - result = get_occurrence_of_value_with_self_value(self.sample6, value) + result = get_occurrences_and_values(self.sample6, value) self.assertEqual(1, result[value]['occurrences']) self.assertEqual(1, len(result[value]['values'])) def test_sample_data8(self): value = '4' - result = get_occurrence_of_value_with_self_value(self.sample7, value) + result = get_occurrences_and_values(self.sample7, value) self.assertEqual(4, result[value]['occurrences']) self.assertEqual(4, len(result[value]['values'])) def test_sample_data9(self): value = '5' - result = get_occurrence_of_value_with_self_value(self.sample7, value) + result = get_occurrences_and_values(self.sample7, value) self.assertEqual(0, result[value]['occurrences']) self.assertEqual(0, len(result[value]['values']))