Revision 8 as of 2009-10-22 20:07:37

Clear message

This is a summary of FSM implementations in Python right now.

For general information about finite state machines, see:


Finite State Machine Editor

This is a tool where you can draw FSM diagrams, and then compile to a Python module (or C++ code.) It also makes an XML description of the FSM.

Requires QT for the editor. (Not the compiler, though, which probably reads XML.)

* tutorial * project wiki

FSA - Finite State Automation in Python

This seems to be all about making FSMs, but I don't see a whole lot on how to use them!

Noah Spurrier's FSM

This is one found on ActiveState; It's purely Python code. You init an FSM, register transitions, and then throw inputs at it. States and inputs must be hashable.

You can also find it on his website.

fsmpy module

This seems to be a Python wrapper around AT&T's FSM library. It's all oriented around "weighted" finite state machines, so I'm not so sure how suitable it is if you just want to use unweighted FSM.

Decorator based FSM

A decorator based FSM can be found in the Decorator Library on this site. The module simplifies implementaion of FSM's based on UML 2.0 state diagrams. The FSM is implemented as a class, with methods of the class associated with transitions or with states. The design is not the best for constructing FSMs to parse text being somewhat slower than alternatives.

SkipMontanaro's FSM

From an e-mail.

Features transition actions.

I've re-interpreted the code, adding formatting lost in e-mail, and PythonStyle-itizing it a bit.

   1 """Finite state machine, featuring transition actions."""
   3 FSMError = 'Invalid input to finite state machine'
   5 class FSM:
   7     """Finite state machine, featuring transition actions.
   9     The class stores a dictionary of (state, input) keys,
  10     and (state, action) values.
  12     When a (state, input) search is performed:
  13     * an exact match is checked first,
  14     * (state, None) is checked next.
  16     The action is of the following form:
  17     * function(current_state, input)
  18     """
  20     def __init__(self):
  21         self.states = {}
  22         self.state = None
  23         self.dbg = None
  25     def add(self, state, input, newstate, action):
  26         """Add a transition to the FSM."""
  27         self.states[(state, input)] = (newstate, action)
  29     def execute(self, input):
  30         """Perform a transition and execute action."""
  31         si = (self.state, input)
  32         sn = (self.state, None)
  33         # exact state match?
  34         if si in self.states:
  35             newstate, action = self.states[si]
  36         # no, how about a None (catch-all) match?
  37         elif sn in self.states:
  38             newstate, action = self.states[sn]
  39         if self.dbg != None:
  40             self.dbg.write('State: %s / Input: %s /'
  41                            'Next State: %s / Action: %s\n' %
  42                            (self.state, input, newstate, action))
  43         action(self.state, input)
  44         self.state = newstate
  46     def start(self, state):
  47         """Define the start state.
  49         Actually, this just resets the current state.
  50         """
  51         self.state = state
  53     def debug(self, out):
  54         """Assign a writable file to log transitions."""
  55         self.dbg = out

Unable to edit the page? See the FrontPage for instructions.