-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights.py
50 lines (43 loc) · 1.58 KB
/
lights.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
from objectBase import ObjectBase
class TrafficLights(ObjectBase):
def __init__(self, id_=None, parent_object=None,
lights_enabled=False, green_duration=None):
super().__init__(id_=id_, parent_object=parent_object)
# Controls whether lights are working or not
self.on = lights_enabled
# Current lights state
self.green = False
# How long green light should be on
self.green_duration = green_duration
def enable_lights(self, state=True):
"""Method enables or disables lights"""
self.on = state
def get_green_duration(self):
"""Method returns green light duration"""
return self.green_duration
def get_lights_state(self):
"""
Method returns current lights state.
Returns None if lights are disabled
or True/False depending on green state
"""
if not self.is_on():
return None
return True if self.is_green() else False
def is_on(self):
"""Method returns true if lights are working"""
return self.on
def is_green(self):
"""Method returns true if lights are green"""
return True if self.green else False
def switch_lights(self, state=None):
"""
Method switches current light to desired state
or to opposite if desired is not given.
If desired state is the same as current state, does nothing.
Returns final lights state.
"""
if state is None:
state = not self.green
self.green = state
return state