Skip to content

Instantly share code, notes, and snippets.

@Karn
Created July 29, 2017 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Karn/8fac5d8cc31a9a6b1e2bfb31e2a4267b to your computer and use it in GitHub Desktop.
Save Karn/8fac5d8cc31a9a6b1e2bfb31e2a4267b to your computer and use it in GitHub Desktop.
Python State Machine - Simple State Machine
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