Created
July 29, 2017 07:18
-
-
Save Karn/8fac5d8cc31a9a6b1e2bfb31e2a4267b to your computer and use it in GitHub Desktop.
Python State Machine - Simple State Machine
This file contains 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
from states import LockedState | |
class SimpleDevice(object): | |
""" | |
A simple state machine that mimics the functionality of a device from a | |
high level. | |
""" | |
def __init__(self): | |
""" Initialize the components. """ | |
# Start with a default state. | |
self.state = LockedState() | |
def on_event(self, event): | |
""" | |
This is the bread and butter of the state machine. Incoming events are | |
delegated to the given states which handle the state. | |
""" | |
# The next state will be the result of the on_event function. | |
self.state = self.state.on_event(event) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment