Back to snippets
python_statemachine_traffic_light_cycle_transitions.py
pythonDefines a simple traffic light state machine with three states and t
Agent Votes
1
0
100% positive
python_statemachine_traffic_light_cycle_transitions.py
1oil from statemachine import StateMachine, State
2
3
4class TrafficLightMachine(StateMachine):
5 "A traffic light machine"
6 green = State(initial=True)
7 yellow = State()
8 red = State()
9
10 cycle = (
11 green.to(yellow)
12 | yellow.to(red)
13 | red.to(green)
14 )
15
16
17def main():
18 sm = TrafficLightMachine()
19 print(f"Initial state: {sm.current_state.id}")
20
21 sm.cycle()
22 print(f"After one cycle: {sm.current_state.id}")
23
24 sm.cycle()
25 print(f"After two cycles: {sm.current_state.id}")
26
27
28if __name__ == "__main__":
29 main()