Repository URL to install this package:
|
Version:
0.1.3 ▾
|
import copy
from typing import Callable, Dict, Iterable, List, Optional, Tuple, Union
def deep_function(func: Callable) -> Callable:
def wrapper(_dict: Dict, keys: Union[List, str], *args: object, **kwargs: object) -> object:
if isinstance(keys, str):
keys = keys.split("__")
return func(_dict, keys, *args, **kwargs)
return wrapper
@deep_function
def deep_pop(_dict: Dict, keys: List, default: Optional[object] = None) -> object:
"""
Pops a value from a nested dictionary using `__` to represent nesting,
if value doesn't exist then return the default (which is set to None)
"""
latest_key = keys[0]
try:
if len(keys) == 1:
return _dict.pop(latest_key)
else:
latest_value = _dict[latest_key]
except KeyError:
return default
return deep_pop(latest_value, keys[1:], default)
@deep_function
def deep_set(_dict: Dict, keys: List, value: object) -> None:
"""
Sets a value in a nested dictionary using `__` to represent nesting,
"""
latest_key = keys[0]
if len(keys) == 1:
_dict[latest_key] = value
else:
if not isinstance(_dict.get(latest_key), dict):
# Handle values that are not dictionaries
_dict[latest_key] = {}
deep_set(_dict[latest_key], keys[1:], value)
def split(_input: Dict, fields_to_match: Iterable[str]) -> Tuple[Dict, Dict]:
"""
A function that takes an input dictionary, popping fields from fields_to_match
returning two dictionaries of matching and non matching fields
"""
input = copy.deepcopy(_input)
matching_result: Dict = {}
for field in fields_to_match:
if value := deep_pop(input, field):
deep_set(matching_result, field, value)
return matching_result, input