-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatemachine.py
More file actions
27 lines (23 loc) · 835 Bytes
/
statemachine.py
File metadata and controls
27 lines (23 loc) · 835 Bytes
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
#!/usr/bin/python
# Author: Justin
"""
This module asserts two abstract classes for state machine application.
"""
class State(object):
"""Template class State"""
def run(self):
assert False, "State.run() not implemented!"
def next(self):
assert False, "State.next() not implemented!"
class StateMachine(object):
"""Template class StateMachine"""
def __init__(self, initial_state):
"""Initialize a state machine. The initial state is added."""
print("StateMachine starts running ...")
self.curr_state = initial_state
def runAll(self):
"""Run all states"""
while self.curr_state is not None:
self.curr_state.run()
self.curr_state = self.curr_state.next()
print("StateMachine has terminated.")