From e10e330e1505d1d072e89bb3d31a377e669fb411 Mon Sep 17 00:00:00 2001 From: Matt Royle <120763743+MattRoyle@users.noreply.github.com> Date: Wed, 19 Aug 2026 10:36:15 +0100 Subject: [PATCH 1/3] Refactor PhysicalValueToStringConverter to pass none and fuzzy into the float to string converter used by convert back --- nion/utils/Converter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nion/utils/Converter.py b/nion/utils/Converter.py index 1336d4f..7358e5d 100644 --- a/nion/utils/Converter.py +++ b/nion/utils/Converter.py @@ -135,12 +135,13 @@ def convert_back(self, formatted_value: typing.Optional[str]) -> typing.Optional class PhysicalValueToStringConverter(ConverterLike[float, str]): """ Convert between physical value represented by a float and a formatted string. """ - def __init__(self, units: str, multiplier: float=1.0, format:typing.Optional[str]=None, pass_none:bool=False, fuzzy:bool=True) -> None: + def __init__(self, units: str, multiplier: float = 1.0, format: typing.Optional[str] = None, pass_none: bool = False, fuzzy: bool = True) -> None: self.__units = units self.__multiplier = multiplier self.__format = format + " {:s}" if format else "{:g} {:s}" self.__pass_none = pass_none self.__fuzzy = fuzzy + self.__float_to_string_converter = FloatToStringConverter(pass_none=self.__pass_none, fuzzy=self.__fuzzy) def convert(self, value: typing.Optional[float]) -> typing.Optional[str]: """ Convert value to string using format string """ @@ -150,7 +151,7 @@ def convert(self, value: typing.Optional[float]) -> typing.Optional[str]: def convert_back(self, formatted_value: typing.Optional[str]) -> typing.Optional[float]: """ Convert string to value using standard float conversion """ - value = FloatToStringConverter().convert_back(formatted_value) + value = self.__float_to_string_converter.convert_back(formatted_value) return value / self.__multiplier if value is not None else None From 5c88572aa7df9a7b4cbf7df534ebb016bdce3fda Mon Sep 17 00:00:00 2001 From: Matt Royle <120763743+MattRoyle@users.noreply.github.com> Date: Mon, 24 Aug 2026 10:00:41 +0100 Subject: [PATCH 2/3] Remove the fuzzy parameter --- nion/utils/Converter.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nion/utils/Converter.py b/nion/utils/Converter.py index 7358e5d..3cbe2b5 100644 --- a/nion/utils/Converter.py +++ b/nion/utils/Converter.py @@ -135,13 +135,12 @@ def convert_back(self, formatted_value: typing.Optional[str]) -> typing.Optional class PhysicalValueToStringConverter(ConverterLike[float, str]): """ Convert between physical value represented by a float and a formatted string. """ - def __init__(self, units: str, multiplier: float = 1.0, format: typing.Optional[str] = None, pass_none: bool = False, fuzzy: bool = True) -> None: + def __init__(self, units: str, multiplier: float = 1.0, format: typing.Optional[str] = None, pass_none: bool = False) -> None: self.__units = units self.__multiplier = multiplier self.__format = format + " {:s}" if format else "{:g} {:s}" self.__pass_none = pass_none - self.__fuzzy = fuzzy - self.__float_to_string_converter = FloatToStringConverter(pass_none=self.__pass_none, fuzzy=self.__fuzzy) + self.__float_to_string_converter = FloatToStringConverter(pass_none=self.__pass_none) def convert(self, value: typing.Optional[float]) -> typing.Optional[str]: """ Convert value to string using format string """ From 54ec554983ad9a82dbb7dc3662eb6d3893efab3e Mon Sep 17 00:00:00 2001 From: Matt Royle <120763743+MattRoyle@users.noreply.github.com> Date: Tue, 25 Aug 2026 10:06:22 +0100 Subject: [PATCH 3/3] Add tests for the physical value converter --- nion/utils/test/Converter_test.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/nion/utils/test/Converter_test.py b/nion/utils/test/Converter_test.py index 25f4410..b3205ab 100644 --- a/nion/utils/test/Converter_test.py +++ b/nion/utils/test/Converter_test.py @@ -56,6 +56,26 @@ def test_date_to_string_converter(self) -> None: self.assertEqual(dt.replace(microsecond=0), converter.convert_back(converter.convert(dt.replace(microsecond=0)))) self.assertEqual(dt.strftime(format), converter.convert(converter.convert_back(dt.strftime(format)))) + def test_physical_value_to_str_converter_convert(self) -> None: + converter = Converter.PhysicalValueToStringConverter(units="m") + value = converter.convert(1.23) + self.assertEqual(value, "1.23 m") + + def test_physical_value_to_str_converter_convert_back(self) -> None: + converter = Converter.PhysicalValueToStringConverter(units="m") + value = converter.convert_back("2.71 m") + self.assertEqual(value, 2.71) + + def test_physical_value_to_str_converter_convert_back_with_none(self) -> None: + converter = Converter.PhysicalValueToStringConverter(units="m") + value = converter.convert_back(None) + self.assertEqual(value, 0.0) + + def test_physical_value_to_str_converter_convert_back_pass_none(self) -> None: + converter = Converter.PhysicalValueToStringConverter(units="m", pass_none=True) + value = converter.convert_back(None) + self.assertEqual(value, None) + if __name__ == '__main__': logging.getLogger().setLevel(logging.DEBUG)