Skip to content

Commit

Permalink
feat: Vector.arange()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 19, 2024
1 parent f7c6a40 commit 8f659c6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
hooks:
- id: ruff-format
name: Run Formatter
- id: ruff
name: Run Linter
args: [ --fix ]
- id: ruff-format
name: Run Formatter

- repo: local
hooks:
Expand Down
4 changes: 2 additions & 2 deletions generate/generate_boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def _generate_properties(current_class: str, units: dict[str, str]) -> list[str]


def _generate_init(units: dict[str, str]) -> list[str]:
code = [" " * 4 + "def __init__(", " " * 8 + "self,"]
code = [" " * 4 + "def __init__(", " " * 8 + "self,", " " * 8 + "_base_value: float = 0.0,"]

for unit in units:
code.append(" " * 8 + f"{unit}: float = 0.0,")

code.append(" " * 4 + ") -> None:")
code.append(" " * 8 + "self._base_value = 0.0")
code.append(" " * 8 + "self._base_value = _base_value")

for unit, factor in units.items():
code.append(" " * 8 + f"self._base_value += {unit} * {factor}")
Expand Down
21 changes: 14 additions & 7 deletions quantio/quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ def g_force(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
meters_per_square_second: float = 0.0,
g_force: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += meters_per_square_second * 1
self._base_value += g_force * (1 / 9.8)

Expand Down Expand Up @@ -56,10 +57,11 @@ def radians(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
degrees: float = 0.0,
radians: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += degrees * (3.141592653589793 / 180)
self._base_value += radians * 1

Expand Down Expand Up @@ -120,6 +122,7 @@ def square_micrometers(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
square_miles: float = 0.0,
square_kilometers: float = 0.0,
square_meters: float = 0.0,
Expand All @@ -129,7 +132,7 @@ def __init__(
square_millimeters: float = 0.0,
square_micrometers: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += square_miles * 1609.34**2
self._base_value += square_kilometers * 10 ** (3 * 2)
self._base_value += square_meters * 1
Expand Down Expand Up @@ -196,6 +199,7 @@ def micrometers(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
miles: float = 0.0,
kilometers: float = 0.0,
meters: float = 0.0,
Expand All @@ -205,7 +209,7 @@ def __init__(
millimeters: float = 0.0,
micrometers: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += miles * 1609.34
self._base_value += kilometers * 10**3
self._base_value += meters * 1
Expand Down Expand Up @@ -267,6 +271,7 @@ def micrograms(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
tonnes: float = 0.0,
kilograms: float = 0.0,
pounds: float = 0.0,
Expand All @@ -275,7 +280,7 @@ def __init__(
milligrams: float = 0.0,
micrograms: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += tonnes * 10**3
self._base_value += kilograms * 1
self._base_value += pounds * (1 / 2.20462)
Expand Down Expand Up @@ -316,11 +321,12 @@ def miles_per_hour(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
meters_per_second: float = 0.0,
kilometers_per_hour: float = 0.0,
miles_per_hour: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += meters_per_second * 1
self._base_value += kilometers_per_hour * (1 / 3.6)
self._base_value += miles_per_hour * (1 / 2.23694)
Expand Down Expand Up @@ -362,12 +368,13 @@ def milliseconds(self) -> float:

def __init__(
self,
_base_value: float = 0.0,
hours: float = 0.0,
minutes: float = 0.0,
seconds: float = 0.0,
milliseconds: float = 0.0,
) -> None:
self._base_value = 0.0
self._base_value = _base_value
self._base_value += hours * 60 * 60
self._base_value += minutes * 60
self._base_value += seconds * 1
Expand Down
21 changes: 21 additions & 0 deletions quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ class Vector(Generic[T]):
def __init__(self, elements: list | tuple | np.ndarray) -> None:
self._elements = np.array(elements)

@classmethod
def arange(cls, start: T, stop: T, step: T) -> Vector[T]:
"""Return evenly spaced values within a given interval."""
if isinstance(start, _QuantityBase):
start_val = start._base_value

if not isinstance(stop, _QuantityBase):
raise TypeError
stop_val = stop._base_value

if not isinstance(step, _QuantityBase):
raise TypeError
step_val = step._base_value

element_type = type(start)
return Vector(
[element_type(value) for value in np.arange(start_val, stop_val, step_val)]
)

return Vector(np.arange(start, stop, step))

def to_numpy(self, unit: str | None = None) -> np.ndarray[float]:
"""Convert this vector into a numpy array of floats."""
if isinstance(self._elements[0], _QuantityBase):
Expand Down
18 changes: 18 additions & 0 deletions test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from quantio import (
Vector,
Length,
Time,
CanNotAddTypesError,
CanNotSubtractTypesError,
NoUnitSpecifiedError,
Expand Down Expand Up @@ -182,5 +183,22 @@ def test_to_numpy__quantity_no_unit():
vec.to_numpy()


def test_arange__float():
actual = Vector[float].arange(start=0.0, stop=5.0, step=2)
assert actual == Vector([0, 2, 4])


def test_arange__quantity():
actual = Vector[Length].arange(
start=Length(meters=0), stop=Length(meters=5), step=Length(meters=2)
)
assert actual == Vector([Length(meters=0), Length(meters=2), Length(meters=4)])


def test_arange__false_type_combination():
with pytest.raises(TypeError):
Vector.arange(start=Length(meters=0), stop=Time(meters=5), step=Length(meters=2))


if __name__ == "__main__":
pytest.main([__file__, "-v"])

0 comments on commit 8f659c6

Please sign in to comment.