Skip to content

Commit

Permalink
feat(xmllib): add english and german to boolean value converter (DEV-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann authored Nov 7, 2024
1 parent 7db2f94 commit 9b70810
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/dsp_tools/xmllib/models/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ def add_bool(
[See XML documentation for details](https://docs.dasch.swiss/latest/DSP-TOOLS/file-formats/xml-data-file/#boolean)
Conversions:
- "false", "0", "0.0", "no" -> "false"
- "true", "1", "1.0", "yes" -> "true"
- The conversion is case-insensitive, meaning that the words can also be capitalised.
- "false", "0", "0.0", "no", "non", "nein" -> "false"
- "true", "1", "1.0", "yes", "oui", "ja" -> "true"
Args:
prop_name: name of the property
Expand All @@ -162,8 +163,9 @@ def add_bool_optional(
[See XML documentation for details](https://docs.dasch.swiss/latest/DSP-TOOLS/file-formats/xml-data-file/#boolean)
Conversions:
- "false", "0", "0.0", "no" -> "false"
- "true", "1", "1.0", "yes" -> "true"
- The conversion is case-insensitive, meaning that the words can also be capitalised.
- "false", "0", "0.0", "no", "non", "nein" -> "false"
- "true", "1", "1.0", "yes", "oui", "ja" -> "true"
Args:
prop_name: name of the property
Expand Down
5 changes: 3 additions & 2 deletions src/dsp_tools/xmllib/value_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def is_nonempty_value(value: Any) -> bool:
def is_bool_like(value: Any) -> bool:
"""
Checks if a value is a bool or can be converted into a bool.
It is case-insensitive, meaning that the words can also be capitalised.
Args:
value: value to check
Expand All @@ -31,9 +32,9 @@ def is_bool_like(value: Any) -> bool:
True if it conforms
"""
value = str(value).lower().strip()
if value in ("false", "0", "0.0", "no"):
if value in ("false", "0", "0.0", "no", "non", "nein"):
return True
elif value in ("true", "1", "1.0", "yes"):
elif value in ("true", "1", "1.0", "yes", "oui", "ja"):
return True
return False

Expand Down
11 changes: 8 additions & 3 deletions src/dsp_tools/xmllib/value_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
def convert_to_bool_string(value: Any) -> str:
"""
Turns a value into a bool string, suitable for an XML.
It is case-insensitive, meaning that the words can also be capitalised.
Accepted values:
- "false", "0", "0.0", "no", "non", "nein" -> "false"
- "true", "1", "1.0", "yes", "oui", "ja" -> "true"
Args:
value: value to transform
Returns:
'true' or 'false' if it is a known value,
'true' or 'false' if it is an accepted value,
else it returns the original value as a string.
"""
str_val = str(value).lower().strip()
if str_val in ("false", "0", "0.0", "no"):
if str_val in ("false", "0", "0.0", "no", "non", "nein"):
return "false"
elif str_val in ("true", "1", "1.0", "yes"):
elif str_val in ("true", "1", "1.0", "yes", "oui", "ja"):
return "true"
return str(value)

Expand Down
4 changes: 3 additions & 1 deletion test/unittests/xmllib/test_value_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from dsp_tools.xmllib.value_checkers import is_timestamp


@pytest.mark.parametrize("val", ["false", "0", "0.0", "no", "true", "1", "1.0", "yes", False, True])
@pytest.mark.parametrize(
"val", ["false", "0", "0.0", "no", "true", "1", "1.0", "yes", False, True, "oui", "JA", "non", "nein"]
)
def test_is_bool_correct(val: Any) -> None:
assert is_bool_like(val)

Expand Down
4 changes: 2 additions & 2 deletions test/unittests/xmllib/test_value_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from dsp_tools.xmllib.value_converters import replace_newlines_with_tags


@pytest.mark.parametrize("val", ["false", "0 ", " 0.0", "NO", False])
@pytest.mark.parametrize("val", ["false", "0 ", " 0.0", "NO", False, "non", "nein"])
def test_convert_to_bool_false(val: Any) -> None:
assert convert_to_bool_string(val) == "false"


@pytest.mark.parametrize("val", ["TRUE ", " 1", "1.0", "Yes", True])
@pytest.mark.parametrize("val", ["TRUE ", " 1", "1.0", "Yes", True, "ouI", "JA"])
def test_convert_to_bool_true(val: Any) -> None:
assert convert_to_bool_string(val) == "true"

Expand Down

0 comments on commit 9b70810

Please sign in to comment.