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):
    ...
    ...    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 = 'p'
    >>> 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.

  • You can also define short version of all transition values like:

    >>> class Task(machine.StateMachine):
    ...
    ...     class States(Enum):
    ...
    ...         DRAFT = 'draft'
    ...         SCHEDULED = 'scheduled'
    ...         PROCESSING = 'processing'
    ...         SENT = 'sent'
    ...         FAILED = 'failed'
    ...
    ...     class Meta:
    ...
    ...         allow_empty = False
    ...         initial_state = 'd'
    ...         transitions = {
    ...             'd': ['sc', 'f'],
    ...             'sc': ['f'],
    ...             'p': ['se', 'f']
    ...         }
    ...         named_transitions = [
    ...             ('process', 'p', ['sc']),
    ...             ('fail', 'f')
    ...         ]
    ...         named_checkers = [
    ...             ('can_be_processed', 'p'),
    ...         ]

    Result code will behave the same as example above. Note also that you can always pass also enum values instead of strings.

History

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

Uploaded Source

Built Distribution

super_state_machine-1.0-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for super_state_machine-1.0.tar.gz
Algorithm Hash digest
SHA256 577c437287c2dee375ff65e323780b6dffc78f51a1b82821b68b1156d61bcd9e
MD5 762d1009993897f8cf427df3a579c5fa
BLAKE2b-256 7432685640deeaacd2a8fd0b851b3db99c8e102f33b51d4eaa34149cca16926f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for super_state_machine-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 759f37a6a783f336681a5b1ca210c6681b1fdc7f698200a7c180f1511bfca873
MD5 e16788224ccf08afd3a8571af701ef8e
BLAKE2b-256 1c570009e45887e1808ee480a93d90123e5a79ed7a373639f4181aa1d8b2221a

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