Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nion/utils/Converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +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)

def convert(self, value: typing.Optional[float]) -> typing.Optional[str]:
""" Convert value to string using format string """
Expand All @@ -150,7 +150,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


Expand Down
20 changes: 20 additions & 0 deletions nion/utils/test/Converter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading