Skip to main content

No project description provided

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(machines.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

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

super_state_machine-2.1.0-py2.py3-none-any.whl (8.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: super_state_machine-2.1.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for super_state_machine-2.1.0.tar.gz
Algorithm Hash digest
SHA256 dbea42b7440255e76726221a8b1c7980d8928d427bc3c25714c10e0c6f15cfe6
MD5 1af15fe7175f8fabfc7f5469c39964ba
BLAKE2b-256 376c6c13e32bf1903f1d71bb21dd8d0b62a7977d514a0d2c399bfaec2e8e4299

See more details on using hashes here.

Provenance

The following attestation bundles were made for super_state_machine-2.1.0.tar.gz:

Publisher: release.yml on beregond/super_state_machine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for super_state_machine-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c6a4d07202486096004055c77824d979c59248ea23b689addb46a5867e259821
MD5 3a6de8924366b6c9461fb7391b94d6b9
BLAKE2b-256 e3e9e1286da32d514ab1bab27b0f28b00e012f4634aa4521fff7a17bfd9c302a

See more details on using hashes here.

Provenance

The following attestation bundles were made for super_state_machine-2.1.0-py2.py3-none-any.whl:

Publisher: release.yml on beregond/super_state_machine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page