This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backendChallenge.py
241 lines (196 loc) · 6.36 KB
/
backendChallenge.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
class SmartDevice:
"""
Super class for all smart devices
"""
def __init__(self):
self.switchedOn = False
self.schedule = []
for _ in range(24): # 0 - 23
self.schedule.append(None)
def toggleSwitch(self):
self.switchedOn = not self.switchedOn
def getSwitchedOn(self):
return self.switchedOn
def getSchedule(self):
return self.schedule
def getScheduleText(self):
out = ""
for i in range(24):
out += f"{self.schedule[i]};"
return out
def setActionAtHour(self, hour, action):
if hour < 0 or hour > 23:
raise ValueError("Hour must be between 0 and 23")
if action == None or action == True or action == False:
self.schedule[hour] = action
else:
raise ValueError("Action must be None (no change), True (on), or False (off)")
class SmartPlug(SmartDevice):
def __init__(self, consumptionRate=0):
super().__init__()
if consumptionRate < 0 or consumptionRate > 150:
raise ValueError("Consumption rate must be between 0 and 150")
self.consumptionRate = consumptionRate
def getConsumptionRate(self):
return self.consumptionRate
def setConsumptionRate(self, consumptionRate):
# must be between 0 and 150
if consumptionRate < 0 or consumptionRate > 150:
raise ValueError("Consumption rate must be between 0 and 150")
else:
self.consumptionRate = consumptionRate
def getCSVRow(self):
return f"SmartPlug, {self.getSwitchedOn()}, {self.getConsumptionRate()}, {self.getScheduleText()}"
def __str__(self):
out = "SmartPlug:"
out += f" switched on: {self.getSwitchedOn()}"
out += f", comp. rate: {self.getConsumptionRate()}"
return out
class SmartDoorbell(SmartDevice):
def __init__(self):
super().__init__()
self.sleepMode = False
def getSleep(self):
return self.sleepMode
def setSleep(self, sleepMode):
if sleepMode == True or sleepMode == False:
self.sleepMode = sleepMode
else:
raise ValueError("Sleep mode must be True or False")
def getCSVRow(self):
return f"SmartDoorbell, {self.getSwitchedOn()}, {self.getSleep()}, {self.getScheduleText()}"
def __str__(self):
out = "SmartDoorbell:"
out += f" switched on: {self.getSwitchedOn()}"
out += f", sleep mode: {self.getSleep()}"
return out
class SmartHome():
def __init__(self):
self.devices = []
def getDevices(self):
return self.devices
def getDeviceAt(self, index):
return self.devices[index]
def addDevice(self, device):
if not isinstance(device, SmartDevice):
raise ValueError("Device must be a SmartDevice")
self.devices.append(device)
def removeDeviceAt(self, index):
if index < 0 or index >= len(self.devices):
raise ValueError("Index out of range")
self.devices.pop(index)
# this should be toggleSwitchAt to match the other names
# but that's what the rubric says ¯\_(ツ)_/¯
def toggleSwitch(self, index):
if index < 0 or index >= len(self.devices):
raise ValueError("Index out of range")
self.devices[index].toggleSwitch()
def turnOffAll(self):
for device in self.devices:
device.switchedOn = False
def turnOnAll(self):
for device in self.devices:
device.switchedOn = True
def getCSV(self):
out = "DeviceType, Switched On, Device Option, Schedule\n"
for device in self.devices:
out += f"{device.getCSVRow()}\n"
return out
def importCSV(self, csv):
self.devices = []
csv = csv.split("\n")[1:] # remove first line
for line in csv:
if not line:
continue
device = line.split(", ")
deviceType = device[0]
switchedOn = device[1]
option = device[2]
schedule = device[3]
if deviceType == "SmartPlug":
newDevice = SmartPlug(int(option))
elif deviceType == "SmartDoorbell":
newDevice = SmartDoorbell()
if option == "True":
newDevice.setSleep(True)
else:
newDevice.setSleep(False)
else:
raise ValueError("Invalid device type")
if switchedOn == "True":
newDevice.toggleSwitch()
schedule = schedule.split(";")
for i in range(24):
newDevice.setActionAtHour(i, eval(schedule[i])) # eval is used to convert string to boolean
self.addDevice(newDevice)
def __str__(self):
out = "SmartHome"
i = 0
for device in self.devices:
out += f"\n{i}: {device}"
i += 1
return out
def testSmartPlug():
print("Testing SmartPlug")
# Create an instance of the SmartPlug class with a consumption rate of 45.
p = SmartPlug(45)
# Toggle the status of this plug by calling its toggleSwitch method.
p.toggleSwitch()
# Print the value of switchedOn using the appropriate accessor method.
print(p.getSwitchedOn())
# Print the value of consumptionRate,
print(p.getConsumptionRate())
# set it to a new valid value (of your choice),
p.setConsumptionRate(42)
# and then print it again.
print(p.getConsumptionRate())
# Print the SmartPlug object.
print(p)
def testSmartDoorbell():
print("Testing SmartDoorbell")
# Create an instance of your CustomDevice class.
d = SmartDoorbell()
# Toggle the status of this device using the toggleSwitch method.
d.toggleSwitch()
# Print the switchedOn instance variable using the appropriate accessor method.
print(d.getSwitchedOn())
# Print the current value of the option instance variable (from table 1).
print(d.getSleep())
# Then set it to a new value (of your choice).
d.setSleep(True)
# Next, print it again.
print(d.getSleep())
# Print the CustomDevice object.
print(d)
def testSmartHome():
# Create an instance of the SmartHome class and two instances of the SmartPlug class
# with consumption rates of 45.
home = SmartHome()
p1 = SmartPlug(45)
p2 = SmartPlug(45)
# Additionally, create an instance of your custom device.
d = SmartDoorbell()
# Toggle the first plug, hence turning it on. Then set its consumption rate to 150.
p1.toggleSwitch()
p1.setConsumptionRate(150)
# Then, set the consumptionRate of the second plug to 25.
p2.setConsumptionRate(25)
# Lastly, set the option of the custom device to a value of your choice.
d.setSleep(True)
# Add both plugs and the custom device to the smart home object.
home.addDevice(p1)
home.addDevice(p2)
home.addDevice(d)
# Toggle the status of the second plug using the appropriate method of the smart home object.
home.toggleSwitch(1)
# Print the smart home object.
print(home)
# Turn on all devices in the smart home and print the smart home object again.
home.turnOnAll()
print(home)
# Remove the first device and print the smart home.
home.removeDeviceAt(0)
print(home)
testSmartPlug()
testSmartDoorbell()
testSmartHome()