-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (46 loc) · 1.63 KB
/
main.py
File metadata and controls
58 lines (46 loc) · 1.63 KB
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
import datetime
import math
import typing
class Vector2D:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
def angle(self):
return math.atan2(self.y, self.x)
def __abs__(self):
# the length of hypotenuse
return math.hypot(self.x, self.y)
def __iter__(self) -> typing.Iterator[float]:
return (i for i in (self.x, self.y))
def __repr__(self) -> str:
class_name = type(self).__name__
return "{}({!r}, {!r})".format(class_name, self.x, self.y)
def __str__(self) -> str:
return str(tuple(self))
def __format__(self, format_spec: str = "") -> str:
if format_spec.endswith("pd"):
format_spec = format_spec[:-2]
cords = (abs(self), math.degrees(self.angle()))
fmt = "<{}, {}>"
elif format_spec.endswith("p"):
format_spec = format_spec[:-1]
cords = (abs(self), self.angle())
fmt = "<{}, {}>"
else:
cords = (self.x, self.y)
fmt = "({}, {})"
components = (format(c, format_spec) for c in cords)
return fmt.format(*components)
print(f"today {datetime.datetime.now():'%a %-d %b %Y - %H:%M'}")
print(
"and we are here only to lean format "
"(f-string, built-in format() and str.format()) in pytyon"
)
origin = Vector2D(0, 0)
destination = Vector2D(2, 2)
polar_bear = Vector2D(4, 4)
print(f"origin: {origin:.4f} using %.4f")
print(f"origin: {origin:g} using %g")
print(f"dest: {destination:.3e} using %.3e")
print(f"polar_bear: {polar_bear:.4fp} using %.4fp!")
print(f"polar_bear: {polar_bear:.4fpd} using %.4fpd!")