Provides functions for reading and manipulating dict
.
Function
get_dict_attr()
core_lib.data_transform.helpers.get_dict_attr() [source]
From the dictionary, returns the value for the given path.
def get_dict_attr(obj: dict, path: str):
Arguments
obj
(dict)
: Dictionary to read.path
(str)
: The path to read the value from.
Returns
(any)
: Returns any value present at the target path
Example
from core_lib.data_transform.helpers import get_dict_attr
data = {'name': 'Jon', 'education':{'school': 'Public School'}}
value = get_dict_attr(data, 'education.school')
print(value) # Public School
value = get_dict_attr(data, 'name')
print(value) # Jon
set_dict_attr()
core_lib.data_transform.helpers.set_dict_attr() [source]
Sets the value of a path-described object. It will be created if any section of the object path does not exist.
def set_dict_attr(obj: dict, path: str, value) -> dict:
Arguments
obj
(dict)
: Dictionary to update.path
(str)
: The path to which the value should be set.value
(value)
: The value to be set at the target path.
Returns
(dict)
: Returns the updated dictionary with the new values.
Example
from core_lib.data_transform.helpers import set_dict_attr
data = {'name': 'Jon', 'education':{'school': 'Public School'}}
set_dict_attr(data, 'education.school', 'International School')
print(get_dict_attr(data, 'education.school')) # International School
new_data = set_dict_attr({}, '1.2.3', 'new dict')
print(new_data) # {'1':{'2':{'3': 'new dict'}}}