forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeslice.py
91 lines (75 loc) · 1.76 KB
/
timeslice.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
"""
Could this be valid Python?
if now >= T[4:20:PM]: chill()
>>> t = T[4:20]
>>> t
T[4:20]
>>> h, m, s = t
>>> h, m, s
(4, 20, 0)
>>> t[11:59:AM]
T[11:59:AM]
>>> start = t[9:O1:PM]
>>> start
T[9:O1:PM]
>>> start.h, start.m, start.s, start.pm
(9, 1, 0, True)
>>> now = T[7:O1:PM]
>>> T[4:OO:PM]
T[4:OO:PM]
>>> now > T[4:20:PM]
True
"""
import functools
AM = -2
PM = -1
for n in range(10):
globals()[f'O{n}'] = n
OO = 0
@functools.total_ordering
class T():
def __init__(self, arg):
if isinstance(arg, slice):
h = arg.start or 0
m = arg.stop or 0
s = arg.step or 0
else:
h, m, s = 0, 0, arg
if m in (AM, PM):
self.pm = m == PM
m = 0
elif s in (AM, PM):
self.pm = s == PM
s = 0
else:
self.pm = None
self.h, self.m, self.s = h, m, s
def __class_getitem__(cls, arg):
return cls(arg)
def __getitem__(self, arg):
return(type(self)(arg))
def __repr__(self):
h, m, s = self.h, self.m, self.s or None
if m == 0:
m = f'OO'
elif m < 10:
m = f'O{m}'
s = '' if s is None else s
if self.pm is None:
pm = ''
else:
pm = ':' + ('AM', 'PM')[self.pm]
return f'T[{h}:{m}{s}{pm}]'
def __iter__(self):
yield from (self.h, self.m, self.s)
def __eq__(self, other):
return tuple(self) == tuple(other)
def __lt__(self, other):
return tuple(self) < tuple(other)
def __add__(self, other):
"""
>>> T[11:O5:AM] + 15 # TODO: preserve pm field
T[11:20]
"""
if isinstance(other, int):
return self[self.h:self.m + other:self.pm]