General purpose toolbox
Project description
Finite State Machine
Lightweight, decorator-based Python implementation of a Finite State Machine.
Installation
pip install finite-state-machine
Usage
You will need to subclass StateMachine
and set the state
instance variable.
The transition
decorator can be used to specify valid transitions.
Example
from finite_state_machine import StateMachine, transition
class Turnstile(StateMachine):
initial_state = "close"
def __init__(self):
self.state = self.initial_state
@transition(source=["close", "open"], target="open")
def insert_coin(self):
pass
@transition(source="open", target="close")
def pass_thru(self):
pass
REPL
In [2]: turnstile = Turnstile()
In [3]: turnstile.state
Out[3]: 'close'
In [4]: turnstile.insert_coin()
In [5]: turnstile.state
Out[5]: 'open'
In [6]: turnstile.insert_coin()
In [7]: turnstile.state
Out[7]: 'open'
In [8]: turnstile.pass_thru()
In [9]: turnstile.state
Out[9]: 'close'
In [10]: turnstile.pass_thru()
---------------------------------------------------------------------------
InvalidStartState Traceback (most recent call last)
<ipython-input-10-6abc6f4be1cd> in <module>
----> 1 turnstile.pass_thru()
~/state_machine.py in _wrapper(*args, **kwargs)
32
33 if self.state not in source:
---> 34 raise InvalidStartState
35
36 for condition in conditions:
InvalidStartState:
Inspiration
This project is inspired by django-fsm. Wanted a decorator-based state machine without having to use Django.
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
Close
Hashes for finite-state-machine-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8f4dc646a880fd55971f999f9d64cc325da5766574638d59f842503da6fdf8d |
|
MD5 | ffbc37ee6842387e6a8ece0e492e8fe1 |
|
BLAKE2b-256 | 038742072347672f4766de9ce739bb3f979c1b812d0bb4448d6c215cd75b3d3e |
Close
Hashes for finite_state_machine-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fef9f8ee4c2e488e7412a54c54ae7c6b12776e5a5d7d81d0ddb767ce736b3ed |
|
MD5 | ca73aa3f81a49f30935c24536ddd7496 |
|
BLAKE2b-256 | ef60dab5cc810ac96fdd5f05085cb35e6c3a92871fbe0552cd0cb59f0c1b2da2 |