modified: README.rst

modified:   nested_lookup/__init__.py
	modified:   nested_lookup/lookup_api.py
	modified:   nested_lookup/nested_lookup.py
	modified:   setup.py
	modified:   test_lookup_api.py
	modified:   test_nested_lookup.py
This commit is contained in:
russellballestrini 2019-05-21 10:23:00 -04:00
parent 8cec46c76e
commit 9348af1790
7 changed files with 361 additions and 387 deletions

View file

@ -1,3 +1,7 @@
from .nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\
get_occurrence_of_value
from .nested_lookup import (
nested_lookup,
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
)
from .lookup_api import nested_update, nested_delete, nested_alter

View file

@ -31,9 +31,7 @@ def _nested_delete(document, key):
return document
def nested_update(
document, key, value, in_place=False, treat_as_element=True
):
def nested_update(document, key, value, in_place=False, treat_as_element=True):
"""
Method to update a key->value pair in a nested document
Args:
@ -61,8 +59,9 @@ def nested_update(
# from the scalar value
# check the length of the list and provide it to _nested_update
if not treat_as_element and not isinstance(value, list):
raise Exception('You need to pass value as list if you opt for' +
'this feature')
raise Exception(
"You need to pass value as list if you opt for" + "this feature"
)
elif treat_as_element:
value = [value]
@ -70,13 +69,10 @@ def nested_update(
if not in_place:
document = copy.deepcopy(document)
return _nested_update(document=document, key=key, value=value,
val_len=val_len)
return _nested_update(document=document, key=key, value=value, val_len=val_len)
def _nested_update(
document, key, value, val_len, run=0
):
def _nested_update(document, key, value, val_len, run=0):
"""
Method to update a key->value pair in a nested document
Args:
@ -95,8 +91,9 @@ def _nested_update(
"""
if isinstance(document, list):
for list_items in document:
_nested_update(document=list_items, key=key, value=value,
val_len=val_len, run=run)
_nested_update(
document=list_items, key=key, value=value, val_len=val_len, run=run
)
elif isinstance(document, dict):
if document.get(key):
# check if a value with the coresponding index exists and
@ -109,14 +106,20 @@ def _nested_update(
document[key] = val
run = run + 1
for dict_key, dict_value in iteritems(document):
_nested_update(document=dict_value, key=key, value=value,
val_len=val_len, run=run)
_nested_update(
document=dict_value, key=key, value=value, val_len=val_len, run=run
)
return document
def nested_alter(
document, key, callback_function=None, function_parameters=None,
conversion_function=None, wild_alter=False, in_place=True
document,
key,
callback_function=None,
function_parameters=None,
conversion_function=None,
wild_alter=False,
in_place=True,
):
"""
Method to alter all values of the occurences of the key "key".
@ -155,17 +158,20 @@ def nested_alter(
if not in_place:
document = copy.deepcopy(document)
return _nested_alter(document=document, keys=key,
callback_function=callback_function,
function_parameters=function_parameters,
conversion_function=conversion_function,
wild_alter=wild_alter,
in_place=in_place, key_len=key_len)
return _nested_alter(
document=document,
keys=key,
callback_function=callback_function,
function_parameters=function_parameters,
conversion_function=conversion_function,
wild_alter=wild_alter,
in_place=in_place,
key_len=key_len,
)
def _call_callback(
value_list, callback_function, function_parameters,
conversion_function
value_list, callback_function, function_parameters, conversion_function
):
"""
internal helper to call the callback function
@ -188,8 +194,14 @@ def _call_callback(
def _nested_alter(
document, keys, callback_function, function_parameters,
conversion_function, wild_alter, in_place, key_len
document,
keys,
callback_function,
function_parameters,
conversion_function,
wild_alter,
in_place,
key_len,
):
"""
"""
@ -201,17 +213,15 @@ def _nested_alter(
# iterate over all given keys in the list
for key in keys:
# try to find the key:
findings = nested_lookup(
key, document, with_keys=True, wild=wild_alter
)
findings = nested_lookup(key, document, with_keys=True, wild=wild_alter)
for k, v in findings.items():
trans_val = _call_callback(v, callback_function,
function_parameters,
conversion_function)
trans_val = _call_callback(
v, callback_function, function_parameters, conversion_function
)
# use the transformed value and apply the update to the key
# (dont treat the lists as elements here)
document = nested_update(document, k, trans_val,
in_place=in_place,
treat_as_element=False)
document = nested_update(
document, k, trans_val, in_place=in_place, treat_as_element=False
)
return document

View file

@ -7,9 +7,7 @@ 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:
d = defaultdict(list)
for k, v in _nested_lookup(
key, document, wild=wild, with_keys=with_keys
):
for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
d[k].append(v)
return d
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
@ -19,9 +17,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, yield a value"""
if isinstance(document, list):
for d in document:
for result in _nested_lookup(
key, d, wild=wild, with_keys=with_keys
):
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
yield result
if isinstance(document, dict):
@ -32,9 +28,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
else:
yield v
if isinstance(v, dict):
for result in _nested_lookup(
key, v, wild=wild, with_keys=with_keys
):
for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
yield result
elif isinstance(v, list):
for d in v:
@ -78,7 +72,7 @@ def get_occurrence_of_key(dictionary, key):
Return:
Number of occurrence (Integer)
"""
return _get_occurrence(dictionary=dictionary, item='key', keyword=key)
return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
def get_occurrence_of_value(dictionary, value):
@ -91,7 +85,7 @@ def get_occurrence_of_value(dictionary, value):
Return:
Number of occurrence (Integer)
"""
return _get_occurrence(dictionary=dictionary, item='value', keyword=value)
return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
def _get_occurrence(dictionary, item, keyword):
@ -108,7 +102,7 @@ def _get_occurrence(dictionary, item, keyword):
occurrence = [0]
def recrusion(dictionary):
if item == 'key':
if item == "key":
if dictionary.get(keyword) is not None:
occurrence[0] += 1
elif keyword in list(dictionary.values()):
@ -118,10 +112,10 @@ def _get_occurrence(dictionary, item, keyword):
recrusion(dictionary=value)
elif isinstance(value, list):
for list_items in value:
if hasattr(list_items, 'items'):
if hasattr(list_items, "items"):
recrusion(dictionary=list_items)
elif list_items == keyword:
occurrence[0] += 1 if item == 'value' else 0
occurrence[0] += 1 if item == "value" else 0
recrusion(dictionary=dictionary)
return occurrence[0]