Skip to main content

Python Finite State Machines made easy.

Project description

https://img.shields.io/pypi/v/python-statemachine.svg Build status Coverage report Documentation Status Updates

Python finite-state machines made easy.

Getting started

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

$ 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 dinamically 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 m.states]
['green', 'red', 'yellow']
>>> [t.identifier for t in m.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):
...
LookupError: 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

Events

Docs needed.

Mixins

Docs needed.

History

0.5.0 (2017-07-13)

  • Custom exceptions.

  • Duplicated definition of on_execute callback is not allowed.

  • Fix bug on StateMachine.on_<transition.identifier> being called with extra self param.

0.4.2 (2017-07-10)

  • Python 3.6 support.

  • Drop official support for Python 3.3.

  • Transition can be used as decorator for on_execute callback definition.

  • Transition can point to multiple destination states.

0.3.0 (2017-03-22)

  • README getting started section.

  • Tests to state machine without model.

0.2.0 (2017-03-22)

  • State can hold a value that will be assigned to the model as the state value.

  • Travis-CI integration.

  • RTD integration.

0.1.0 (2017-03-21)

  • First release on PyPI.

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

python-statemachine-0.5.0.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

python_statemachine-0.5.0-py2.py3-none-any.whl (10.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-statemachine-0.5.0.tar.gz.

File metadata

File hashes

Hashes for python-statemachine-0.5.0.tar.gz
Algorithm Hash digest
SHA256 1ece28d44ca92be9fb7786d252fd39077211bd9dd1065a6f2fa52b105797ae1a
MD5 2dbe0e9ef6a260f20d933d026708be38
BLAKE2b-256 65e46922d4aa083ab2674aee7f339ef6e95a0cc656f0da176c0c67b976ca5778

See more details on using hashes here.

File details

Details for the file python_statemachine-0.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_statemachine-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3bef00307db99e514d4968ecd5ecc6eb362e46844022a38e76ed62a89aaec275
MD5 cb19766d267628b8dcb9e1b3e5824c95
BLAKE2b-256 f43a5a2a36b60f55739ca70f88f13b0b23c508b20c8a90d6568bfe4cb507ddd1

See more details on using hashes here.

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