Overview
docs |
|
|---|---|
tests |
|
package |
Minimal state machine
Free software: BSD license
import fsm
class MyTasks(fsm.FiniteStateMachineMixin):
"""An example to test the state machine.
Contains transitions to everywhere, nowhere and specific states.
"""
state_machine = {
'created': '__all__',
'pending': ('running',),
'running': ('success', 'failed'),
'success': None,
'failed': ('retry',),
'retry': ('pending', 'retry'),
}
def __init__(self, state):
"""Initialize setting a state."""
self.state = state
def on_before_pending(self):
print("I'm going to a pending state")
In [4]: m = MyTasks(state='created')
In [5]: m.change_state('pending')
I'm going to a pending state
Out[5]: 'pending'
In [6]: m.change_state('failed') # Let's try to transition to an invalid state
---------------------------------------------------------------------------
InvalidTransition Traceback (most recent call last)
<ipython-input-6-71d2461eee74> in <module>()
----> 1 m.change_state('failed')
~/pyfsm/src/fsm/fsm.py in change_state(self, next_state, **kwargs)
90 msg = "The transition from {0} to {1} is not valid".format(previous_state,
91 next_state)
---> 92 raise InvalidTransition(msg)
93
94 name = 'pre_{0}'.format(next_state)
InvalidTransition: The transition from pending to failed is not valid
Installation
pip install fsmpy
Usage
Define in a class the
state_machineInitialize
state, either with a value, using__init__or as a django fieldAdd hooks:
Method |
Description |
on_before_change_state |
Before transitioning to the state |
on_change_state |
After transitioning to the state, if no failure, runs for every state |
pre_<state_name> |
Runs before a particular state, where |
post_<state_name> |
Runs after a particular state, where |
This hooks will receive any extra argument given to change_state
E.g:
Running m.change_state('pending', name='john') will trigger pre_pending(name='john')
Django integration
import fsm
from django.db import models
class MyModel(models.Model, fsm.FiniteStateMachineMixin):
"""An example to test the state machine.
Contains transitions to everywhere, nowhere and specific states.
"""
CHOICES = (
('created', 'CREATED'),
('pending', 'PENDING'),
('running', 'RUNNING'),
('success', 'SUCCESS'),
('failed', 'FAILED'),
('retry', 'RETRY'),
)
state_machine = {
'created': '__all__',
'pending': ('running',),
'running': ('success', 'failed'),
'success': None,
'failed': ('retry',),
'retry': ('pending', 'retry'),
}
state = models.CharField(max_length=30, choices=CHOICES, default='created')
def on_change_state(self, previous_state, next_state, **kwargs):
self.save()
Django Rest Framework
If you are using serializers, they usually perform the save, so saving inside on_change_state is not necessary.
One simple solution is to do this:
class MySerializer(serializers.ModelSerializer):
def update(self, instance, validated_data):
new_state = validated_data.get('state', instance.state)
try:
instance.change_state(new_state)
except fsm.InvalidTransition:
raise serializers.ValidationError("Invalid transition")
instance = super().update(instance, validated_data)
return instance
Documentation
Development
To run the tests run:
tox
Note, to combine the coverage data from all the tox environments run:
Windows |
set PYTEST_ADDOPTS=--cov-append tox |
|---|---|
Other |
PYTEST_ADDOPTS=--cov-append tox |
Metadata
Release files for fsmpy 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fsmpy-2.1.0.tar.gz | 6.0 kB | Without provenance | Details for fsmpy-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fsmpy-2.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for fsmpy-2.1.0-py2.py3-none-any.whl |
Total release size: 11.8 kB
Release files / fsmpy-2.1.0.tar.gz
| Download URL | fsmpy-2.1.0.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be81b809d1ce4cf7647f4293897e9656e2e8b12f7f303f5b678ac921c3bbfb4d
|
|
BLAKE2b-256 checksum How to use checksums |
25b03f7229c149d2fcf6b93fb42d67e2e22ba023b1f4859b837e2b1d569117de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.7.6 Linux/5.4.0-72-generic
|
Release files / fsmpy-2.1.0-py2.py3-none-any.whl
| Download URL | fsmpy-2.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
fb16c0f5376afa868b1946c90205a2ff8fd877a7d8636e2574b5b6b6823fdd99
|
|
BLAKE2b-256 checksum How to use checksums |
51e0f579621301175dfe016dcb7678e83a09ca58e8bb96882d05094d37d78454
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.6 CPython/3.7.6 Linux/5.4.0-72-generic
|
Security for fsmpy 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.