Belle II Software development
State Class Reference

Public Member Functions

 __init__ (self, name, enter=None, exit=None)
 
 on_enter (self)
 
 on_enter (self, callbacks)
 
 on_exit (self)
 
 on_exit (self, callbacks)
 
 __str__ (self)
 
 __repr__ (self)
 
 __eq__ (self, other)
 
 __hash__ (self)
 

Public Attributes

 name = name
 Name of the State.
 
 on_enter = enter
 Callback list when entering state.
 
 on_exit = exit
 Callback list when exiting state.
 

Protected Member Functions

 _add_callbacks (self, callback, attribute)
 

Protected Attributes

list _on_enter = []
 set state as empty list when entering it
 
list _on_exit = []
 set state as empty list when exiting it
 

Detailed Description

Basic State object that can take enter and exit state methods and records
the state of a machine.

You should assign the self.on_enter or self.on_exit attributes to callback functions
or lists of them, if you need them.

Definition at line 41 of file state_machines.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
name,
enter = None,
exit = None )
Initialise State with a name and optional lists of callbacks.

Definition at line 50 of file state_machines.py.

50 def __init__(self, name, enter=None, exit=None):
51 """
52 Initialise State with a name and optional lists of callbacks.
53 """
54
55 self.name = name
56
57 self.on_enter = enter
58
59 self.on_exit = exit
60

Member Function Documentation

◆ __eq__()

__eq__ ( self,
other )
 

Definition at line 130 of file state_machines.py.

130 def __eq__(self, other):
131 """
132 """
133 if isinstance(other, str):
134 return self.name == other
135 else:
136 return self.name == other.name
137

◆ __hash__()

__hash__ ( self)
 

Definition at line 138 of file state_machines.py.

138 def __hash__(self):
139 """
140 """
141 return hash(self.name)
142
143

◆ __repr__()

__repr__ ( self)
 

Definition at line 125 of file state_machines.py.

125 def __repr__(self):
126 """
127 """
128 return f"State(name={self.name})"
129

◆ __str__()

__str__ ( self)
 

Definition at line 120 of file state_machines.py.

120 def __str__(self):
121 """
122 """
123 return self.name
124

◆ _add_callbacks()

_add_callbacks ( self,
callback,
attribute )
protected
Adds callback to a property.

Definition at line 94 of file state_machines.py.

94 def _add_callbacks(self, callback, attribute):
95 """
96 Adds callback to a property.
97 """
98 if callable(callback):
99 attribute.append(callback)
100 else:
101 B2ERROR(f"Something other than a function (callable) passed into State {self.name}.")
102

◆ on_enter() [1/2]

on_enter ( self)
Runs callbacks when a state is entered.

Definition at line 62 of file state_machines.py.

62 def on_enter(self):
63 """
64 Runs callbacks when a state is entered.
65 """
66 return self._on_enter
67

◆ on_enter() [2/2]

on_enter ( self,
callbacks )
 

Definition at line 69 of file state_machines.py.

69 def on_enter(self, callbacks):
70 """
71 """
72
73 self._on_enter = []
74 if callbacks:
75 self._add_callbacks(callbacks, self._on_enter)
76

◆ on_exit() [1/2]

on_exit ( self)
Runs callbacks when a state is exited.

Definition at line 78 of file state_machines.py.

78 def on_exit(self):
79 """
80 Runs callbacks when a state is exited.
81 """
82 return self._on_exit
83

◆ on_exit() [2/2]

on_exit ( self,
callbacks )
 

Definition at line 85 of file state_machines.py.

85 def on_exit(self, callbacks):
86 """
87 """
88
89 self._on_exit = []
90 if callbacks:
91 self._add_callbacks(callbacks, self._on_exit)
92

Member Data Documentation

◆ _on_enter

_on_enter = []
protected

set state as empty list when entering it

Definition at line 73 of file state_machines.py.

◆ _on_exit

_on_exit = []
protected

set state as empty list when exiting it

Definition at line 89 of file state_machines.py.

◆ name

name = name

Name of the State.

Definition at line 55 of file state_machines.py.

◆ on_enter

on_enter = enter

Callback list when entering state.

Definition at line 57 of file state_machines.py.

◆ on_exit

on_exit = exit

Callback list when exiting state.

Definition at line 59 of file state_machines.py.


The documentation for this class was generated from the following file: