From 001cebc2bcd814731c7a974385b01657b77cab2c Mon Sep 17 00:00:00 2001 From: unexcellent <> Date: Wed, 13 Nov 2024 16:54:46 +0100 Subject: [PATCH] feat: Vector indexing --- quantio/vector.py | 6 +++++- test/test_vector.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/quantio/vector.py b/quantio/vector.py index c76864b..fea8477 100644 --- a/quantio/vector.py +++ b/quantio/vector.py @@ -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 @@ -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] diff --git a/test/test_vector.py b/test/test_vector.py index 32fac0e..dc1624d 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -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"])