Skip to main content

Overview

Minimal state machine

  • Free software: BSD license

Usage

import fsm

class MyModel(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 current_state(self):
        """Overriden."""
        return self.state

    def on_before_pending(self):
        print("I'm going to a pending state")
In [4]: m = MyModel(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

Documentation

https://pyfsm.readthedocs.org/

Development

To run the all 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

Changelog

1.0.0 (2018-07-03)

  • Renamed hooks to pre_<state_name> and post_<state_name>

0.1.3 (2017-15-09)

  • Updated docs

  • Corrections to code

  • ci updated

0.1.0 (2016-04-18)

  • First release on PyPI.

Metadata

Release files for fsmpy 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fsmpy 1.0.1
File Size Uploaded Provenance Actions
fsmpy-1.0.1.tar.gz 13.5 kB Without provenance Details for fsmpy-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fsmpy 1.0.1
File Interpreter ABI Platform Actions
fsmpy-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details for fsmpy-1.0.1-py2.py3-none-any.whl

Total release size: 17.5 kB

Release files / fsmpy-1.0.1.tar.gz

Download URL fsmpy-1.0.1.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
df4fbff7e52184ff9e35f76dfd1d06e0584b52f732c4c2020a4f0de3cf2476d3
BLAKE2b-256 checksum
How to use checksums
ec626ba9d265920bcfffbf23fa91800366e7acc7af8294a82b313ecbb440b084
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / fsmpy-1.0.1-py2.py3-none-any.whl

Download URL fsmpy-1.0.1-py2.py3-none-any.whl
Size 4.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
466ada233bfb7d01e9065b4060cfc577e74db61ca60ca7437f237d7feb85b26d
BLAKE2b-256 checksum
How to use checksums
1dcd2ca7ad8ca358261e030b79428fe007f5070ad9de343e38bb968895a1e817
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for fsmpy 1.0.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page