Source code for gain.utils.dict_utils

from copy import deepcopy
from typing import Any


[docs] def recursive_dict_update( input_dict: dict[str, Any], updater_dict: dict[str, Any], ) -> dict[str, Any]: """Recursively update a dictionary with another dictionary.""" # This method cannot handle nested dictionaries # that hold a reference to the dictionary that # contains them. If such a dictionary is given # to this function, it will reach the maximum # recursion depth. result_dict = deepcopy(input_dict) for key, val in updater_dict.items(): if key in result_dict and isinstance(val, dict): assert isinstance(result_dict[key], dict), result_dict[key] result_dict[key] = recursive_dict_update( result_dict[key], updater_dict[key], ) else: result_dict[key] = updater_dict[key] return result_dict
[docs] def recursive_dict_update_inplace( input_dict: dict[str, Any], updater_dict: dict[str, Any], ) -> None: """Recursively update a dictionary with another dictionary.""" # This method cannot handle nested dictionaries # that hold a reference to the dictionary that # contains them. If such a dictionary is given # to this function, it will reach the maximum # recursion depth. for key, val in updater_dict.items(): if key in input_dict and isinstance(val, dict): assert isinstance(input_dict[key], dict), input_dict[key] recursive_dict_update_inplace( input_dict[key], updater_dict[key], ) else: input_dict[key] = updater_dict[key]