Rename function

This commit is contained in:
Matheus Lins 2020-01-29 09:37:43 -03:00 committed by Russell Ballestrini
parent 5fbc1a1671
commit 77895304bb
3 changed files with 12 additions and 12 deletions

View file

@ -3,6 +3,6 @@ from .nested_lookup import (
get_all_keys, get_all_keys,
get_occurrence_of_key, get_occurrence_of_key,
get_occurrence_of_value, 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 from .lookup_api import nested_update, nested_delete, nested_alter

View file

@ -83,7 +83,7 @@ def get_occurrence_of_key(dictionary, key):
return _get_occurrence(dictionary=dictionary, item="key", keyword=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 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): def _get_occurrence_with_values(dictionary, item, keyword):
occurrence = [0] occurrence = [0]
result_recursion = recursion(dictionary, item, keyword, occurrence, True) result_recursion = _recursion(dictionary, item, keyword, occurrence, True)
global values_list global values_list
values_list = [] values_list = []
@ -137,7 +137,7 @@ def get_occurrence_of_value(dictionary, value):
return _get_occurrence(dictionary=dictionary, item="value", keyword=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 global values_list
@ -150,11 +150,11 @@ def recursion(dictionary, item, keyword, occurrence, with_values=False):
values_list.append(dictionary) values_list.append(dictionary)
for key, value in iteritems(dictionary): for key, value in iteritems(dictionary):
if isinstance(value, dict): if isinstance(value, dict):
recursion(value, item, keyword, occurrence, with_values) _recursion(value, item, keyword, occurrence, with_values)
elif isinstance(value, list): elif isinstance(value, list):
for list_items in value: for list_items in value:
if hasattr(list_items, "items"): 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: elif list_items == keyword:
occurrence[0] += 1 if item == "value" else 0 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 Number of occurrence of the given keyword in the dict
""" """
occurrence = [0] occurrence = [0]
recursion(dictionary, item, keyword, occurrence) _recursion(dictionary, item, keyword, occurrence)
global values_list global values_list
values_list = [] values_list = []

View file

@ -5,7 +5,7 @@ from nested_lookup import (
get_all_keys, get_all_keys,
get_occurrence_of_key, get_occurrence_of_key,
get_occurrence_of_value, 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): def test_sample_data6(self):
value = '4' 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, result[value]['occurrences'])
self.assertEqual(2, len(result[value]['values'])) self.assertEqual(2, len(result[value]['values']))
def test_sample_data7(self): def test_sample_data7(self):
value = '2.7 GHz' 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, result[value]['occurrences'])
self.assertEqual(1, len(result[value]['values'])) self.assertEqual(1, len(result[value]['values']))
def test_sample_data8(self): def test_sample_data8(self):
value = '4' 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, result[value]['occurrences'])
self.assertEqual(4, len(result[value]['values'])) self.assertEqual(4, len(result[value]['values']))
def test_sample_data9(self): def test_sample_data9(self):
value = '5' 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, result[value]['occurrences'])
self.assertEqual(0, len(result[value]['values'])) self.assertEqual(0, len(result[value]['values']))