Skip to content

Commit

Permalink
math.vector3 has 100% test coverage. Remove __cmp__ method (not used)…
Browse files Browse the repository at this point in the history
…. Improve docs.
  • Loading branch information
ynikitenko committed Nov 7, 2021
1 parent ea5af93 commit 4c95719
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lena/math/vector3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
if that makes sense for the operations used
(types are not tested during the initialization).
For example, vectors in the examples above have integer coordinates,
which can change if we multiply them by floats
or rotate. For usual vector additions, though, their coordinates
remain integer.
which will become floats if we multiply them by floats,
divide or maybe rotate.
For usual vector additions or subtractions, though,
their coordinates will remain integer.
"""

from math import sin, cos, acos, pi, atan2, sqrt
Expand Down Expand Up @@ -311,6 +312,8 @@ def _mag2(self):
"""
return self.dot(self)

# todo: probably make r2 an attribute (like rho2)
# and why do we have a separate method _mag2?
def getr2(self):
return self._mag2()

Expand Down Expand Up @@ -346,12 +349,13 @@ def __ne__(self, B):

## Prohibit comparison methods <, <=, >, >=, __cmp__

def __cmp__(self, B):
# works fine for Python 2, but __lt__, etc.
# should be also implemented for Python 3.
raise lena.core.LenaTypeError(
"comparison is not supported for vector3"
)
# this method is never called. Probably remove.
# def __cmp__(self, B):
# # works fine for Python 2, but __lt__, etc.
# # should be also implemented for Python 3.
# raise lena.core.LenaTypeError(
# "comparison is not supported for vector3"
# )

def __lt__(self, B):
raise lena.core.LenaTypeError(
Expand All @@ -378,8 +382,8 @@ def __ge__(self, B):
def __getitem__(self, i):
return self._v[i]

def __setitem__(self, key, value):
self._v[key] = float(value)
def __setitem__(self, ind, value):
self._v[ind] = float(value)

def __iter__(self):
return iter(self._v)
Expand Down Expand Up @@ -412,6 +416,8 @@ def isclose(self, B, rel_tol=1e-09, abs_tol=0.0):
# http://blog.teamtreehouse.com/operator-overloading-python

def __nonzero__(self):
# implement truth value testing and bool()
# not used in Python 3 tests
return self._mag2() != 0

## Regular Binary Operations
Expand All @@ -433,6 +439,10 @@ def __div__(self, c):
>>> v1 / 2
vector3(0.0, 0.5, 1.0)
"""
# in Python 3 4/2 = 2.,
# while in Python 2 4/2 is integer 2!
# so there is no need to care about preserving integers:
# they will be lost in general.
return 1./c * self

def __truediv__(self, c):
Expand Down
41 changes: 41 additions & 0 deletions tests/math/test_vector3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import lena.core
from lena.math import vector3
from lena.core.exceptions import LenaTypeError

Expand Down Expand Up @@ -36,6 +37,46 @@ def test_operations():
v1 /= 2
assert v1 == vector3(3, 4, 5)

# after division vector representation will be with floats
assert str(v1) == "vector3(3.0, 4.0, 5.0)"

# test all assignments / getters
v2 = vector3(0, 0, 0)
v2.x = 1
v2.y = 2
v2.z = 3
assert v2 == vector3(1, 2, 3)

# r2 getter works
assert v2.getr2() == 14

# != works
assert v1 != v2

# != with a wrong type raises
with pytest.raises(lena.core.LenaTypeError):
v1 != 0

# comparison with a wrong type raises
with pytest.raises(lena.core.LenaTypeError):
v1 < 0

# iter works
assert list(coord for coord in v2) == [1, 2, 3]

# container length is 3
assert len(v1) == 3

# we also have this method!
v2[2] = 4
assert v2 == vector3(1, 2, 4)

# we can add a list to vector3! From the right side.
assert [1.1, 2.2, 3.3] + v2 == vector3(2.1, 4.2, 7.3)

# non-zero works
assert bool(v2)


def test_cylindrical():
v1 = vector3(3, 4, 5)
Expand Down

0 comments on commit 4c95719

Please sign in to comment.