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_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),
    ]

    FsmHandler(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.2.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_fsm_handler-0.0.2.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

Hashes for python_fsm_handler-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b3d62db4022d0de5d046581863635c0bcdfdd06c2c8c2fc38e7782cefc6a2e37
MD5 a54f3964fc76b639bbda1d29b073ea3f
BLAKE2b-256 6e5e905951af14ef1ecabf73714c89245cdc55b333c0f8ea82f7ad487925b62b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_fsm_handler-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f62bec175ae3ba1d0c827c2b36e99393a184f30c0e5a7d0e4fcfb2028882250a
MD5 43e9410fdcd269d276f76e6810134f97
BLAKE2b-256 a7b10046863c8f8d7bbf26f1b8853054e04504b2e7347229dc5f01cdec5f773b

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