Fork me on GitHub
Release:
Date:
1.3
Feb 14, 2012
Flattr Mktoc

Table Of Contents

Download

Get latest source archive,
mktoc-1.3.tar.gz, or install with:

pip install mktoc --upgrade --user

Found a Bug?

Fill out a report on the issue tracker.

This Page

3.4. mktoc.fsm

A finite state machine implementation.

exception mktoc.fsm.NullStateException[source]

Bases: exceptions.Exception

Exception thrown when StateMachine has no state defined to handle the current input data.

class mktoc.fsm.StateMachine[source]

Bases: object

Base class for building a finite state machine.

The state machine is controled by two externally provided data structures.

regex_obj

A compiled regex pattern instance that contains one or more named groups (i.e. (?P<name>...)) sub-patterns.

match_handlers

A dict that maps a handler function to a group name returned by a successful match in the regex_obj instance.

Warning

The change_state() method must be run to initialize the state machine before the StateMachine instance is called.

__call__(lines)[source]

For each line of text in lines, a regex match is performed and a state handler function is called to finalize processing of the string.

Parameters:lines (list) – Input data consumed by state machine
change_state(regex_obj=None, match_handlers=None)[source]

Modifies the control flow of the state machine.

Replaces the internal regex_obj or match_handlers. Using keywords, one paramter can be changed without effecting the other.

Parameters:
  • regex_obj (regex) – Compiled regular expression object
  • match_handlers (dict of callable()‘s) – handler functions map, keyed with regex_obj group name.