Skip to content

Commit

Permalink
feat: Vector item assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 13, 2024
1 parent 001cebc commit 0721e9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions quantio/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ def __class_getitem__(cls, *_: object) -> type:
def __getitem__(self, index: int) -> T:
"""Return the element at a specific index."""
return self._elements[index]

def __setitem__(self, index: int, value: T) -> None:
"""Set the element at a specific index."""
self._elements[index] = value
19 changes: 13 additions & 6 deletions test/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@


def test_init():
actual: Vector[Length] = Vector([Length.zero(), Length.zero()])
assert np.all(actual._elements == np.array([Length.zero(), Length.zero()]))
vec: Vector[Length] = Vector([Length.zero(), Length.zero()])
assert np.all(vec._elements == np.array([Length.zero(), Length.zero()]))


def test_init_with_type_hint():
actual = Vector[Length]([Length.zero(), Length.zero()])
assert np.all(actual._elements == np.array([Length.zero(), Length.zero()]))
vec = Vector[Length]([Length.zero(), Length.zero()])
assert np.all(vec._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)
vec: Vector[Length] = Vector([Length(meters=1), Length(meters=2)])
assert vec[0] == Length(meters=1)


def test_set_item():
vec: Vector[Length] = Vector([Length(meters=1), Length(meters=2)])
vec[0] = Length(meters=3)

assert vec[0] == Length(meters=3)


if __name__ == "__main__":
Expand Down

0 comments on commit 0721e9b

Please sign in to comment.