Skip to main content

Pythonic FSM

Project description

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.

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

simple_fsm-0.0.3.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_fsm-0.0.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_fsm-0.0.3.tar.gz.

File metadata

  • Download URL: simple_fsm-0.0.3.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for simple_fsm-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c3c61a4dcb10d713585f791bce2096c8392592e5cb02b40dfc2ea214ed14d75b
MD5 51842cac1e3dcf800ec1a64447ac49ef
BLAKE2b-256 326d94af5860e2c7e09185bd1b2aa4c514599c8021bbbd2d27f0542a1197e6bf

See more details on using hashes here.

File details

Details for the file simple_fsm-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: simple_fsm-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for simple_fsm-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 23a3ef62e4feef201a1492ba54597e79e28723bb57f380aa2277b32c147c88c1
MD5 0f676b279181262518cda3696e4b88a6
BLAKE2b-256 341b28cfd03ec24fa87b0359665c5092131f95467e2add6a3ed1493a6b4e5ca9

See more details on using hashes here.

Supported by

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