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)
 
 _ (self, callbacks, 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

◆ _()

_ ( self,
callbacks,
attribute )
protected
Alternate method for lists and tuples of function objects.

Definition at line 105 of file state_machines.py.

105 def _(self, callbacks, attribute):
106 """
107 Alternate method for lists and tuples of function objects.
108 """
109 if callbacks:
110 for function in callbacks:
111 if callable(function):
112 attribute.append(function)
113 else:
114 B2ERROR(f"Something other than a function (callable) passed into State {self.name}.")
115

◆ __eq__()

__eq__ ( self,
other )
 

Definition at line 126 of file state_machines.py.

126 def __eq__(self, other):
127 """
128 """
129 if isinstance(other, str):
130 return self.name == other
131 else:
132 return self.name == other.name
133

◆ __hash__()

__hash__ ( self)
 

Definition at line 134 of file state_machines.py.

134 def __hash__(self):
135 """
136 """
137 return hash(self.name)
138
139

◆ __repr__()

__repr__ ( self)
 

Definition at line 121 of file state_machines.py.

121 def __repr__(self):
122 """
123 """
124 return f"State(name={self.name})"
125

◆ __str__()

__str__ ( self)
 

Definition at line 116 of file state_machines.py.

116 def __str__(self):
117 """
118 """
119 return self.name
120

◆ _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: