Skip to content

Simple Finite State Machine implementation in Python 3

License

Notifications You must be signed in to change notification settings

nourspace/pyfsm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Finite State Machine

Simple Finite State Machine implementation in Python 3.

Installation

pip install git+https://github.com/nourchawich/pyfsm

Usage

from pyfsm import FSM

# Initialise
fsm = FSM(initial='new', transitions=[
    {'name': 'age', 'src': 'new', 'dst': 'old'},
    {'name': 'renew', 'src': 'old', 'dst': 'new'},
    {'name': 'expire', 'src': 'old', 'dst': 'expired'},
])

# Get current state
print(fsm.current)  # new

# Apply transition
fsm.apply('age')
print(fsm.current)  # old

fsm.apply('age')  # Raises FSMError if the transition can not be applied

# Check whether a transition can be applied
fsm.apply('expire')
print(fsm.current)  # expired
print(fsm.can('renew'))  # False

Running Tests

python -m unittest tests

Todo

  • Identify final state.
  • Add callbacks.
    • Allow passing arguments when applying transitions.
  • Print transitions/states graph.
  • Record transitions history.

About

Simple Finite State Machine implementation in Python 3

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages