From b836822003bed854a51b312e86b560e2cb5475e1 Mon Sep 17 00:00:00 2001 From: tristan-ranff Date: Wed, 15 Mar 2023 14:33:07 +0100 Subject: [PATCH 1/3] fix replacement for keys which are tuples or lists --- uparma/uparma.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/uparma/uparma.py b/uparma/uparma.py index 927ad7d..adb0030 100755 --- a/uparma/uparma.py +++ b/uparma/uparma.py @@ -141,12 +141,10 @@ def _parse_jsons(self): # _id = uparma_entry["_id"] self.parameters[_id] = uparma_entry for key, value in uparma_entry["key_translations"].items(): - if isinstance(value, list): value = tuple(value) if key in self.parameter2id.keys(): - # found key does value also exist if value in self.parameter2id[key]: # parameter already found @@ -321,8 +319,14 @@ def translate(self, param_dict, original_style=None, translated_style=None): if _uparma_v == _uparma_vt: translated_value = _transtyle_v was_translated = True - if translated_key.endswith(""): - translated_key = None + if isinstance(translated_key, tuple) is True: + translated_key = [ + None if key.endswith("DROP_KEY") else key + for key in translated_key + ] + else: + if translated_key.endswith(""): + translated_key = None template_dict.update( { "translated_key": translated_key, @@ -402,6 +406,14 @@ def get_default_params(self, style=None): translated_default = value["default_value"] else: translated_default = value["default_value"] + if isinstance(translated_key, list) is True: + translated_key = [ + None if key.endswith("DROP_KEY") else key + for key in translated_key + ] + else: + if translated_key.endswith(""): + translated_key = None params[name] = { "original_key": value["name"], "original_value": value["default_value"], From 83d2c7775f8539614542f98603d2aa117ceb038a Mon Sep 17 00:00:00 2001 From: tristan-ranff Date: Wed, 15 Mar 2023 14:51:10 +0100 Subject: [PATCH 2/3] add missing delimiters --- uparma/uparma.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uparma/uparma.py b/uparma/uparma.py index adb0030..e4cbb2d 100755 --- a/uparma/uparma.py +++ b/uparma/uparma.py @@ -321,7 +321,7 @@ def translate(self, param_dict, original_style=None, translated_style=None): was_translated = True if isinstance(translated_key, tuple) is True: translated_key = [ - None if key.endswith("DROP_KEY") else key + None if key.endswith("") else key for key in translated_key ] else: @@ -408,7 +408,7 @@ def get_default_params(self, style=None): translated_default = value["default_value"] if isinstance(translated_key, list) is True: translated_key = [ - None if key.endswith("DROP_KEY") else key + None if key.endswith("") else key for key in translated_key ] else: From bf4f5d34582c926b27486429873ac76d4cc54ac3 Mon Sep 17 00:00:00 2001 From: tristan-ranff Date: Wed, 15 Mar 2023 15:07:02 +0100 Subject: [PATCH 3/3] add drop key tests --- tests/test_drop_key.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/test_drop_key.py diff --git a/tests/test_drop_key.py b/tests/test_drop_key.py new file mode 100644 index 0000000..9d79a89 --- /dev/null +++ b/tests/test_drop_key.py @@ -0,0 +1,73 @@ +import uparma +import pytest + + +test_data_list = [ + { + "input": { + ("general", "parameters"): [ + { + "name": "to_key_or_not_to_key", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key", + "some_other_style": "to_key_or_not_to_key_", + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + { + "name": "to_key_or_not_to_key_list", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key_list", + "some_other_style": [ + "keyp_no_pun_intended", + "to_key_or_not_to_key_", + ], + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + { + "name": "to_key_or_not_to_key_rip_delim", + "key_translations": { + "ursgal_style_1": "to_key_or_not_to_key_rip_delim", + "some_other_style": "to_key_or_not_to_key_DROP_KEY", + }, + "value_translations": { + "ursgal_style_1": [[True, True], [False, False]], + "some_other_style": [[True, "i_lived"], [False, "i_died"]], + }, + }, + ] + }, + } +] + + +@pytest.mark.parametrize("test_dict", test_data_list) +def test_load_data(test_dict): + up = uparma.UParma(refresh_jsons=False, parameter_data=test_dict["input"]) + translation = up.convert( + { + "to_key_or_not_to_key": True, + "to_key_or_not_to_key_list": False, + "to_key_or_not_to_key_rip_delim": False, + }, + "some_other_style", + ) + assert translation["to_key_or_not_to_key"]["translated_key"] is None + assert translation["to_key_or_not_to_key"]["translated_value"] == "i_lived" + assert translation["to_key_or_not_to_key_list"]["translated_key"] == [ + "keyp_no_pun_intended", + None, + ] + assert translation["to_key_or_not_to_key_list"]["translated_value"] == "i_died" + assert ( + translation["to_key_or_not_to_key_rip_delim"]["translated_key"] + == "to_key_or_not_to_key_DROP_KEY" + ) + assert translation["to_key_or_not_to_key_rip_delim"]["translated_value"] == "i_died"