Skip to content

Commit

Permalink
feat: Vector indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 5dd38de commit 001cebc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class Vector(Generic[T]):
"""A vector of either quanity or numeric elements."""
"""An 1 dimensional array of either quantity or numeric elements."""

_elements: np.array

Expand All @@ -19,3 +19,7 @@ def __init__(self, elements: list | tuple | np.ndarray) -> None:
def __class_getitem__(cls, *_: object) -> type:
"""Return this class for type hinting."""
return cls

def __getitem__(self, index: int) -> T:
"""Return the element at a specific index."""
return self._elements[index]
5 changes: 5 additions & 0 deletions test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ def test_init_with_type_hint():
assert np.all(actual._elements == np.array([Length.zero(), Length.zero()]))


def test_indexing():
actual: Vector[Length] = Vector([Length(meters=1), Length(meters=2)])
assert actual[0] == Length(meters=1)


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

0 comments on commit 001cebc

Please sign in to comment.