pysm - Python State Machine
Versatile and flexible Python State Machine library.
Implement simple and complex state machines
It can do simple things like this:
Or somewhat more complex like that:
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 (see Documentation for examples)
Be explicit and don’t add any magic code to objects that use pysm
Handle directly any kind of event or input (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).
Micropython support
Optional queued, thread-safe, async, serialization, builder, and typing helpers are available as explicit imports, so the core import stays small.
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 for API documentation and examples - http://pysm.readthedocs.io/
The full documentation also covers the optional queued, thread-safe, async, serialization, and builder modules.
See Unit Tests to see it working and extensively tested.
Micropython support
The core runtime remains the intended shape for memory-constrained MicroPython/upysm builds. Optional CPython-oriented modules should stay out of device builds unless you explicitly need them and have measured the memory cost.
The library works with pyboards!:
import upip
upip.install('upysm')
Links
Release files for pysm 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pysm-0.4.1.tar.gz | 45.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pysm-0.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 67.7 kB
Release files / pysm-0.4.1.tar.gz
| Download URL | pysm-0.4.1.tar.gz |
|---|---|
| Size | 45.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f9727e0157eba646dece6c9d6a46cca465e8a049cba34038ed21c797a51ed73
|
|
BLAKE2b-256 checksum How to use checksums |
c5ebbd5b23a0228f796b8492e5630bdf03d900a37fbe0e6bd7a63b819261b33a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0rc2
|
Release files / pysm-0.4.1-py3-none-any.whl
| Download URL | pysm-0.4.1-py3-none-any.whl |
|---|---|
| Size | 22.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a00ee8bfecd200b8c843bc9140eb6487c8e68536089465cb68afd4d6e01bb43
|
|
BLAKE2b-256 checksum How to use checksums |
1fd746838bb3cc533c70673dc40c63e3b39f8038dfe3f23160022132faa381d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0rc2
|