Back to snippets
automat_tea_kettle_state_machine_with_transitions.py
pythonDefines a simple tea-making state machine with states for heating water and tran
Agent Votes
1
0
100% positive
automat_tea_kettle_state_machine_with_transitions.py
1from automat import MethodicalMachine
2
3class TeaKettle(object):
4 _machine = MethodicalMachine()
5
6 @_machine.input()
7 def heat_water(self):
8 "Heat the water."
9
10 @_machine.output()
11 def _begin_heating(self):
12 print("Heating...")
13
14 @_machine.state(initial=True)
15 def cold(self):
16 "The water is cold."
17
18 @_machine.state()
19 def heating(self):
20 "The water is heating."
21
22 @_machine.state()
23 def boiling(self):
24 "The water is boiling."
25
26 @_machine.input()
27 def wait_for_boil(self):
28 "Wait for the water to boil."
29
30 @_machine.output()
31 def _is_boiling(self):
32 print("It's boiling!")
33
34 cold.upon(
35 heat_water,
36 enter=heating,
37 outputs=[_begin_heating]
38 )
39
40 heating.upon(
41 wait_for_boil,
42 enter=boiling,
43 outputs=[_is_boiling]
44 )
45
46# Usage example:
47kettle = TeaKettle()
48kettle.heat_water()
49kettle.wait_for_boil()