Skip to main content

Super State Machine gives you utilities to build finite state machines.

Project description

https://badge.fury.io/py/super_state_machine.png https://travis-ci.org/beregond/super_state_machine.png?branch=master https://pypip.in/d/super_state_machine/badge.png

Super State Machine gives you utilities to build finite state machines.

Features

  • Fully tested with Python 2.7, 3.3, 3.4 and PyPy.

  • Create finite state machines:

    >>> from enum import Enum
    
    >>> from super_state_machine import machines
    
    
    >>> class Task(machines.StateMachine):
    ...
    ...    state = 'draft'
    ...
    ...    class States(Enum):
    ...
    ...         DRAFT = 'draft'
    ...         SCHEDULED = 'scheduled'
    ...         PROCESSING = 'processing'
    ...         SENT = 'sent'
    ...         FAILED = 'failed'
    
    >>> task = Task()
    >>> task.is_draft
    False
    >>> task.set_draft()
    >>> task.state
    'draft'
    >>> task.state = 'scheduled'
    >>> task.is_scheduled
    True
    >>> task.state = 'process'
    >>> task.state
    'processing'
    >>> task.state = 'wrong'
    *** ValueError: Unrecognized value ('wrong').
  • Define allowed transitions graph, define additional named transitions and checkers:

    >>> class Task(machines.StateMachine):
    ...
    ...     class States(Enum):
    ...
    ...         DRAFT = 'draft'
    ...         SCHEDULED = 'scheduled'
    ...         PROCESSING = 'processing'
    ...         SENT = 'sent'
    ...         FAILED = 'failed'
    ...
    ...     class Meta:
    ...
    ...         allow_empty = False
    ...         initial_state = 'draft'
    ...         transitions = {
    ...             'draft': ['scheduled', 'failed'],
    ...             'scheduled': ['failed'],
    ...             'processing': ['sent', 'failed']
    ...         }
    ...         named_transitions = [
    ...             ('process', 'processing', ['scheduled']),
    ...             ('fail', 'failed')
    ...         ]
    ...         named_checkers = [
    ...             ('can_be_processed', 'processing'),
    ...         ]
    
    >>> task = Task()
    >>> task.state
    'draft'
    >>> task.process()
    *** TransitionError: Cannot transit from 'draft' to 'processing'.
    >>> task.set_scheduled()
    >>> task.can_be_processed
    True
    >>> task.process()
    >>> task.state
    'processing'
    >>> task.fail()
    >>> task.state
    'failed'

    Note, that third argument restricts from which states transition will be added to allowed (in case of process, new allowed transition will be added, from ‘scheduled’ to ‘processing’). No argument means all available states, None or empty list won’t add anything beyond defined ones.

  • Use state machines as properties:

>>> from enum import Enum

>>> from super_state_machine import machines, extras


>>> class Lock(machine.StateMachine):

...     class States(Enum):
...
...         OPEN = 'open'
...         LOCKED = 'locked'
...
...     class Meta:
...
...         allow_empty = False
...         initial_state = 'locked'
...         named_transitions = [
...             ('open', 'open'),
...             ('lock', 'locked'),
...         ]


>>> class Safe(object):
...
...     lock1 = extras.PropertyMachine(Lock)
...     lock2 = extras.PropertyMachine(Lock)
...     lock3 = extras.PropertyMachine(Lock)
...
...     locks = ['lock1', 'lock2', 'lock3']
...
...     def is_locked(self):
...          locks = [getattr(self, lock).is_locked for lock in self.locks]
...          return any(locks)
...
...     def is_open(self):
...         locks = [getattr(self, lock).is_open for lock in self.locks]
...         return all(locks)

>>> safe = Safe()
>>> safe.lock1
'locked'
>>> safe.is_open
False
>>> safe.lock1.open()
>>> safe.lock1.is_open
True
>>> safe.lock1
'open'
>>> safe.is_open
False
>>> safe.lock2.open()
>>> safe.lock3 = 'open'
>>> safe.is_open
True

History

2.0 (2016-09-26)

  • Added force_set method.

  • Added field machine.

  • Added support for Python 3.5.

Backward compatibility breaks:

  • Empty state is now disallowed.

  • Only full names are allowed, when using scalars, no shortcuts.

  • Removed support for unhashable types.

1.0 (2014-09-04)

  • Added all basic features.

0.1.0 (2014-08-08)

  • First release on PyPI.

  • Added utilities to create simple state machine.

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

super_state_machine-2.0.1.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

super_state_machine-2.0.1-py2.py3-none-any.whl (10.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file super_state_machine-2.0.1.tar.gz.

File metadata

File hashes

Hashes for super_state_machine-2.0.1.tar.gz
Algorithm Hash digest
SHA256 7b84e77990362f21343235e5f424dbbfb22e05084a6b9ff26ee1b6d9d03402f7
MD5 deaffc04aa15ed5a149ab684c063f232
BLAKE2b-256 ab288c68d40026936c5eae788961dc43c32f04a29907c9fa7d97beb58bfb7879

See more details on using hashes here.

File details

Details for the file super_state_machine-2.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for super_state_machine-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 742f7533fcad4f2679aa6bf8bf056a50cc5cf7d30776d094a145d6987b9246b6
MD5 3f77214e5ea91d64b5e6d829111f3243
BLAKE2b-256 062c1d90911f573e963baa01513d27a4a470ad3001707475d046464b9c325303

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