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.1.tar.gz (45.0 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.1-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pysm-0.4.1.tar.gz
  • Upload date:
  • Size: 45.0 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.1.tar.gz
Algorithm Hash digest
SHA256 4f9727e0157eba646dece6c9d6a46cca465e8a049cba34038ed21c797a51ed73
MD5 91b7dcbaa4635317cb07f6cd29507f15
BLAKE2b-256 c5ebbd5b23a0228f796b8492e5630bdf03d900a37fbe0e6bd7a63b819261b33a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pysm-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 22.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0a00ee8bfecd200b8c843bc9140eb6487c8e68536089465cb68afd4d6e01bb43
MD5 85852fecf130ca2dfb255b88351a179d
BLAKE2b-256 1fd746838bb3cc533c70673dc40c63e3b39f8038dfe3f23160022132faa381d3

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