-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbasic.py
executable file
·45 lines (35 loc) · 961 Bytes
/
basic.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
#!/usr/bin/env python3
# add parent to the path to include local xstate module
import sys
sys.path.insert(0, "..")
import time # noqa
from xstate import Machine # noqa
# Trafic light example
# green -> yellow -> red -> green ..
lights = Machine(
{
"id": "lights",
"initial": "green",
"states": {
"green": {
"on": {"TIMER": "yellow"},
},
"yellow": {"on": {"TIMER": "red"}},
"red": {"on": {"TIMER": "green"}},
},
}
)
if __name__ == "__main__":
state = lights.initial_state
print(state.value)
time.sleep(0.5)
for i in range(10):
state = lights.transition(state, "TIMER")
print(state.value)
time.sleep(0.5)
state = lights.transition(state, "TIMER")
print(state.value)
time.sleep(0.5)
state = lights.transition(state, "TIMER")
print(state.value)
time.sleep(0.5)