diff --git a/generate/generate_boilerplate.py b/generate/generate_boilerplate.py index fbd2167..2e99c05 100644 --- a/generate/generate_boilerplate.py +++ b/generate/generate_boilerplate.py @@ -28,6 +28,7 @@ def generate_boilerplate(quantities_with_fields: dict[str, dict[str, str]]) -> N target_content_with_boilerplate.extend( _generate_init(quantities_with_fields[current_class]) ) + target_content_with_boilerplate.extend(_generat_zero_function(current_class)) if "# --- End of auto generated part. ---" in line: current_line_is_part_of_autogenerated_code = False @@ -45,6 +46,19 @@ def generate_boilerplate(quantities_with_fields: dict[str, dict[str, str]]) -> N target_file.writelines(target_content_with_boilerplate) +def _generate_properties(current_class: str, units: dict[str, str]) -> list[str]: + code = [] + + for unit, factor in units.items(): + code.append(" " * 4 + "@property") + code.append(" " * 4 + f"def {unit}(self) -> float:") + code.append(" " * 8 + f'"""The {current_class.lower()} in {unit.replace("_", " ")}."""') + code.append(" " * 8 + f"return self._base_value / {factor}") + code.append("") + + return code + + def _generate_init(units: dict[str, str]) -> list[str]: code = [" " * 4 + "def __init__(", " " * 8 + "self,"] @@ -61,17 +75,15 @@ def _generate_init(units: dict[str, str]) -> list[str]: return code -def _generate_properties(current_class: str, units: dict[str, str]) -> list[str]: - code = [] - - for unit, factor in units.items(): - code.append(" " * 4 + "@property") - code.append(" " * 4 + f"def {unit}(self) -> float:") - code.append(" " * 8 + f'"""The {current_class.lower()} in {unit.replace("_", " ")}."""') - code.append(" " * 8 + f"return self._base_value / {factor}") - code.append("") - - return code +def _generat_zero_function(current_class: str) -> list[str]: + return [ + "", + " " * 4 + "@classmethod", + " " * 4 + f"def zero(cls) -> {current_class}:", + " " * 8 + f'"""Create a {current_class} with a value of zero."""', + " " * 8 + f"return {current_class}()", + "", + ] if __name__ == "__main__": diff --git a/quantio/quantities.py b/quantio/quantities.py index e9d9f26..8f71db2 100644 --- a/quantio/quantities.py +++ b/quantio/quantities.py @@ -27,6 +27,11 @@ def __init__( self._base_value += meters_per_square_second * 1 self._base_value += g_force * (1 / 9.8) + @classmethod + def zero(cls) -> Acceleration: + """Create a Acceleration with a value of zero.""" + return Acceleration() + # --- End of auto generated part. --- @@ -54,6 +59,11 @@ def __init__( self._base_value += degrees * (3.141592653589793 / 180) self._base_value += radians * 1 + @classmethod + def zero(cls) -> Angle: + """Create a Angle with a value of zero.""" + return Angle() + # --- End of auto generated part. --- @@ -123,6 +133,11 @@ def __init__( self._base_value += square_millimeters * 10 ** (-3 * 2) self._base_value += square_micrometers * 10 ** (-6 * 2) + @classmethod + def zero(cls) -> Area: + """Create a Area with a value of zero.""" + return Area() + # --- End of auto generated part. --- @@ -192,6 +207,11 @@ def __init__( self._base_value += millimeters * 10**-3 self._base_value += micrometers * 10**-6 + @classmethod + def zero(cls) -> Length: + """Create a Length with a value of zero.""" + return Length() + # --- End of auto generated part. --- @@ -254,6 +274,11 @@ def __init__( self._base_value += milligrams * 10**-6 self._base_value += micrograms * 10**-9 + @classmethod + def zero(cls) -> Mass: + """Create a Mass with a value of zero.""" + return Mass() + # --- End of auto generated part. --- @@ -288,6 +313,11 @@ def __init__( self._base_value += kilometers_per_hour * (1 / 3.6) self._base_value += miles_per_hour * (1 / 2.23694) + @classmethod + def zero(cls) -> Velocity: + """Create a Velocity with a value of zero.""" + return Velocity() + # --- End of auto generated part. --- @@ -329,4 +359,9 @@ def __init__( self._base_value += seconds * 1 self._base_value += milliseconds * 10**-3 + @classmethod + def zero(cls) -> Time: + """Create a Time with a value of zero.""" + return Time() + # --- End of auto generated part. --- diff --git a/test/test_zero.py b/test/test_zero.py new file mode 100644 index 0000000..31d5cfc --- /dev/null +++ b/test/test_zero.py @@ -0,0 +1,12 @@ +import pytest + +from quantio import Length + + +def test_zero(): + actual = Length.zero() + assert actual == Length(meters=0) + + +if __name__ == "__main__": + pytest.main([__file__, "-v"])