Skip to main content

Python Finite State Machines made easy.

Project description

===============================
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/travis/fgmacedo/python-statemachine.svg
:target: https://travis-ci.org/fgmacedo/python-statemachine

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

.. image:: https://pyup.io/repos/github/fgmacedo/python-statemachine/shield.svg
:target: https://pyup.io/repos/github/fgmacedo/python-statemachine/
:alt: Updates


Python finite-state machines made easy.


* Free software: MIT license
* Documentation: https://python-statemachine.readthedocs.io.


Getting started
===============

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

.. code-block:: console

$ pip install python-statemachine


Import the statemachine::

from statemachine import StateMachine, State


Define your state machine::


class TrafficLightMachine(StateMachine):
green = State('Green', initial=True)
yellow = State('Yellow')
red = State('Red')

slowdown = green.to(yellow)
stop = yellow.to(green)
go = red.to(green)


You can now create an instance::

>>> machine = TrafficLightMachine()

And inspect about the current state::

>>> machine.current_state
State('Green', identifier='green', value='green', initial=True)
>>> machine.current_state == TrafficLightMachine.green == machine.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::

>>> machine.is_green
True
>>> machine.is_yellow
False
>>> machine.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::

>>> machine.slowdown()

And check for the current status::

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

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

>>> machine.is_yellow
True
>>> machine.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::

>>> machine.is_yellow
True
>>> machine.run('stop')
>>> machine.is_red
True

A state machine can be instantiated with an initial value::

>>> machine = TrafficLightMachine(start_value='red')
>>> machine.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')
>>> machine = TrafficLightMachine(obj)
>>> machine.is_red
True
>>> obj.state
'red'
>>> obj.state = 'green'
>>> machine.is_green
True
>>> machine.slowdown()
>>> obj.state
'yellow'
>>> machine.is_yellow
True


Events
------

Docs needed.


=======
History
=======

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

Uploaded Source

Built Distribution

python_statemachine-0.3.0-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for python-statemachine-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ef7357b3b2e981f07b3d0da31d384119726457156632b75ede0aa465f8b17f9f
MD5 6e9fa988c2211c5b152e12150691c39b
BLAKE2b-256 a64e2587e36fc6ada1e4560867bf636803356d5eb161ed5b467790966862c096

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_statemachine-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 db81e3c11c69b7f455dc2b468609b80ae5ed039d40ca342dd9163543d4156777
MD5 2bf042f395671fc98e0d33de7e0b91a0
BLAKE2b-256 3c267407d9894f120a49bf67d49b3c5e28ff23ac36ecdfe7a1ad3a029e7a24c8

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