Python State Machine
Project description
Python State Machine
The State Pattern solves many problems, untangles the code and saves one’s sanity. Yet.., it’s a bit rigid and doesn’t scale. The goal of this library is to give you a close to the State Pattern simplicity with much more flexibility. And, if needed, the full state machine functionality, including FSM, HSM, PDA and other tasty things.
Goals
Provide a State Pattern-like behavior with more flexibility
Be explicit and don’t add any code to objects
Handle directly any kind of event (not only strings) - parsing strings is cool again!
Keep it simple, even for someone who’s not very familiar with the FSM terminology
Features
Finite State Machine (FSM)
Hierarchical State Machine (HSM) with Internal/External/Local transitions
Pushdown Automaton (PDA)
Transition callbacks - action, before, after
State hooks - enter, exit, and other event handlers
Entry and exit actions are associated with states, not transitions
Events may be anything as long as they’re hashable
States history and transition to previous states
Conditional transitions (if/elif/else-like logic)
Explicit behaviour (no method or attribute is added to the object containing a state machine)
No need to extend a class with State Machine class (composition over inheritance)
Fast (even with hundreds of transition rules)
Not too many pythonisms, so that it’s easily portable to other languages (ie. JavaScript).
Installation
Install pysm from PyPI:
pip install pysm
or clone the Github pysm repository:
git clone https://github.com/pgularski/pysm cd pysm python setup.py install
Documentation
Read the docs - http://pysm.readthedocs.io/
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.