Finite State Machine [ FSM ]
A simple 'pythonic' FSM implementation. Decorate your actions with state transition.
Create an instance of an FSM using state transition table. Push events into event queue.
Process state machine using next generator.
State Transition Table
Decorate your actions with state transitions. Let's say you have a simple two state machine and one event that can between both states.
import fsm
table = fsm.Table()
@table('STATE1', 'doit', 'STATE2')
def action1(*args, **kwargs):
pass
@table('STATE2', 'doit', 'STATE1')
def action2(*args, **kwargs):
pass
print(table.dump())
OUTPUT
STATE1 --doit--> STATE2
STATE2 --doit--> STATE1
FSM Instance
Define an instance of your FSM using the state transition table and an initial state.
To execute your state machine, you push events to your event queue and process via the
next method.
myfsm = fsm.FSM(table, 'STATE1')
myfsm.push('doit')
myfsm.push('doit')
for _ in myfsm.next():
print(myfsm.state)
OUTPUT
STATE2
STATE1
Since next is a generator, you will need to process via generator syntax. Doing it
this way permits events to be pushed at any point into the FSM.
Actions
The action fn is called for each state transition. Sometimes you'll want to pass annotated
data through the FSM. The next method provides that with *args, **kwargs. You may
push an event with custom **kwargs data which will be merged into the action call
**kwargs. Finally, every action called will have an transition_FSM passed in as kwargs
as well.
Async
Use fsm.AsyncFSM to drive coroutine actions. It behaves exactly like fsm.FSM,
except next is an async generator that awaits each action, so you process it with
async for.
import asyncio
import fsm
table = fsm.Table()
@table('STATE1', 'doit', 'STATE2')
async def action1(*args, **kwargs):
pass
@table('STATE2', 'doit', 'STATE1')
async def action2(*args, **kwargs):
pass
async def main():
myfsm = fsm.AsyncFSM(table, 'STATE1')
myfsm.push('doit')
myfsm.push('doit')
async for _ in myfsm.next():
print(myfsm.state)
asyncio.run(main())
OUTPUT
STATE2
STATE1
Actions may be either coroutines or plain functions — AsyncFSM awaits any awaitable
result and uses the rest as-is, so you can freely mix sync and async actions in the same
table.
Metadata
Release files for simple-fsm 0.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 |
|---|---|---|---|---|
| simple_fsm-0.1.0.tar.gz | 5.5 kB | Without provenance | Details for simple_fsm-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simple_fsm-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for simple_fsm-0.1.0-py3-none-any.whl |
Total release size: 9.8 kB
Release files / simple_fsm-0.1.0.tar.gz
| Download URL | simple_fsm-0.1.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
811094d4838f17dde5aaeb0a4a3347cbb5e1ab5ee23dc5d42c39c611c0957194
|
|
BLAKE2b-256 checksum How to use checksums |
392bca6b46d32f964d71eb46b5d8f2cc790079944a06a59d8174f86e42aaf220
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Release files / simple_fsm-0.1.0-py3-none-any.whl
| Download URL | simple_fsm-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e04ac902fa5775e274dcdcb40ff65e02bd0f76125581579f4fba7d27aab38224
|
|
BLAKE2b-256 checksum How to use checksums |
86a5835340df786a6ec6b7afe7498ee3ffd839a8f9323c413d9b05b77a8bf502
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.14
|
Security for simple-fsm 0.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.