Python FSM
This package contains a set of classes to handle a Finite State Machine (FSM).
Example usage
from python_fsm_handler.StateHandler import FsmHandler, StateLink, StateObject
from enum import Enum
moveCounter = 0
workCounter = 0
class FsmState(Enum):
MOVE = 0
WORK = 1
def test():
def movePre():
print("move pre")
def moveExec():
print("move exec")
global moveCounter
moveCounter += 1
def movePost():
print("move post")
global moveCounter
moveCounter = 0
def workPre():
print("work pre")
def workExec():
print("work exec")
global workCounter
workCounter += 1
def workPost():
print("work post")
global workCounter
workCounter = 0
def loopCallback(state):
print(f"loopCallback: {state}")
print(f"moveCounter: {moveCounter}")
print(f"workCounter: {workCounter}")
def moveToWork():
return moveCounter >= 5
def workToMove():
return workCounter >= 5
states = [
StateObject(FsmState.MOVE, [
StateLink(FsmState.WORK, moveToWork)
], movePre, moveExec, movePost),
StateObject(FsmState.WORK, [
StateLink(FsmState.MOVE, workToMove)
], workPre, workExec, workPost),
]
fsm = FsmHandler(states, FsmState.MOVE, logger=print, loopCallback=loopCallback)
fsm.run()
if __name__ == "__main__":
test()
This code will loop an FSM infinetely back and forth between two states. Running the code you may notice that the log in the loop callback will never print counter = 5 even if the 5 is reached. This is because the execution order in the state handler is as follow:
- exec current
- check condition for transition and if it's true change state
- exec loop callback So as you can see, the transition is happened, so the post condition reset the counter to 0 before printing in the loop callback
Metadata
Release files for python-fsm-handler 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_fsm_handler-0.0.3.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_fsm_handler-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.7 kB
Release files / python_fsm_handler-0.0.3.tar.gz
| Download URL | python_fsm_handler-0.0.3.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd385e2d1f08d3478aa576742d2b261858bf433559ff2d97942527c828e42ea5
|
|
BLAKE2b-256 checksum How to use checksums |
9bbe86ad05cab4d129eda91d685cc3ec264b488bd0d25195301968be64f9f7b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|
Release files / python_fsm_handler-0.0.3-py3-none-any.whl
| Download URL | python_fsm_handler-0.0.3-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6aa54b97b35acbe210fa113c4a6071203c36e3294e35ba33e504e701441e0da6
|
|
BLAKE2b-256 checksum How to use checksums |
bf5387b0641500567e03c75a65b3086846b87e033123d18f1f599d1563a3e19d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|