Skip to main content

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_creminem94.StateHandler import StateHandler, StateLink, RobotState
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 = [
        RobotState(FsmState.MOVE, [
            StateLink(FsmState.WORK, moveToWork)
        ], movePre, moveExec, movePost),
        RobotState(FsmState.WORK, [
            StateLink(FsmState.MOVE, workToMove)
        ], workPre, workExec, workPost),
    ]

    stateHandler = StateHandler(states, FsmState.MOVE, logger=print, loopCallback=loopCallback)


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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_fsm_handler-0.0.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

python_fsm_handler-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file python_fsm_handler-0.0.1.tar.gz.

File metadata

  • Download URL: python_fsm_handler-0.0.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for python_fsm_handler-0.0.1.tar.gz
Algorithm Hash digest
SHA256 942d91db88462d4a40b47346bf770731c9eb02d05c825a81a5575bb46fac834e
MD5 bafabd61ddeb16098f27d56d0b780964
BLAKE2b-256 a8d978ada0e766898dc2a214c860209e3c6963805ebea63c15ac50012d107cd3

See more details on using hashes here.

File details

Details for the file python_fsm_handler-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_fsm_handler-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2945fc89b53b2622f76577eb369b64ace223cac8a5eadf114e3d543548d0a89b
MD5 a7e2432d682ac0e9007ccd67f531c4a7
BLAKE2b-256 2a880b9b3b5112c81994b909280f92bbe313eb27ad39d1d43c97d9b81ff1a4bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page