From 106989b29bd44c5a555a084eda148f2fd7928231 Mon Sep 17 00:00:00 2001 From: dostuffthatmatters Date: Wed, 3 May 2023 16:01:10 +0200 Subject: [PATCH] #165 Use `merge_dicts` from tum-esm-utils Close #165 --- packages/cli/commands/config.py | 6 ++- packages/core/interfaces/state_interface.py | 4 +- packages/core/utils/__init__.py | 1 - .../core/utils/update_dict_recursively.py | 41 ------------------- 4 files changed, 6 insertions(+), 46 deletions(-) delete mode 100644 packages/core/utils/update_dict_recursively.py diff --git a/packages/cli/commands/config.py b/packages/cli/commands/config.py index da97c06b..fe56314a 100644 --- a/packages/cli/commands/config.py +++ b/packages/cli/commands/config.py @@ -7,7 +7,6 @@ import tum_esm_utils from packages.core import types -from packages.core.utils import update_dict_recursively _dir = os.path.dirname _PROJECT_DIR = _dir(_dir(_dir(_dir(os.path.abspath(__file__))))) @@ -77,7 +76,10 @@ def _update_config(content: str) -> None: return # merge current config and new partial config - merged_json = update_dict_recursively(current_json, new_partial_json) + merged_json = tum_esm_utils.datastructures.merge_dicts( + current_json, + new_partial_json, + ) with open(_CONFIG_FILE_PATH, "w") as f: json.dump(merged_json, f, indent=4) diff --git a/packages/core/interfaces/state_interface.py b/packages/core/interfaces/state_interface.py index 4e0d945c..3f51c17c 100644 --- a/packages/core/interfaces/state_interface.py +++ b/packages/core/interfaces/state_interface.py @@ -180,7 +180,7 @@ def update(update: types.StateDictPartial) -> None: changed in contrast to containing the whole file.""" current_state = StateInterface.read_without_filelock() - new_state = utils.update_dict_recursively(current_state, update) + new_state = tum_esm_utils.datastructures.merge_dicts(current_state, update) with open(_STATE_FILE_PATH, "w") as f: json.dump(new_state, f, indent=4) @@ -195,6 +195,6 @@ def update_persistent(update: types.PersistentStateDictPartial) -> None: changed in contrast to containing the whole file.""" current_state = StateInterface.read_persistent_without_filelock() - new_state = utils.update_dict_recursively(current_state, update) + new_state = tum_esm_utils.datastructures.merge_dicts(current_state, update) with open(_PERSISTENT_STATE_FILE_PATH, "w") as f: json.dump(new_state, f, indent=4) diff --git a/packages/core/utils/__init__.py b/packages/core/utils/__init__.py index a04fe070..ed0c773b 100644 --- a/packages/core/utils/__init__.py +++ b/packages/core/utils/__init__.py @@ -1,5 +1,4 @@ from .logger import Logger from .astronomy import Astronomy from .exception_email_client import ExceptionEmailClient -from .update_dict_recursively import update_dict_recursively from .helios_image_processing import HeliosImageProcessing diff --git a/packages/core/utils/update_dict_recursively.py b/packages/core/utils/update_dict_recursively.py deleted file mode 100644 index 016917e2..00000000 --- a/packages/core/utils/update_dict_recursively.py +++ /dev/null @@ -1,41 +0,0 @@ -from typing import Any - - -# TODO: use tum_esm_utils - - -def update_dict_recursively(old_object: Any, new_object: Any) -> Any: - """Update a given dict recursively with data from a new dict. - - It will not add any properties and assert that the types - remain the same (or null). null->int or int->null is possible - but not int->dict or list->int. - - example: - ```python - merge_dicts( - old_object={"a": 3, "b": {"c": 50, "e": None}}, - new_object={"b": {"e": 80}}, - ) == {"a": 3, "b": {"c": 50, "e": 80}} - ``` - """ - - if old_object is None or new_object is None: - return new_object - - # if the old_object is a dict, loop through - if type(old_object) == dict: - assert type(new_object) == dict - updated_dict = {} - for key in old_object.keys(): - if key in new_object: - updated_dict[key] = update_dict_recursively(old_object[key], new_object[key]) - else: - updated_dict[key] = old_object[key] - return updated_dict - else: - if type(old_object) in [int, float]: - assert type(new_object) in [int, float] - else: - assert type(old_object) == type(new_object) - return new_object