Skip to content

Instantly share code, notes, and snippets.

@ilium007
Forked from Karn/simple_device.py
Created April 12, 2022 15:07
Show Gist options
  • Save ilium007/3529129f3cb2b8206f238a8e091ad629 to your computer and use it in GitHub Desktop.
Save ilium007/3529129f3cb2b8206f238a8e091ad629 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