From 00b38733b1bbe88a72d4d301655c7e3b5b0ac3f5 Mon Sep 17 00:00:00 2001 From: Andreas Wicenec Date: Fri, 22 Mar 2024 09:48:10 +0800 Subject: [PATCH] Added dict functions --- daliuge-engine/dlg/apps/simple_functions.py | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/daliuge-engine/dlg/apps/simple_functions.py b/daliuge-engine/dlg/apps/simple_functions.py index 7610acc83..ba0de9eed 100644 --- a/daliuge-engine/dlg/apps/simple_functions.py +++ b/daliuge-engine/dlg/apps/simple_functions.py @@ -77,3 +77,36 @@ def string2json(string: str, pickle_flag: bool = False) -> list: return json.loads(string) else: return pickle.dumps(json.loads(string)) + + +def value_from_dict(dict_in: dict, dict_key: str) -> Any: + """ + Select a value from a dictionary. + + Parameters: + ----------- + dict_in: input dictionary + dict_key: keyword to select + + Returns: + -------- + The value referred to by key. + """ + # We deliberatly let this fail if the type is not str or array like + dict_key = dict_key if isinstance(dict_key, str) else dict_key[0] + return dict_in[dict_key] + + +def keys_from_dict(dict_in: dict) -> list: + """ + Return key list of dictionary. + + Parameters: + ----------- + dict_in: input dictionary + + Returns: + -------- + The list of keys + """ + return list(dict_in.keys())