Skip to main content

Basic Finite State Machine (FSM) tools.

Project description

Basic Finite State Machine (FSM) tools.

Latest release 20230816.3: Bump cs.gvutils requirement.

Class FSM(cs.gvutils.DOTNodeMixin)

Base class for a finite state machine (FSM).

The allowed states and transitions are defined by the class attribute FSM_TRANSITIONS, a mapping of state->event->new_state.

Each instance has the following attributes:

  • fsm_state: the current state value.
  • fsm_history: an optional iterable of FSMTransitionEvent state transitions recorded by the fsm_event method. Usually this would be None (the default) or a list.

Method FSM.__init__(self, state=None, *, history=None, lock=None, transitions=None): Initialise the FSM from:

  • state: optional positional parameter for the initial state, default self.FSM_DEFAULT_STATE
  • history: an optional object to record state transition history, default None; if not None this should be an iterable object with a .append(entry) method such as a list.
  • lock: an optional mutex to control access; if presupplied and shared with the caller it should probably be an RLock; the default is a Lock, which is enough for FSM private use
  • transitions: optional state->event->state mapping; if provided, this will override the class FSM_TRANSITIONS mapping

Note that the FSM base class does not provide a FSM_DEFAULT_STATE attribute; a default state value of None will leave .fsm_state unset.

This behaviour is is chosen mostly to support subclasses with unusual behaviour, particularly Django's Model class whose refresh_from_db method seems to not refresh fields which already exist, and setting .fsm_state from a FSM_DEFAULT_STATE class attribute thus breaks this method. Subclasses of this class and Model should not provide a FSM_DEFAULT_STATE attribute, instead relying on the field definition to provide this default in the usual way.

Class FSMError(builtins.Exception, builtins.BaseException)

An exception associated with an FSM.

These have a .fsm attribute storing an (optional) FSM reference supplied at initialisation.

FSMSubType = ~FSMSubType

Type variable.

Usage::

T = TypeVar('T') # Can be anything A = TypeVar('A', str, bytes) # Must be str or bytes

Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function definitions. See class Generic for more information on generic types. Generic functions work as follows:

def repeat(x: T, n: int) -> List[T]: '''Return a list containing n references to x.''' return [x]*n

def longest(x: A, y: A) -> A: '''Return the longest of two strings.''' return x if len(x) >= len(y) else y

The latter example's signature is essentially the overloading of (str, str) -> str and (bytes, bytes) -> bytes. Also note that if the arguments are instances of some subclass of str, the return type is still plain str.

At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.

Type variables defined with covariant=True or contravariant=True can be used to declare covariant or contravariant generic types. See PEP 484 for more details. By default generic types are invariant in all type variables.

Type variables can be introspected. e.g.:

T.name == 'T' T.constraints == () T.covariant == False T.contravariant = False A.constraints == (str, bytes)

Note that only type variables defined in global scope can be pickled.

Class FSMTransitionEvent(builtins.tuple)

FSMTransitionEvent(old_state, new_state, event, when, extra)

Method FSMTransitionEvent.__new__(_cls, old_state, new_state, event, when, extra): Create new instance of FSMTransitionEvent(old_state, new_state, event, when, extra)

Release Log

Release 20230816.3: Bump cs.gvutils requirement.

Release 20230816.2: FSM.fsm_transitions_as_dot: bugfix: the style needs "style=filled" as well as the fillcolor.

Release 20230816.1: FSM.fsm_transitions_as_dot: now an instance method so that we can colour the current state.

Release 20230816: FSM: new fsm_as_svg method and fsm_svg property.

Release 20221118:

  • FSM.init: make state optional, default from self.FSM_DEFAULT_STATE - now all args are optional.
  • FSM.init: if the state is None or not supplied, do not set .fsm_state at all; add explaination for this weird design choice.
  • FSM.getattr: only generate event methods for events with public names (no leading underscore).
  • FSM: new .fsm_history property, aiding subclassing elsewhere.
  • FSM: drop dot_node_fillcolor, now provided by DOTNodeMixin.getattr, provide dot_node_palette_key using self.fsm_state.
  • FSM.dot_node_attrs: color from self.dot_node_color.

Release 20220918: Replace callback exception warning() with exception() for the traceback.

Release 20220805.1:

  • FSM: subclass DOTNodeMixin and provide a hook for a colour palette for node fillcolors.
  • Other minor changes.

Release 20220805: Initial PyPI release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.fsm-20230816.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs.fsm-20230816.3-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file cs.fsm-20230816.3.tar.gz.

File metadata

  • Download URL: cs.fsm-20230816.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for cs.fsm-20230816.3.tar.gz
Algorithm Hash digest
SHA256 cc4f3173615a3a9506ce5b1625972a03fc01216a7dc11ea430d08eb62cafd45e
MD5 6b30df4dc07cce58646b61b2dbc6872d
BLAKE2b-256 d5e61be447ba317220836450c6b1e45ba49b3003e9a329c28beee4146e841906

See more details on using hashes here.

File details

Details for the file cs.fsm-20230816.3-py3-none-any.whl.

File metadata

  • Download URL: cs.fsm-20230816.3-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for cs.fsm-20230816.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0f0bc5a7775df455b2270705a021972396e26432aca08f12fdd29e100dd5fead
MD5 37c4b8539060d346c7af9f4f7e891adb
BLAKE2b-256 f7f095db4e554aec337f4ca00c8610917af362a4ffc2b76f7bdb85346e5f0383

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page