Skip to main content

Versatile and flexible Python State Machine library

Project description

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

Project details


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.4.0.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysm-0.4.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file pysm-0.4.0.tar.gz.

File metadata

  • Download URL: pysm-0.4.0.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc2

File hashes

Hashes for pysm-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d734f39ca5d144aea4418610daea310311a2080e0dd02c7b4e0b04167c7139f5
MD5 e719efc013b840e247db72197d59c61a
BLAKE2b-256 472e57d665753d952feab5535dedcc92ba28cdd2a50e63d42fff1ad01e9df1cf

See more details on using hashes here.

File details

Details for the file pysm-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: pysm-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc2

File hashes

Hashes for pysm-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0494a3fbb3e4a231d91677cdb6cee1f5ac6e09996d105ecca0582e05fce0fa0d
MD5 c3c7f9a8af2e037b5587785189487cf0
BLAKE2b-256 bb71d10036642573887d30d0083e356f1b32935cdd9b37a656569c91208edf9a

See more details on using hashes here.

Supported by

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