All Projects → alysivji → finite-state-machine

alysivji / finite-state-machine

Licence: MIT license
Lightweight, decorator-based Python implementation of a Finite State Machine

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to finite-state-machine

IOSD-UIETKUK-HacktoberFest-Meetup-2019
This repository for IOSD HacktoberFest 2020
Stars: ✭ 17 (-81.72%)
Mutual labels:  hacktoberfest2020
Radar.Adr
The Action-Domain-Responder core for Radar.
Stars: ✭ 53 (-43.01%)
Mutual labels:  hacktoberfest2020
smacha
SMACHA is a meta-scripting, templating, and code generation engine for rapid prototyping of ROS SMACH state machines.
Stars: ✭ 15 (-83.87%)
Mutual labels:  finite-state-machine
asscan
Automated Subnet Scanner
Stars: ✭ 25 (-73.12%)
Mutual labels:  hacktoberfest2020
Igloo
Official GitHub repo for Igloo - The private social media
Stars: ✭ 17 (-81.72%)
Mutual labels:  hacktoberfest2020
inertia-django
django connector for inertia
Stars: ✭ 90 (-3.23%)
Mutual labels:  hacktoberfest2020
ebisp
Embedded Lisp
Stars: ✭ 46 (-50.54%)
Mutual labels:  hacktoberfest2020
ml-resources-react
A simple react app displaying resources to learn Machine learning.
Stars: ✭ 15 (-83.87%)
Mutual labels:  hacktoberfest2020
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (-51.61%)
Mutual labels:  finite-state-machine
CP-DSA-Cpp-C
🕺 Give me data and I will structure it! 🔥
Stars: ✭ 27 (-70.97%)
Mutual labels:  hacktoberfest2020
HacktoberFest2020
🤗Feel free to submit a PR💻 to have it merged and get a free Hacktoberfest tee👕 from Github🔮. Updated as per new guidelines✔️
Stars: ✭ 22 (-76.34%)
Mutual labels:  hacktoberfest2020
SparkAR-Creators
The right place for SparkAR creators from around the globe. Exchange resources on SparkAR, assets, patches and scripts to make your favourite filters for Instagram and Facebook. Create your spark here!
Stars: ✭ 24 (-74.19%)
Mutual labels:  hacktoberfest2020
Map-of-India
A Node.js script to generate the Map of India in terminal
Stars: ✭ 83 (-10.75%)
Mutual labels:  hacktoberfest2020
tsm
A Hierarchical State Machine Framework in C++
Stars: ✭ 30 (-67.74%)
Mutual labels:  finite-state-machine
app-idea-generator
💡 Generate app ideas to take inspiration from, or to have a laugh
Stars: ✭ 13 (-86.02%)
Mutual labels:  hacktoberfest2020
SnowState
Finite State Machine for GameMaker
Stars: ✭ 90 (-3.23%)
Mutual labels:  finite-state-machine
neu ui
Prototype and build projects faster using Neu UI - an open source React component library designed to neumorphic style. Built using React, Styled Components, Jest and Storybook.
Stars: ✭ 23 (-75.27%)
Mutual labels:  hacktoberfest2020
CookeyLang
The official CookeyLang
Stars: ✭ 16 (-82.8%)
Mutual labels:  hacktoberfest2020
good-first-issues
Find good first issues right from your CLI! 🚀
Stars: ✭ 64 (-31.18%)
Mutual labels:  hacktoberfest2020
focussed-twitter
Let's focus on the tweets! 🐦
Stars: ✭ 15 (-83.87%)
Mutual labels:  hacktoberfest2020

Finite State Machine Banner

Finite State Machine

Latest Release Supports Python 3.6+ License: MIT Code Style: Black

Build Status codecov

Lightweight, decorator-based Python implementation of a Finite State Machine.

Table of Contents

Installation

pip install finite-state-machine

Usage

Subclass StateMachine and set the state instance variable:

from finite_state_machine import StateMachine, transition

class LightSwitch(StateMachine):
    def __init__(self):
        self.state = "off"
        super().__init__()

The transition decorator can be used to specify valid state transitions with an optional parameter for conditions. States can be of type: string, int, bool, Enum, or IntEnum. Can specify a single sate or a list of states for the source parameter; can only specify a single state as the target target. All condition functions need to return True for the transition to occur, else a ConditionsNotMet exception will be raised. Condition functions require the same positional position and keyword arguments present in the transition function.

    @transition(source="off", target="on", conditions=[light_is_off])
    def turn_on(self):
        # transition function
        # logic to define what happens to "complete a transition"
        # ex. update database record,
        ...

def light_is_off(machine):
    # condition function, first param will always be the state machine class
    # return a boolean to specify if a transition is valid
    return machine.state == "off"

Can also specify an on_error state to handle situations where the transition function raises an exception:

    @transition(source="off", target="on", on_error="failed")
    def turn_on(self):
        raise ValueError

Example

from finite_state_machine import StateMachine, transition

class Turnstile(StateMachine):
    initial_state = "close"

    def __init__(self):
        self.state = self.initial_state
        super().__init__()

    @transition(source=["close", "open"], target="open")
    def insert_coin(self):
        pass

    @transition(source="open", target="close")
    def pass_thru(self):
        pass

REPL

In [2]: turnstile = Turnstile()

In [3]: turnstile.state
Out[3]: 'close'

In [4]: turnstile.insert_coin()

In [5]: turnstile.state
Out[5]: 'open'

In [6]: turnstile.insert_coin()

In [7]: turnstile.state
Out[7]: 'open'

In [8]: turnstile.pass_thru()

In [9]: turnstile.state
Out[9]: 'close'

In [10]: turnstile.pass_thru()
---------------------------------------------------------------------------
InvalidStartState                         Traceback (most recent call last)
<ipython-input-10-6abc6f4be1cd> in <module>
----> 1 turnstile.pass_thru()

~/state_machine.py in _wrapper(*args, **kwargs)
     32
     33             if self.state not in source:
---> 34                 raise InvalidStartState
     35
     36             for condition in conditions:

InvalidStartState:

The examples folder contains additional workflows.

Asynchronous Support

finite-state-machine can be used to build both synchronous and asynchronous State Machines. The @transition decorator supports transition functions and condition functions as follows:

Synchronous transition function Asynchronous transition function
Synchronous condition function
Asynchronous condition function

State Diagram

State Machine workflows can be visualized using a state diagram.

finite-state-machine generates diagrams using Mermaid Markdown syntax, which can be viewed using the Mermaid Live Editor.

Use the fsm_draw_state_diagram command and point to State Machine workflow class that inherits from StateMachine.

# class parameter is required
$ fsm_draw_state_diagram --class examples.turnstile:Turnstile

# initial_state parameter is optional
$ fsm_draw_state_diagram --class examples.turnstile:Turnstile --initial_state close

Contributing

  1. Clone repo
  2. Create a virtual environment
  3. pip install -r requirements_dev.txt
  4. Install pre-commit
  5. Set up pre-commit hooks in repo: pre-commit install

To install a package locally for development, run:

flit install [--symlink] [--python path/to/python]

Running Tests

pytest

Inspiration

This project is inspired by django-fsm. I wanted a decorator-based state machine without having to use Django.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].