creek.automatas#

Automatas (finite state machines): step a state through a stream of symbols.

Main entry points:

  • mapping_to_transition_func: a transition function from a {(state, symbol): next_state} mapping

  • basic_automata: basic_automata(transition_func, state)(symbols) yields the state after each symbol

  • BasicAutomata: the same as a stateful, resettable object

>>> from creek.automatas import mapping_to_transition_func, basic_automata
>>> step = mapping_to_transition_func({('a', 1): 'b', ('b', 1): 'a'})
>>> list(basic_automata(step, 'a')([1, 1, 1]))
['b', 'a', 'b']

Functions

automata(transition_func, state)

Make an automata: basic_automata(f, state)(symbols) yields the state after each symbol.

basic_automata(transition_func, state)

Make an automata: basic_automata(f, state)(symbols) yields the state after each symbol.

mapping_to_transition_func(mapping[, strict])

Make a transition function from a {(state, symbol): next_state} mapping.

Classes

BasicAutomata(transition_func[, state])

Stateful automata: automata(state, symbols) yields the state after each symbol; reset restores the initial one.

MappingTransitionFunc(mapping[, strict])

Class form of mapping_to_transition_func: a callable (state, symbol) -> next_state.

class creek.automatas.BasicAutomata(transition_func, state=None)[source]#

Bases: object

Stateful automata: automata(state, symbols) yields the state after each symbol; reset restores the initial one.

reset()[source]#

Reset state to initial state and return self.

transition(symbol)[source]#

Apply one symbol to the current state and return the new state.

Return type:

TypeVar(State)

class creek.automatas.MappingTransitionFunc(mapping, strict=True)[source]#

Bases: object

Class form of mapping_to_transition_func: a callable (state, symbol) -> next_state.

map_states(state_mapper)[source]#

Return a new MappingTransitionFunc with the same mapping but with state_mapper applied to the states.

Return type:

MappingTransitionFunc

creek.automatas.automata(transition_func, state)#

Make an automata: basic_automata(f, state)(symbols) yields the state after each symbol.

Return type:

Callable[[TypeVar(State), Iterable[TypeVar(Symbol)]], Iterable[TypeVar(State)]]

creek.automatas.basic_automata(transition_func, state)[source]#

Make an automata: basic_automata(f, state)(symbols) yields the state after each symbol.

Return type:

Callable[[TypeVar(State), Iterable[TypeVar(Symbol)]], Iterable[TypeVar(State)]]

creek.automatas.mapping_to_transition_func(mapping, strict=True)[source]#

Make a transition function from a {(state, symbol): next_state} mapping.

Parameters:
  • mapping (Mapping[tuple[TypeVar(State), TypeVar(Symbol)], TypeVar(State)]) – The transitions.

  • strict (bool) – If true (the default), an unmapped (state, symbol) pair is a lookup error (KeyError for a dict); if false, the state is left unchanged.

Return type:

Callable[[TypeVar(State), TypeVar(Symbol)], TypeVar(State)]