Onyx logo

Previous topic

onyx.util.quantity – >>> True

Next topic

onyx.util.timedict – Simple storage for timing information.

This Page

onyx.util.state_machine – Configuration state machine

class onyx.util.state_machine.ConfigurationStateMachineMixin(*args, **kwargs)

Bases: object

Mixin class provides support for a state machine.

The mixin requires the subclass to have attribute CONFIGURATION_TRANSITIONS, a sequence of (config_name, (successor_config_name, ...)) that defines the legal transitions of the machine. It also requires subclass attribute CONFIGURATION_START, which is the starting configuration for the machine.

Provides methods set_configuration() and check_configuration() used to move through configurations and to verify being in one of a set of configurations. Provides property configuration, the current configuration.

>>> class TwoStates(ConfigurationStateMachineMixin):
...   RESET = 'reset'
...   ACTIVE = 'active'
...   PASSIVE = 'passive'
...   CONFIGURATION_TRANSITIONS = (
...       (RESET, (ACTIVE,)),
...       (ACTIVE, (PASSIVE, RESET)),
...       (PASSIVE, (RESET,)))
...   CONFIGURATION_START = RESET

Note: the following uses of ConfigurationStateMachineMixin methods and attributes would typically be done from within methods of the deriving class; but, for this testing, we do them explicity

>>> self = TwoStates()
>>> self.configuration
'reset'
>>> self.set_configuration(self.ACTIVE)
'reset'
>>> self.configuration
'active'
>>> self.check_configuration(self.ACTIVE, self.PASSIVE)
>>> self.set_configuration(self.PASSIVE)
'active'
>>> self.set_configuration(self.RESET)
'passive'
>>> self.set_configuration(self.ACTIVE)
'reset'
>>> self.set_configuration(self.RESET)
'active'

Errors

>>> self.check_configuration(self.ACTIVE, self.PASSIVE)
Traceback (most recent call last):
  ...
ValueError: TwoStates expected current configuration, 'reset', to be one of ('active', 'passive')
>>> self.set_configuration(self.PASSIVE)
Traceback (most recent call last):
  ...
ValueError: TwoStates in current configuration, 'reset', expected new configuration to be one of: ('active',), but got 'passive'
check_configuration(*configs)

Verify that the current configuration is one of configs.

Raises ValueError if the current configuration is not in configs.

configuration

The current configuration of the state machine.

set_configuration(configuration)

Switch the state machine to configuration. Returns the previous configuration.

Raises ValueError if the transition isn’t valid.