Skip to content

Commit

Permalink
refactor: extract property creation to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 8, 2024
1 parent 05d9de9 commit 7fa0bd5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 141 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ ignore = [

"ISC001", # conflicts with ruff formatter

"N802", # interferes with setting constant abstract properties

"PGH003", # necessary for _QuantityBase.__eq__()

"RUF012", # does not work with constants

"TCH001", # adds hard to understand compexity without providing a benefit for smaller projects
"TCH002", # same as TCH001
"TCH003", # same as TCH001
Expand Down
13 changes: 10 additions & 3 deletions quantio/_quantity_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ class _QuantityBase(ABC):

@property
@abstractmethod
def unit_conversion(self) -> dict[str, float]:
"""Dict containing all units of the quantity."""
def _UNIT_CONVERSION(self) -> dict[str, float]:
"""Table used for recording the units with conversion values."""

def __init__(self, **kwargs: float) -> None:
"""Construct this class with the used units."""
self._base_value = kwargs.get("_base_value", 0.0)

for unit_name, factor in self.unit_conversion.items():
for unit_name, factor in self._UNIT_CONVERSION.items():
self._base_value += kwargs.get(unit_name, 0.0) * factor

for unit_name, factor in self._UNIT_CONVERSION.items():

def make_property(factor: float) -> property:
return property(lambda self: self._base_value / factor)

setattr(self.__class__, unit_name, make_property(factor))

def __eq__(self, other: object) -> bool:
"""Assess if this object is the same as another."""
if type(self) is not type(other):
Expand Down
66 changes: 2 additions & 64 deletions quantio/quantities.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from __future__ import annotations

from typing import ClassVar

from ._quantity_base import _QuantityBase


class Length(_QuantityBase):
"""The one-dimensional extent of an object or the distance between two points."""

unit_conversion: ClassVar[dict[str, float]] = {
_UNIT_CONVERSION: dict[str, float] = {
"miles": 1609.34,
"kilometers": 10**3,
"meters": 10**0,
Expand All @@ -19,73 +17,13 @@ class Length(_QuantityBase):
"micrometers": 10**-6,
}

@property
def miles(self) -> float:
"""The length in miles."""
return self._base_value / self.unit_conversion["miles"]

@property
def kilometers(self) -> float:
"""The length in kilometers."""
return self._base_value / self.unit_conversion["kilometers"]

@property
def meters(self) -> float:
"""The length in meters."""
return self._base_value / self.unit_conversion["meters"]

@property
def feet(self) -> float:
"""The length in feet."""
return self._base_value / self.unit_conversion["feet"]

@property
def inches(self) -> float:
"""The length in inches."""
return self._base_value / self.unit_conversion["inches"]

@property
def centimeters(self) -> float:
"""The length in centimeters."""
return self._base_value / self.unit_conversion["centimeters"]

@property
def millimeters(self) -> float:
"""The length in millimeters."""
return self._base_value / self.unit_conversion["millimeters"]

@property
def micrometers(self) -> float:
"""The length in micrometers."""
return self._base_value / self.unit_conversion["micrometers"]


class Time(_QuantityBase):
"""The duration of an event."""

unit_conversion: ClassVar[dict[str, float]] = {
_UNIT_CONVERSION: dict[str, float] = {
"hours": 60 * 60,
"minutes": 60,
"seconds": 1,
"milliseconds": 10**-3,
}

@property
def hours(self) -> float:
"""The time in hours."""
return self._base_value / self.unit_conversion["hours"]

@property
def minutes(self) -> float:
"""The time in minutes."""
return self._base_value / self.unit_conversion["minutes"]

@property
def seconds(self) -> float:
"""The time in seconds."""
return self._base_value / self.unit_conversion["seconds"]

@property
def milliseconds(self) -> float:
"""The time in milliseconds."""
return self._base_value / self.unit_conversion["milliseconds"]
47 changes: 0 additions & 47 deletions test/test_length.py

This file was deleted.

27 changes: 0 additions & 27 deletions test/test_time.py

This file was deleted.

0 comments on commit 7fa0bd5

Please sign in to comment.