Class to handle Finite State Machines
Project description
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
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file python_fsm_handler-0.0.3.tar.gz
.
File metadata
- Download URL: python_fsm_handler-0.0.3.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
bd385e2d1f08d3478aa576742d2b261858bf433559ff2d97942527c828e42ea5
|
|
MD5 |
c515cec10ff81659e6a6069e4dbac4a5
|
|
BLAKE2b-256 |
9bbe86ad05cab4d129eda91d685cc3ec264b488bd0d25195301968be64f9f7b8
|
File details
Details for the file python_fsm_handler-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: python_fsm_handler-0.0.3-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
6aa54b97b35acbe210fa113c4a6071203c36e3294e35ba33e504e701441e0da6
|
|
MD5 |
ecb9c534781e9f90c0265d6f35a28ce0
|
|
BLAKE2b-256 |
bf5387b0641500567e03c75a65b3086846b87e033123d18f1f599d1563a3e19d
|