Skip to main content

Minimal state machine

Project description

docs

Documentation Status

tests

Travis-CI Build Status
Coverage Status

package

PyPI Package latest release PyPI Wheel Supported versions Supported implementations

Minimal state machine

  • Free software: BSD license

Usage

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')
---------------------------------------------------------------------------
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

There are hooks that can be included before a state transition happens and after.

fsm will look for these functions

pre_<state_name>
post_<state_name>

And will give them any extra argument given to change_state

E.g:

Running m.change_state('pending', name='john') will trigger pre_pending(name='john')

Installation

pip install fsmpy

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()

Documentation

https://pyfsm.readthedocs.org/

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

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

fsmpy-2.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

fsmpy-2.0.0-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fsmpy-2.0.0.tar.gz.

File metadata

  • Download URL: fsmpy-2.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.11.4 CPython/3.6.5 Linux/4.15.0-30-generic

File hashes

Hashes for fsmpy-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3b23e2ffeb85a344e8650b0c7c7be04ff421defc81f30bb737cc17baeeeb4b3c
MD5 75c1f1f00b4825837c786ad5f0d6f1a5
BLAKE2b-256 8181a4f33a4865bf8689ccd272c455e97d718e57a4447db3fcd10cdc2ff2a5db

See more details on using hashes here.

File details

Details for the file fsmpy-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: fsmpy-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.11.4 CPython/3.6.5 Linux/4.15.0-30-generic

File hashes

Hashes for fsmpy-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 86efb4c5ac9ec1af20270ac9abb8408c2327222672119026c64e399a4a652608
MD5 cdba622913b907369d6da47995e6be2f
BLAKE2b-256 eebe80c9e6db810fc3e9dc7ed0d78d4491ac456341f8111e2f6f3319c314448b

See more details on using hashes here.

Supported by

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