From fe69530b38184aad7204c1a12550ff60a6d5cb5b Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 23 Oct 2025 15:31:52 +0900 Subject: [PATCH] Add a simple add key entry to dictionary --- src/corelibs/iterator_handling/dict_helpers.py | 18 ++++++++++++++++++ test-run/iterator_handling/dict_helpers.py | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/corelibs/iterator_handling/dict_helpers.py b/src/corelibs/iterator_handling/dict_helpers.py index 4740be7..daefc5e 100644 --- a/src/corelibs/iterator_handling/dict_helpers.py +++ b/src/corelibs/iterator_handling/dict_helpers.py @@ -82,4 +82,22 @@ def mask( for key, value in data_set.items() } + +def set_entry(dict_set: dict[str, Any], key: str, value_set: Any) -> dict[str, Any]: + """ + set a new entry in the dict set + + Arguments: + key {str} -- _description_ + dict_set {dict[str, Any]} -- _description_ + value_set {Any} -- _description_ + + Returns: + dict[str, Any] -- _description_ + """ + if not dict_set.get(key): + dict_set[key] = {} + dict_set[key] = value_set + return dict_set + # __END__ diff --git a/test-run/iterator_handling/dict_helpers.py b/test-run/iterator_handling/dict_helpers.py index ce65ac2..f56c169 100644 --- a/test-run/iterator_handling/dict_helpers.py +++ b/test-run/iterator_handling/dict_helpers.py @@ -2,8 +2,9 @@ Iterator helper testing """ +from typing import Any from corelibs.debug_handling.dump_data import dump_data -from corelibs.iterator_handling.dict_helpers import mask +from corelibs.iterator_handling.dict_helpers import mask, set_entry def __mask(): @@ -95,11 +96,23 @@ def __mask(): print(f"===> Masked: {dump_data(result)}") +def __set_dict_value_entry(): + + dict_empty: dict[str, Any] = {} + new = set_entry(dict_empty, 'a.b.c', 1) + print(f"[1] Set dict entry: {dump_data(new)}") + new = set_entry(new, 'dict', {'key': 'value'}) + print(f"[2] Set dict entry: {dump_data(new)}") + new = set_entry(new, 'list', [1, 2, 3]) + print(f"[3] Set dict entry: {dump_data(new)}") + + def main(): """ Test: corelibs.string_handling.string_helpers """ __mask() + __set_dict_value_entry() if __name__ == "__main__":