Package: cs.fsm
Basic Finite State Machine (FSM) tools.Latest release 20231020:
FSM.getattr: known transition names no longerfall through to the superclass if not valid for the current state.ClassFSM(cs.gvutils.DOTNodeMixin)Base class for a finite state machine (FSM).The allowed states and transitions are defined by the class
attributeFSM_TRANSITIONS, a mapping ofstate->event->new_state.Each instance has the following attributes:fsm_state: the current state value.fsm_history: an optional iterable ofFSMTransitionEventstate transitions recorded by thefsm_eventmethod.
Usually this would beNone(the default) or alist.MethodFSM.__init__(self, state=None, *, history=None, lock=None, transitions=None):
Initialise theFSMfrom:state: optionalpositionalparameter for the initial state,
defaultself.FSM_DEFAULT_STATEhistory: an optional object to record state transition
history, defaultNone; if notNonethis should be an
iterable object with a.append(entry)method such as alist.lock: an optional mutex to control access;
if presupplied and shared with the caller
it should probably be anRLock;
the default is aLock, which is enough forFSMprivate usetransitions: optionalstate->event->statemapping;
if provided, this will override the classFSM_TRANSITIONSmappingNote that theFSMbase class does not provide aFSM_DEFAULT_STATEattribute; a defaultstatevalue ofNonewill leave.fsm_stateunset.This behaviour is is chosen mostly to support subclasses
with unusual behaviour, particularly Django'sModelclass
whoserefresh_from_dbmethod seems to not refresh fields
which already exist, and setting.fsm_statefrom aFSM_DEFAULT_STATEclass attribute thus breaks this method.
Subclasses of this class andModelshouldnotprovide aFSM_DEFAULT_STATEattribute, instead relying on the field
definition to provide this default in the usual way.ClassFSMError(builtins.Exception, builtins.BaseException)An exception associated with anFSM.These have a.fsmattribute storing an (optional)FSMreference supplied at initialisation.FSMSubType = ~FSMSubTypeType variable.Usage::T = TypeVar('T')  # Can be anything
A = TypeVar('A', str, bytes)  # Must be str or bytesType 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]*ndef longest(x: A, y: A) -> A:
'''Return the longest of two strings.'''
return x if len(x) >= len(y) else yThe 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.ClassFSMTransitionEvent(builtins.tuple)FSMTransitionEvent(old_state, new_state, event, when, extra)MethodFSMTransitionEvent.__new__(_cls, old_state, new_state, event, when, extra):
Create new instance of FSMTransitionEvent(old_state, new_state, event, when, extra)Release LogRelease 20231020:
FSM.getattr: known transition names no longerfall through to the superclass if not valid for the current state.Release 20231018:FSM.fsm_transitions_as_dot: new optional history_style parameter to style transitioned edges.FSM.fsm_as_svg: plumb optional history_style parameter.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.