Пример #1
0
 def parse_physical_size(physical_size: Optional[int],
                         unit: Optional[str]) -> Optional[Quantity]:
     supported_units = ("meters", "inches", "cm")
     if physical_size is not None and parse_float(physical_size) is not None \
             and unit in supported_units:
         return parse_float(physical_size) * UNIT_REGISTRY(unit)
     return None
Пример #2
0
 def parse_physical_size(physical_size: Optional[str], unit: Optional[str],
                         inverse: bool) -> Optional[Quantity]:
     supported_units = {1: "meter", 2: "inch"}
     if type(unit) == str:
         supported_units = {"meters": "meter", "inches": "inch"}
     if physical_size is not None and parse_float(physical_size) is not None \
             and unit in supported_units.keys():
         physical_size = parse_float(physical_size)
         if inverse:
             physical_size = 1 / physical_size
         return physical_size * UNIT_REGISTRY(supported_units[unit])
     return None
Пример #3
0
def test_float_parser():
    assert parse_float(2.3) == 2.3
    assert parse_float("2.3") == 2.3
    assert parse_float("2,3") == 2.3

    assert parse_float("foo", False) is None
    with pytest.raises(ValueError):
        parse_float("foo", True)
Пример #4
0
 def parse_physical_size(physical_size: str) -> Optional[Quantity]:
     if physical_size is not None and parse_float(
             physical_size) is not None:
         return parse_float(physical_size) * UNIT_REGISTRY("millimeter")
     return None
Пример #5
0
 def parse_physical_size(
         physical_size: Optional[str]) -> Optional[Quantity]:
     if physical_size is not None and parse_float(physical_size) not in (
             None, 0.0):
         return 1 / parse_float(physical_size) * UNIT_REGISTRY("meters")
     return None