State Machines in Python
STMPY is a simple implementation for state machines in Python.
Installation
STMPY is available via Python Package Index, and you can install it via pip:
pip install stmpy
To upgrade, use:
pip install --upgrade stmpy
Example: Tick Tock State Machine
from stmpy import Machine, Driver
class Tick:
def __init__(self):
self.ticks = 0
self.tocks = 0
def on_init(self):
print('Init!')
def on_tick(self):
print('Tick!')
self.ticks = self.ticks + 1
def on_tock(self):
print('Tock!')
self.tocks = self.tocks + 1
= Driver()
driver = Tick()
tick
= {'source':'initial', 'target':'s_tick', 'effect':'on_init; start_timer("tick", 1000)'}
t0 = {'trigger':'tick', 'source':'s_tick', 'target':'s_tock', 'effect':'on_tick; start_timer("tock", 1000)'}
t1 = {'trigger':'tock', 'source':'s_tock', 'target':'s_tick', 'effect':'on_tock; start_timer("tick", 1000)'}
t2
= Machine(transitions=[t0, t1, t2], obj=tick, name='stm_tick')
stm_tick = stm_tick
tick.stm
driver.add_stm(stm_tick)=5)
driver.start(max_transitions driver.wait_until_finished()
Contributing
stmpy
is on GitHub. Pull requests and bug reports are welcome.