Skip to main content

Versatile and flexible Python State Machine library

Project description

pysm - Python State Machine

Versatile and flexible Python State Machine library.

.. image::

.. image::

.. image::

.. image::
:alt: Code Health

.. image::
:alt: Documentation Status

Implement simple and complex state machines

It can do simple things like this:

.. image::

Or somewhat more complex like that:

.. image::

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
#Hierarchically_nested_states>`_, `PDA
<>`_ and other tasty things.


* Provide a State Pattern-like behavior with more flexibility (see
`Documentation <>`_ for
* 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


* 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


Install pysm from `PyPI <>`_::

pip install pysm

or clone the `Github pysm repository <>`_::

git clone
cd pysm
python install


Read the docs for API documentation and examples -

See Unit Tests to see it working and extensively tested.

Micropython support
The library works with pyboards!::

import upip

* `Documentation <>`_
* `Installation <>`_
* `Github <>`_
* `Issues <>`_
* `Examples <>`_

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysm-0.3.9.tar.gz (21.1 kB view hashes)

Uploaded source

Built Distribution

pysm-0.3.9-py2.py3-none-any.whl (13.5 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page