Last active
December 23, 2019 20:07
-
-
Save chelseatroy/6c2f26b6503f64fe337bb41bd5b8a8b1 to your computer and use it in GitHub Desktop.
Storing State
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from threading import Event, Thread | |
import queue | |
class TrafficLight: | |
def __init__(self, name, state): | |
self.name = str(name) | |
self.current_state = state | |
def progress(self): | |
if self.current_state == "green": | |
print(self.name + " light yellow") | |
self.current_state = "yellow" | |
elif self.current_state == "yellow": | |
print(self.name + " light red") | |
self.current_state = "red" | |
elif self.current_state == "red": | |
print(self.name + " light green") | |
self.current_state = "green" | |
class TrafficSystem: | |
states = { | |
"red": 30, | |
"yellow": 5, | |
"green": 25 | |
} | |
current_time = 0 | |
def __init__(self, speed=3): | |
self.traffic_lights = {} | |
self.traffic_lights['1'] = TrafficLight(1, "red") | |
self.traffic_lights['2'] = TrafficLight(2, "green") | |
self.speed = speed | |
self.event_queue = queue.Queue() | |
Thread(target=self.timer_thread, args=(self.traffic_lights['1'],), daemon=True).start() | |
Thread(target=self.timer_thread, args=(self.traffic_lights['2'],), daemon=True).start() | |
self.start_traffic() | |
def start_traffic(self): | |
while True: | |
event = self.event_queue.get().split(" ") | |
if event[0] == "progress": | |
self.traffic_lights[event[1]].progress() | |
def timer_thread(self, light): | |
self.event_queue.put('progress ' + light.name) | |
time.sleep(self.states[light.current_state] / (self.speed)) | |
self.timer_thread(light) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment