Skip to main content

pysm - Python State Machine

Versatile and flexible Python State Machine library.

CI Documentation Status

Implement simple and complex state machines

It can do simple things like this:

https://cloud.githubusercontent.com/assets/3026621/15031178/bf5efb2a-124e-11e6-9748-0b5a5be60a30.png

Or somewhat more complex like that:

https://cloud.githubusercontent.com/assets/3026621/15031148/ad955f06-124e-11e6-865e-c7e3340f14cb.png

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')

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)

Source distribution for pysm 0.4.1
File Size Uploaded
pysm-0.4.1.tar.gz 45.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pysm 0.4.1
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

1 release file

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page