Skip to content

Commit

Permalink
Merge pull request #49 from uparma/feature/drop_non_keys
Browse files Browse the repository at this point in the history
fix replacement for keys which are tuples or lists
  • Loading branch information
fu authored Mar 15, 2023
2 parents c56d1f2 + bf4f5d3 commit bea452a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
73 changes: 73 additions & 0 deletions tests/test_drop_key.py
Original file line number Diff line number Diff line change
@@ -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_<DROP_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_<DROP_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"
20 changes: 16 additions & 4 deletions uparma/uparma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("<DROP_KEY>"):
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("<DROP_KEY>"):
translated_key = None
template_dict.update(
{
"translated_key": translated_key,
Expand Down Expand Up @@ -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("<DROP_KEY>"):
translated_key = None
params[name] = {
"original_key": value["name"],
"original_value": value["default_value"],
Expand Down

0 comments on commit bea452a

Please sign in to comment.