Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 26b8b3b

Browse files
author
Noah Corona
authored
Merge pull request #1 from Zer0897/animation/view
Coord Class
2 parents 2359b8a + d7e8ab4 commit 26b8b3b

File tree

5 files changed

+156
-30
lines changed

5 files changed

+156
-30
lines changed

Pipfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ verify_ssl = true
55

66
[dev-packages]
77
flake8 = "*"
8-
flake8-docstrings = "*"
8+
pytest = "*"
99

1010
[packages]
1111

@@ -14,3 +14,5 @@ python_version = "3.7"
1414

1515
[scripts]
1616
lint = "python -m flake8"
17+
start = "python -m src"
18+
test = "python -m pytest"

Pipfile.lock

Lines changed: 43 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/coord.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from __future__ import annotations
2+
3+
import operator
4+
from typing import NamedTuple, Callable, TypeVar
5+
from functools import partialmethod
6+
7+
8+
class Coord(NamedTuple):
9+
"""
10+
Helper class for managing coordinate values.
11+
12+
Coord overloads many of the numeric operators by mapping
13+
it to the x and y values individually.
14+
15+
param:
16+
x: int -- X position.
17+
y: int -- Y position.
18+
19+
example::
20+
21+
```
22+
c1 = c2 = Coord(1, 1)
23+
c1 + c2
24+
>>> Coord(2, 2)
25+
# For convenience, integers are accepted as well
26+
c1 = Coord(1, 1)
27+
c1 + 1 # 1 is cast to Coord(1, 1)
28+
>>> Coord(2, 2)
29+
```
30+
31+
note:
32+
33+
True divide `round`s in order to remain compatible with tkinter
34+
coordinate values (`int`).
35+
"""
36+
37+
x: int
38+
y: int
39+
40+
Operand = TypeVar('Operand', 'Coord', int)
41+
42+
def __apply(self, op: Callable, other: Coord.Operand) -> Coord:
43+
if isinstance(other, int):
44+
other = Coord(other, other)
45+
46+
x = op(self.x, other.x)
47+
y = op(self.y, other.y)
48+
return Coord(x, y)
49+
50+
def midpoint(self, other: Coord) -> Coord:
51+
"""
52+
The Coord that is equal distance from `self` and `other`.
53+
54+
param:
55+
other: Coord -- The point to consider.
56+
57+
return:
58+
Coord -- The resulting coordinate.
59+
"""
60+
return (self + other) / 2
61+
62+
def __truediv__(self, other):
63+
result = self.__apply(operator.truediv, other)
64+
return Coord(*map(round, result))
65+
66+
__add__ = partialmethod(__apply, operator.add)
67+
__sub__ = partialmethod(__apply, operator.sub)
68+
__mul__ = partialmethod(__apply, operator.mul)
69+
__mod__ = partialmethod(__apply, operator.mod)
70+
__pow__ = partialmethod(__apply, operator.pow)
71+
__floordiv__ = partialmethod(__apply, operator.floordiv)

src/test/__main__.py

Whitespace-only changes.

src/test/test_coord.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from ..animation import Coord
2+
3+
coord1 = Coord(1, 1)
4+
coord2 = Coord(1, 1)
5+
6+
7+
def test_add():
8+
assert coord1 + coord2 == Coord(2, 2)
9+
assert coord1 + 1 == Coord(2, 2)
10+
11+
12+
def test_sub():
13+
assert coord1 - coord2 == Coord(0, 0)
14+
assert coord1 - 1 == Coord(0, 0)
15+
16+
17+
def test_mul():
18+
assert coord1 * coord2 == Coord(1, 1)
19+
assert coord1 * 1 == Coord(1, 1)
20+
21+
22+
def test_mod():
23+
assert coord1 % coord2 == Coord(0, 0)
24+
assert coord1 % 1 == Coord(0, 0)
25+
26+
27+
def test_pow():
28+
assert coord1 ** coord2 == Coord(1, 1)
29+
assert coord1 ** 1 == Coord(1, 1)
30+
31+
32+
def test_truediv():
33+
assert coord1 / coord2 == Coord(1, 1)
34+
assert Coord(2, 2) / 3 == Coord(1, 1)
35+
36+
37+
def test_floordiv():
38+
assert coord1 // coord2 == Coord(1, 1)
39+
assert coord1 // 1 == Coord(1, 1)

0 commit comments

Comments
 (0)