-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathBerlinClock.py
161 lines (116 loc) · 4.83 KB
/
BerlinClock.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from abc import ABCMeta
import copy
import sys
import re
class Parser:
PATTERN = "(\d{2})::(\d{2}):(\d{2})"
def parse(self, time):
reg = re.search(self.PATTERN, time)
if (reg):
return Time(reg.group(1), reg.group(2), reg.group(3))
else:
raise ValueError("time: " + time + " doesn't match to pattern: " + self.PATTERN)
class BerlinClock:
TIME_UNIT_DELIMITER = " "
def displayTime(self, input):
t = Parser().parse(input)
return Seconds().display(t.s) + self.TIME_UNIT_DELIMITER + \
Hours().display(t.h) + self.TIME_UNIT_DELIMITER + \
Minutes().display(t.m);
class Precondition:
def __init__(self, unitsPredicate, errorMessage):
self.unitsPredicate = unitsPredicate
self.errorMessage = errorMessage
def validate(self, units):
if self.unitsPredicate(units) is False:
raise ValueError(self.errorMessage)
class Time:
def __init__(self, h, m, s):
self.h = int(h)
self.m = int(m)
self.s = int(s)
self.TIME_TO_STRING_SEPARATOR = ":"
def __str__(self):
return str(self.h) + self.TIME_TO_STRING_SEPARATOR + str(self.m) + self.TIME_TO_STRING_SEPARATOR + str(
self.s)
def __eq__(self, other):
return self.__dict__ == other.__dict__
class Color:
YELLOW = "Y"
RED = "R"
class Lamp:
def __init__(self, color):
self.switch = False
self.color = color
self.SWITCH_OFF_COLOR = "O"
def switchOn(self):
self.switch = True
def switchOff(self):
self.switch = False
def __str__(self):
return self.color.__str__() if self.switch else self.SWITCH_OFF_COLOR
class AbsTimeUnit:
__metaclass__ = ABCMeta
def display(self, units):
self.precondition.validate(units)
return " ".join(timeUnit.display(units) for timeUnit in self.timeUnits)
class Hours(AbsTimeUnit):
def __init__(self):
self.precondition = Precondition(lambda units: (units >= 1 and units <= 24),
"Hour units must be in range: units >= 1 && units <= 24")
self.timeUnits = HoursFactory().createParts()
class Minutes(AbsTimeUnit):
def __init__(self):
self.precondition = Precondition(lambda units: (units >= 0 and units <= 59),
"Minute units must be in range: units >= 0 && units <= 59")
self.timeUnits = MinutesFactory().createParts()
class Seconds(AbsTimeUnit):
def __init__(self):
self.precondition = Precondition(lambda units: (units >= 0 and units <= 59),
"Second units must be in range: units >= 0 && units <= 59")
self.timeUnits = SecondsFactory().createParts()
class TimeUnitPart:
def __init__(self, lampsFunction, lampsList):
self.lampsFunction = lampsFunction
self.lampsList = lampsList
def display(self, units):
listClone = copy.deepcopy(self.lampsList)
for i in range(0, self.lampsFunction(units)):
listClone[i].switchOn()
return "".join(str(lamp) for lamp in listClone)
def calc(self, units):
return self.lampsFunction(units)
class SecondsFactory:
def createPart(self):
return TimeUnitPart(lambda units: (abs((units % 2) - 1)), [Lamp(Color.YELLOW)])
def createParts(self):
return [self.createPart()]
class MinutesFactory:
def createTopPart(self):
return TimeUnitPart(lambda units: (units / 5),
[
Lamp(Color.YELLOW), Lamp(Color.YELLOW), Lamp(Color.RED),
Lamp(Color.YELLOW), Lamp(Color.YELLOW), Lamp(Color.RED),
Lamp(Color.YELLOW), Lamp(Color.YELLOW), Lamp(Color.RED),
Lamp(Color.YELLOW), Lamp(Color.YELLOW)
])
def createBottomPart(self):
return TimeUnitPart(lambda units: (units % 5),
[
Lamp(Color.YELLOW), Lamp(Color.YELLOW), Lamp(Color.YELLOW), Lamp(Color.YELLOW)
])
def createParts(self):
return [self.createTopPart(), self.createBottomPart()]
class HoursFactory:
def createTopPart(self):
return TimeUnitPart(lambda units: (units / 5),
[
Lamp(Color.RED), Lamp(Color.RED), Lamp(Color.RED), Lamp(Color.RED)
])
def createBottomPart(self):
return TimeUnitPart(lambda units: (units % 5),
[
Lamp(Color.RED), Lamp(Color.RED), Lamp(Color.RED), Lamp(Color.RED)
])
def createParts(self):
return [self.createTopPart(), self.createBottomPart()]