3.4. mktoc.fsm¶
A finite state machine implementation.
-
exception
mktoc.fsm.NullStateException[source]¶ Bases:
exceptions.ExceptionException thrown when
StateMachinehas no state defined to handle the current input data.
-
class
mktoc.fsm.StateMachine[source]¶ Bases:
objectBase class for building a finite state machine.
The state machine is controled by two externally provided data structures.
-
regex_obj¶ A compiled
regexpattern instance that contains one or more named groups (i.e.(?P<name>...)) sub-patterns.
-
match_handlers¶ A
dictthat maps a handler function to a group name returned by a successful match in theregex_objinstance.
Warning
The
change_state()method must be run to initialize the state machine before theStateMachineinstance 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_objormatch_handlers. Using keywords, one paramter can be changed without effecting the other.Parameters: - regex_obj (regex) – Compiled regular expression object
- match_handlers (
dictofcallable()‘s) – handler functions map, keyed withregex_objgroup name.
-