No project description provided
Project description
Super State Machine gives you utilities to build finite state machines.
Free software: BSD license
Documentation: https://super_state_machine.readthedocs.org
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbea42b7440255e76726221a8b1c7980d8928d427bc3c25714c10e0c6f15cfe6
|
|
| MD5 |
1af15fe7175f8fabfc7f5469c39964ba
|
|
| BLAKE2b-256 |
376c6c13e32bf1903f1d71bb21dd8d0b62a7977d514a0d2c399bfaec2e8e4299
|
Provenance
The following attestation bundles were made for super_state_machine-2.1.0.tar.gz:
Publisher:
release.yml on beregond/super_state_machine
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
super_state_machine-2.1.0.tar.gz -
Subject digest:
dbea42b7440255e76726221a8b1c7980d8928d427bc3c25714c10e0c6f15cfe6 - Sigstore transparency entry: 479450768
- Sigstore integration time:
-
Permalink:
beregond/super_state_machine@2ff5f012bc6755fd2034a4c012efda9d782c382a -
Branch / Tag:
refs/tags/2.1.0 - Owner: https://github.com/beregond
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2ff5f012bc6755fd2034a4c012efda9d782c382a -
Trigger Event:
push
-
Statement type:
File details
Details for the file super_state_machine-2.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: super_state_machine-2.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6a4d07202486096004055c77824d979c59248ea23b689addb46a5867e259821
|
|
| MD5 |
3a6de8924366b6c9461fb7391b94d6b9
|
|
| BLAKE2b-256 |
e3e9e1286da32d514ab1bab27b0f28b00e012f4634aa4521fff7a17bfd9c302a
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
super_state_machine-2.1.0-py2.py3-none-any.whl -
Subject digest:
c6a4d07202486096004055c77824d979c59248ea23b689addb46a5867e259821 - Sigstore transparency entry: 479450790
- Sigstore integration time:
-
Permalink:
beregond/super_state_machine@2ff5f012bc6755fd2034a4c012efda9d782c382a -
Branch / Tag:
refs/tags/2.1.0 - Owner: https://github.com/beregond
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2ff5f012bc6755fd2034a4c012efda9d782c382a -
Trigger Event:
push
-
Statement type: