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

Uploaded Source

Built Distribution

simple_fsm-0.0.2-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file simple-fsm-0.0.2.tar.gz.

File metadata

  • Download URL: simple-fsm-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for simple-fsm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 398d9ddea4ebb542f10f20157f5e3a9b865662b124ce126ba702e4d0d48d0546
MD5 c41ed7647dba78e480f4e745f0fab14e
BLAKE2b-256 7921d453df7e6471aac1689493b31867114a27e2e05ae661a3cb6d61ae347a07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_fsm-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for simple_fsm-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 40a1ee4926c466f3d0437db77996189e5407033a0aa355c631a109a291f17234
MD5 8e987d4ca2e2ddd7e7f20d3ca78d7485
BLAKE2b-256 703d88b94c0b189c1587d3adc0e717e8df71e3346dbfcb01d6b550c12bcf40ec

See more details on using hashes here.

Supported by

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