Skip to content

Commit

Permalink
feat: implement zero constructor for all quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent b7d3e88 commit 6368a67
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
34 changes: 23 additions & 11 deletions generate/generate_boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,"]

Expand All @@ -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__":
Expand Down
35 changes: 35 additions & 0 deletions quantio/quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---


Expand Down Expand Up @@ -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. ---
12 changes: 12 additions & 0 deletions test/test_zero.py
Original file line number Diff line number Diff line change
@@ -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"])

0 comments on commit 6368a67

Please sign in to comment.