forked from butthurtclub/nice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.py
105 lines (84 loc) · 2.14 KB
/
point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
__author__ = 'nice'
from math import hypot
import unittest
__all__ = (
'Point',
)
class Point:
"""
A class for Point(x,y) representing.
Supported types: integer, floating.
Usage:
>>> first = Point()
>>> first
(0, 0)
>>> second = Point(0, 2)
>>> second
(0, 2)
>>> first.distance(second)
2.0
>>> first == second
False
>>> first = second
>>> first == second
>>> True
"""
def __init__(self, x=0, y=0):
"""
The initializer.
:param x: x-coordinate. Possible values: integer, float.
:param y: y-coordinate. Possible values: integer, float.
"""
self._x = x
self._y = y
@property
def x(self):
return self._x
@property
def y(self):
return self._y
@x.setter
def x(self, value):
self._x = value
@y.setter
def y(self, value):
self._y = value
def distance(self, other):
"""
Counts distance between two points.
:param other: Point.
:return: float.
"""
return hypot(self.x - other.x, self.y - other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self.x != other.x and self.y != other.y
def __str__(self):
return '({}, {})'.format(self.x, self.y)
class PointTest(unittest.TestCase):
def test_initial(self):
first = Point(4, 7)
self.assertIsInstance(first, Point)
self.assertEqual(first.x, 4)
self.assertEqual(first.y, 7)
self.assertEqual((first.x, first.y), (4, 7))
def test_distance(self):
first = Point(0, 0)
second = Point(0, 2)
self.assertEqual(first.distance(second), 2)
def test_equal(self):
first = Point(1, 2)
second = Point(1, 2)
self.assertEqual(first, second)
def test_not_equal(self):
first = Point(2, 3)
second = Point(1, 2)
self.assertNotEqual(first, second)
first = Point()
second = Point(0, 2)
print(first)
print(second)
print(first.distance(second))
if __name__ == '__main__':
unittest.main()