All Projects → fgmacedo → Python Statemachine

fgmacedo / Python Statemachine

Licence: mit
Python Finite State Machines made easy.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Python Statemachine

use-tiny-state-machine
A tiny (~700 bytes) react hook to help you write finite state machines
Stars: ✭ 37 (-79.89%)
Mutual labels:  state-machine, finite-state-machine
Xstateful
A wrapper for xstate that stores state, handles transitions, emits events for state changes and actions/activities, and includes an optional reducer framework for updating state and invoking side-effects
Stars: ✭ 81 (-55.98%)
Mutual labels:  state-machine, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+44.02%)
Mutual labels:  state-machine, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-74.46%)
Mutual labels:  state-machine, finite-state-machine
Hsm
Finite state machine library based on the boost hana meta programming library. It follows the principles of the boost msm and boost sml libraries, but tries to reduce own complex meta programming code to a minimum.
Stars: ✭ 106 (-42.39%)
Mutual labels:  state-machine, finite-state-machine
SimpleStateMachineLibrary
📚 A simple library for realization state machines in C# code
Stars: ✭ 30 (-83.7%)
Mutual labels:  state-machine, finite-state-machine
Hal
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down
Stars: ✭ 63 (-65.76%)
Mutual labels:  state-machine, finite-state-machine
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-84.78%)
Mutual labels:  state-machine, finite-state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (-17.93%)
Mutual labels:  state-machine, finite-state-machine
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (-52.17%)
Mutual labels:  state-machine, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+32.07%)
Mutual labels:  state-machine, finite-state-machine
Sm
🚀 SM – a static State Machine library
Stars: ✭ 149 (-19.02%)
Mutual labels:  state-machine, finite-state-machine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-86.41%)
Mutual labels:  state-machine, finite-state-machine
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+931.52%)
Mutual labels:  state-machine, finite-state-machine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-89.13%)
Mutual labels:  state-machine, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+142.39%)
Mutual labels:  state-machine, finite-state-machine
smacha
SMACHA is a meta-scripting, templating, and code generation engine for rapid prototyping of ROS SMACH state machines.
Stars: ✭ 15 (-91.85%)
Mutual labels:  state-machine, finite-state-machine
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+11468.48%)
Mutual labels:  state-machine, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (-54.35%)
Mutual labels:  state-machine, finite-state-machine
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (-38.59%)
Mutual labels:  state-machine, finite-state-machine

==================== Python State Machine

.. image:: https://img.shields.io/pypi/v/python-statemachine.svg :target: https://pypi.python.org/pypi/python-statemachine

.. image:: https://img.shields.io/pypi/dm/python-statemachine.svg :target: https://pypi.python.org/pypi/python-statemachine

.. image:: https://travis-ci.org/fgmacedo/python-statemachine.svg?branch=develop :target: https://travis-ci.org/fgmacedo/python-statemachine :alt: Build status

.. image:: https://codecov.io/gh/fgmacedo/python-statemachine/branch/develop/graph/badge.svg :target: https://codecov.io/gh/fgmacedo/python-statemachine :alt: Coverage report

.. image:: https://readthedocs.org/projects/python-statemachine/badge/?version=latest :target: https://python-statemachine.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status

Python finite-state machines <https://en.wikipedia.org/wiki/Finite-state_machine>_ made easy.

Getting started

To install Python State Machine, run this command in your terminal:

.. code-block:: console

$ pip install python-statemachine

Define your state machine:

from statemachine import StateMachine, State

class TrafficLightMachine(StateMachine): ... green = State('Green', initial=True) ... yellow = State('Yellow') ... red = State('Red') ... ... slowdown = green.to(yellow) ... stop = yellow.to(red) ... go = red.to(green)

You can now create an instance:

traffic_light = TrafficLightMachine()

And inspect about the current state:

traffic_light.current_state State('Green', identifier='green', value='green', initial=True) traffic_light.current_state == TrafficLightMachine.green == traffic_light.green True

For each state, there's a dynamically created property in the form is_<state.identifier>, that returns True if the current status matches the query:

traffic_light.is_green True traffic_light.is_yellow False traffic_light.is_red False

Query about metadata:

[s.identifier for s in traffic_light.states] ['green', 'red', 'yellow'] [t.identifier for t in traffic_light.transitions] ['go', 'slowdown', 'stop']

Call a transition:

traffic_light.slowdown()

And check for the current status:

traffic_light.current_state State('Yellow', identifier='yellow', value='yellow', initial=False) traffic_light.is_yellow True

You can't run a transition from an invalid state:

traffic_light.is_yellow True traffic_light.slowdown() Traceback (most recent call last): statemachine.exceptions.TransitionNotAllowed: Can't slowdown when in Yellow.

You can also trigger events in an alternative way, calling the run(<transition.identificer>) method:

traffic_light.is_yellow True traffic_light.run('stop') traffic_light.is_red True

A state machine can be instantiated with an initial value:

machine = TrafficLightMachine(start_value='red') traffic_light.is_red True

Models

If you need to persist the current state on another object, or you're using the state machine to control the flow of another object, you can pass this object to the StateMachine constructor:

class MyModel(object): ... def init(self, state): ... self.state = state ... obj = MyModel(state='red') traffic_light = TrafficLightMachine(obj) traffic_light.is_red True obj.state 'red' obj.state = 'green' traffic_light.is_green True traffic_light.slowdown() obj.state 'yellow' traffic_light.is_yellow True

Callbacks

Callbacks when running events:

from statemachine import StateMachine, State

class TrafficLightMachine(StateMachine): ... "A traffic light machine" ... green = State('Green', initial=True) ... yellow = State('Yellow') ... red = State('Red') ... ... slowdown = green.to(yellow) ... stop = yellow.to(red) ... go = red.to(green) ... ... def on_slowdown(self): ... print('Calma, lá!') ... ... def on_stop(self): ... print('Parou.') ... ... def on_go(self): ... print('Valendo!')

stm = TrafficLightMachine() stm.slowdown() Calma, lá! stm.stop() Parou. stm.go() Valendo!

Or when entering/exiting states:

from statemachine import StateMachine, State

class TrafficLightMachine(StateMachine): ... "A traffic light machine" ... green = State('Green', initial=True) ... yellow = State('Yellow') ... red = State('Red') ... ... cycle = green.to(yellow) | yellow.to(red) | red.to(green) ... ... def on_enter_green(self): ... print('Valendo!') ... ... def on_enter_yellow(self): ... print('Calma, lá!') ... ... def on_enter_red(self): ... print('Parou.')

stm = TrafficLightMachine() stm.cycle() Calma, lá! stm.cycle() Parou. stm.cycle() Valendo!

Mixins

Your model can inherited from a custom mixin to auto-instantiate a state machine.

from statemachine.mixins import MachineMixin

class CampaignMachineWithKeys(StateMachine): ... "A workflow machine" ... draft = State('Draft', initial=True, value=1) ... producing = State('Being produced', value=2) ... closed = State('Closed', value=3) ... cancelled = State('Cancelled', value=4) ... ... add_job = draft.to.itself() | producing.to.itself() ... produce = draft.to(producing) ... deliver = producing.to(closed) ... cancel = cancelled.from_(draft, producing)

class MyModel(MachineMixin): ... state_machine_name = 'CampaignMachineWithKeys' ... ... def init(self, **kwargs): ... for k, v in kwargs.items(): ... setattr(self, k, v) ... super(MyModel, self).init() ... ... def repr(self): ... return "{}({!r})".format(type(self).name, self.dict)

model = MyModel(state=1) assert isinstance(model.statemachine, CampaignMachineWithKeys) assert model.state == 1 assert model.statemachine.current_state == model.statemachine.draft model.statemachine.cancel() assert model.state == 4 assert model.statemachine.current_state == model.statemachine.cancelled

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].