Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hypothesis: Initial support #62

Merged
merged 8 commits into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.cache/
/*.egg-info/
__pycache__/
.hypothesis/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ matrix:
allow_failures:
- python: "nightly"

cache:
directories:
- $HOME/.cache/pip
- .hypothesis

before_install:
- if [[ $MINICONDA_OS ]]; then
URL="https://repo.continuum.io/miniconda/Miniconda3-latest-${MINICONDA_OS}-x86_64.sh";
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest~=3.8
hypothesis
30 changes: 26 additions & 4 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from math import acos, cos, degrees, hypot, radians, sin
from numbers import Number
from numbers import Real
from collections.abc import Sequence


class Vector2(Sequence):

def __init__(self, x, y):
def __init__(self, x: Real, y: Real):
self.x = x
self.y = y
self.length = hypot(x, y)
Expand Down Expand Up @@ -37,13 +37,13 @@ def __sub__(self, other):
def __mul__(self, other):
if isinstance(other, Vector2):
return self.x * other.x + self.y * other.y
elif isinstance(other, Number):
elif isinstance(other, Real):
return Vector2(self.x * other, self.y * other)
else:
return NotImplemented

def __rmul__(self, other):
if isinstance(other, Number):
if isinstance(other, Real):
return Vector2(self.x * other, self.y * other)

def __xor__(self, other):
Expand Down Expand Up @@ -94,6 +94,28 @@ def __neg__(self):
def angle(self, other):
return degrees(acos(self.normalize() * other.normalize()))

def isclose(self, other: 'Vector2', *, rel_tol: float=1e-06, abs_tol: float=1e-3):
"""
Determine whether two vectors are close in value.

rel_tol
maximum difference for being considered "close", relative to the
magnitude of the input values
abs_tol
maximum difference for being considered "close", regardless of the
magnitude of the input values

Return True if self is close in value to other, and False otherwise.

For the values to be considered close, the difference between them
must be smaller than at least one of the tolerances.
"""
diff = (self - other).length
return (
diff < rel_tol * max(self.length, other.length) or
diff < abs_tol
)

def rotate(self, degrees):
r = radians(degrees)
r_cos = cos(r)
Expand Down
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from ppb_vector import Vector2
import hypothesis.strategies as st

vectors = lambda: st.builds(
Vector2,
st.floats(min_value=-1e300, max_value=1e300),
st.floats(min_value=-1e300, max_value=1e300)
)

@st.composite
def units(draw, elements=st.floats(min_value=0, max_value=360)):
angle = draw(elements)
return Vector2(1, 0).rotate(angle)


def angle_isclose(x, y, epsilon = 6.5e-5):
d = (x - y) % 360
return (d < epsilon) or (d > 360 - epsilon)