Add a new method to return count of values and self values

This commit is contained in:
Matheus Lins 2020-01-28 15:38:07 -03:00 committed by Russell Ballestrini
parent d3e0ea564b
commit 2e8098ef62
3 changed files with 75 additions and 17 deletions

3
.gitignore vendored
View file

@ -7,4 +7,5 @@ dist/*
nested_lookup.egg-info/*
.cache
.vscode
git_commit.sh
git_commit.sh
venv

View file

@ -3,5 +3,6 @@ from .nested_lookup import (
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
get_occurrence_of_value_with_self_value
)
from .lookup_api import nested_update, nested_delete, nested_alter

View file

@ -3,6 +3,9 @@ from six import iteritems
from collections import defaultdict
values_list = []
def nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, return a list of values"""
if with_keys:
@ -80,6 +83,47 @@ 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:
"""
Method to get occurrence of a value in a nested list of dictionary
Args:
items: list of dictionary: Nested dictionary
value: Value to search for the occurrences
Return:
Dict where the key is the value arg and his value is a new
dict with occurrences and values
"""
occurrences = {}
occurrence = 0
value_list = []
for item in items:
occurrence_result, values = _get_occurrence_with_values(dictionary=item, item="value", keyword=value)
occurrence = occurrence + occurrence_result
if occurrence_result:
value_list.extend(values)
occurrences[value] = {
'occurrences': occurrence,
'values': value_list
}
return occurrences
def _get_occurrence_with_values(dictionary, item, keyword):
occurrence = [0]
result_recursion = recursion(dictionary, item, keyword, occurrence, True)
global values_list
values_list = []
return occurrence[0], result_recursion
def get_occurrence_of_value(dictionary, value):
"""
Method to get occurrence of a value in a nested dictionary
@ -93,6 +137,31 @@ 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):
global values_list
if item == "key":
if dictionary.get(keyword) is not None:
occurrence[0] += 1
elif keyword in list(dictionary.values()):
occurrence[0] += list(dictionary.values()).count(keyword)
if with_values:
values_list.append(dictionary)
for key, value in iteritems(dictionary):
if isinstance(value, dict):
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)
elif list_items == keyword:
occurrence[0] += 1 if item == "value" else 0
if values_list:
return values_list
def _get_occurrence(dictionary, item, keyword):
"""
Method to get occurrence of a key or value in a nested dictionary
@ -105,22 +174,9 @@ def _get_occurrence(dictionary, item, keyword):
Number of occurrence of the given keyword in the dict
"""
occurrence = [0]
recursion(dictionary, item, keyword, occurrence)
def recrusion(dictionary):
if item == "key":
if dictionary.get(keyword) is not None:
occurrence[0] += 1
elif keyword in list(dictionary.values()):
occurrence[0] += list(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
global values_list
values_list = []
recrusion(dictionary=dictionary)
return occurrence[0]