Skip to content

Commit

Permalink
Rename Point operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard de Ruijter committed Jan 8, 2018
1 parent 7308287 commit 070bb36
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 23 deletions.
84 changes: 66 additions & 18 deletions source/locationHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ def __sub__(self,other):
def __rsub__(self,other):
return self.__sub__(other)

def __lt__(self,other):
def yWiseLessThan(self,other):
"""
Returns whether self is less than other.
This first compares y, then x coordinates.
Returns whether self is less than other, first comparing y, then x coordinates.
For example: (x=4,y=3) < (x=3,y=4) because self.y is less than other.y.
To compare in opposite order (i.e. compare x, then y), use tuple(self) < tuple(other)
To compare in opposite order (i.e. compare x, then y), use L{xWiseLessThan}
"""
if not isinstance(other,POINT_CLASSES):
try:
Expand All @@ -50,12 +49,24 @@ def __lt__(self,other):
return False
return (self.y, self.x) < (other.y, other.x)

def __le__(self,other):
def xWiseLessThan(self,other):
"""
Returns whether self is less than or equal to other.
This first compares y, then x coordinates.
For example: (x=4,y=3) < (x=3,y=4) because self.y is less than other.y.
To compare in opposite order (i.e. compare x, then y), use tuple(self) <= tuple(other)
Returns whether self is less than other, first comparing x, then y coordinates.
For example: (x=3,y=4) < (x=4,y=3) because self.x is less than other.x.
To compare in opposite order (i.e. compare y, then x), use L{yWiseLessThan}
"""
if not isinstance(other,POINT_CLASSES):
try:
other=toPoint(other)
except ValueError:
return False
return (self.x, self.y) < (other.x, other.y)

def yWiseLessOrEq(self,other):
"""
Returns whether self is less than or equal to other, first comparing y, then x coordinates.
For example: (x=4,y=3) <= (x=3,y=4) because self.y is less than or equal to other.y.
To compare in opposite order (i.e. compare x, then y), use L{xWiseLessOrEq}
"""
if not isinstance(other,POINT_CLASSES):
try:
Expand All @@ -64,12 +75,24 @@ def __le__(self,other):
return False
return (self.y, self.x) <= (other.y, other.x)

def __gt__(self,other):
def xWiseLessOrEq(self,other):
"""
Returns whether self is less than or equal to other, first comparing x, then y coordinates.
For example: (x=3,y=4) <= (x=4,y=3) because self.x is less than or equal to other.x.
To compare in opposite order (i.e. compare y, then x), use L{yWiseLessOrEq}
"""
if not isinstance(other,POINT_CLASSES):
try:
other=toPoint(other)
except ValueError:
return False
return (self.x, self.y) <= (other.x, other.y)

def yWiseGreaterThan(self,other):
"""
Returns whether self is greater than other.
This first compares y, then x coordinates.
Returns whether self is greater than other, first comparing y, then x coordinates.
For example: (x=3,y=4) > (x=4,y=3) because self.y is greater than other.y.
To compare in opposite order (i.e. compare x, then y), use tuple(self) > tuple(other)
To compare in opposite order (i.e. compare x, then y), use L{xWiseGreaterThan}
"""
if not isinstance(other,POINT_CLASSES):
try:
Expand All @@ -78,12 +101,24 @@ def __gt__(self,other):
return False
return (self.y, self.x) > (other.y, other.x)

def __ge__(self,other):
def xWiseGreaterThan(self,other):
"""
Returns whether self is greater than or equal to other.
This first compares y, then x coordinates.
For example: (x=3,y=4) > (x=4,y=3) because self.y is greater than other.y.
To compare in opposite order (i.e. compare x, then y), use tuple(self) >= tuple(other)
Returns whether self is greater than other, first comparing x, then y coordinates.
For example: (x=4,y=3) > (x=3,y=4) because self.x is greater than other.x.
To compare in opposite order (i.e. compare y, then x), use L{yWiseGreaterThan}
"""
if not isinstance(other,POINT_CLASSES):
try:
other=toPoint(other)
except ValueError:
return False
return (self.x, self.y) > (other.x, other.y)

def yWiseGreaterOrEq(self,other):
"""
Returns whether self is greater than or equal to other, first comparing y, then x coordinates.
For example: (x=3,y=4) >= (x=4,y=3) because self.y is greater than or equal to other.y.
To compare in opposite order (i.e. compare x, then y), use L{xWiseGreaterOrEq}
"""
if not isinstance(other,POINT_CLASSES):
try:
Expand All @@ -92,6 +127,19 @@ def __ge__(self,other):
return False
return (self.y, self.x) >= (other.y, other.x)

def xWiseGreaterOrEq(self,other):
"""
Returns whether self is greater than or equal to other, first comparing x, then y coordinates.
For example: (x=4,y=3) >= (x=3,y=4) because self.x is greater than or equal to other.x.
To compare in opposite order (i.e. compare y, then x), use L{yWiseGreaterOrEq}
"""
if not isinstance(other,POINT_CLASSES):
try:
other=toPoint(other)
except ValueError:
return False
return (self.x, self.y) >= (other.x, other.y)

def __eq__(self,other):
if not isinstance(other,POINT_CLASSES):
try:
Expand Down
16 changes: 11 additions & 5 deletions tests/unit/test_locationHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ def test_sum(self):
def test_sub(self):
self.assertEqual(Point(x=2,y=4)-Point(x=4,y=8),Point(x=-2,y=-4))

def test_gt(self):
self.assertGreater(Point(x=3,y=4), Point(x=4,y=3))

def test_lt(self):
self.assertLess(Point(x=4,y=3), Point(x=3,y=4))
def test_greaterThan(self):
self.assertTrue(Point(x=3,y=4).yWiseGreaterThan(Point(x=4,y=3)))
self.assertTrue(Point(x=4,y=3).xWiseGreaterThan(Point(x=3,y=4)))
self.assertTrue(Point(x=3,y=4).yWiseGreaterOrEq(Point(x=4,y=3)))

This comment has been minimized.

Copy link
@feerrenrut

feerrenrut Jan 9, 2018

Contributor

Could you also test:

  • the contrary, that false is returned when they are not greater.
  • the "equal to" boundary.
self.assertTrue(Point(x=4,y=3).xWiseGreaterOrEq(Point(x=3,y=4)))

def test_lessThan(self):
self.assertTrue(Point(x=4,y=3).yWiseLessThan(Point(x=3,y=4)))
self.assertTrue(Point(x=3,y=4).xWiseLessThan(Point(x=4,y=3)))
self.assertTrue(Point(x=4,y=3).yWiseLessOrEq(Point(x=3,y=4)))
self.assertTrue(Point(x=3,y=4).xWiseLessOrEq(Point(x=4,y=3)))


0 comments on commit 070bb36

Please sign in to comment.