aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/nested_lookup/lookup_api.py
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-19 13:17:09 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-19 13:17:09 -0500
commitbc1280444ea3d5d4e5b5afd4e39bfb9365689393 (patch)
treeff467903e14b9b66b4644fd86edc81d046187264 /config_file/nested_lookup/lookup_api.py
parentAdd more info on installation with poetry and zsh (diff)
downloadconfig-file-bc1280444ea3d5d4e5b5afd4e39bfb9365689393.zip
Add nested-lookup to project
russellballestrini is a python project in the public domain. Rather than installing it as a dependency, this adds it as part of the config-file package now, which allows more control.
Diffstat (limited to 'config_file/nested_lookup/lookup_api.py')
-rw-r--r--config_file/nested_lookup/lookup_api.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/config_file/nested_lookup/lookup_api.py b/config_file/nested_lookup/lookup_api.py
index a0af63a..e103534 100644
--- a/config_file/nested_lookup/lookup_api.py
+++ b/config_file/nested_lookup/lookup_api.py
@@ -1,7 +1,7 @@
import copy
import warnings
-from six import iteritems
-from nested_lookup import nested_lookup
+
+from .nested_lookup import nested_lookup
def nested_delete(document, key, in_place=False):
@@ -26,7 +26,7 @@ def _nested_delete(document, key):
elif isinstance(document, dict):
if document.get(key):
del document[key]
- for dict_key, dict_value in iteritems(document):
+ for dict_key, dict_value in document.items():
_nested_delete(document=dict_value, key=key)
return document
@@ -34,16 +34,21 @@ def _nested_delete(document, key):
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:
document: Might be List of Dicts (or) Dict of Lists (or)
Dict of List of Dicts etc...
+
key: Key to update the value
+
value: Value to set
+
in_place (bool):
True: modify the dict in place;
False: create a deep copy of the dict and modify it
Defaults to False
- treat_list_element (bool):
+
+ treat_as_element (bool):
True: if a list is provided as "value", the function trys
to match the list elements to the occurences of the key.
If the key occures more often than the provided list has
@@ -51,6 +56,7 @@ def nested_update(document, key, value, in_place=False, treat_as_element=True):
False: the provided list is treated as one scalar value and
will be set as value to every key that matches.
Defaults to True (because of backwards portability of the package).
+
Return:
Returns a document that has updated key, value pair.
"""
@@ -89,7 +95,7 @@ def _nested_update(document, key, value):
document=list_items, key=key, value=value
)
elif isinstance(document, dict):
- for dict_key, dict_value in iteritems(document):
+ for dict_key, dict_value in document.items():
if dict_key == key:
document[key] = value[0]
if len(value) > 1: