Source code for torx.fileio.recursive_rename_dictkeys_m
"""Function for renaming dictionaries of data."""
from torx.autodoc_decorators_m import autodoc_function
[docs]
@autodoc_function
def recursive_rename_keys(old_dict: dict, old_to_new_map: dict):
"""Update all keys according to a map in a nested dict."""
if not isinstance(old_dict, dict):
return old_dict
new_dict = {}
for key, value in old_dict.items():
if key in old_to_new_map:
new_dict[old_to_new_map[key]] = recursive_rename_keys(value, old_to_new_map)
else:
new_dict[key] = recursive_rename_keys(value, old_to_new_map)
return new_dict