The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
GaudiKernel.GaudiHandles.GaudiHandleArray Class Reference
Inheritance diagram for GaudiKernel.GaudiHandles.GaudiHandleArray:
Collaboration diagram for GaudiKernel.GaudiHandles.GaudiHandleArray:

Public Member Functions

 __init__ (self, typesAndNames=None)
 __len__ (self)
 __eq__ (self, other)
 __repr__ (self)
 __str__ (self)
 __opt_value__ (self)
 __copy__ (self)
 __setitem__ (self, index, value)
 __getitem__ (self, index)
 __iter__ (self)
 __delitem__ (self, index)
 __contains__ (self, value)
 __add__ (self, other)
 index (self, value, *args)
 insert (self, index, value)
 isPublic (self)
 toStringProperty (self)

Static Public Attributes

 handleType = None

Protected Member Functions

 _indexToKey (self, index)

Protected Attributes

dict _items = {}

Static Private Attributes

tuple __slots__ = ("_items",)

Detailed Description

A unique list of GaudiHandles. Only handles of one type are allowed,
as specified by self.__class__.handleType

Definition at line 129 of file GaudiHandles.py.

Constructor & Destructor Documentation

◆ __init__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__init__ ( self,
typesAndNames = None )

Definition at line 136 of file GaudiHandles.py.

136 def __init__(self, typesAndNames=None):
137 # Use of dictionary as storage for faster duplicate finding
138 self._items = {} # name : handle
139
140 # check the type
141 if typesAndNames is None:
142 typesAndNames = []
143 elif not isinstance(typesAndNames, list):
144 raise TypeError(
145 f"Argument to {self.__class__.__name__} must be a list. Got a {type(typesAndNames).__name__} instead"
146 )
147 # add entries to list
148 self.extend(typesAndNames)
149

Member Function Documentation

◆ __add__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__add__ ( self,
other )

Definition at line 227 of file GaudiHandles.py.

227 def __add__(self, other):
228 return self.__class__(list(self) + list(other))
229

◆ __contains__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__contains__ ( self,
value )

Definition at line 221 of file GaudiHandles.py.

221 def __contains__(self, value):
222 if isinstance(value, str):
223 return value in self._items
224 else:
225 return value.getName() in self._items
226

◆ __copy__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__copy__ ( self)

Definition at line 180 of file GaudiHandles.py.

180 def __copy__(self):
181 inst = self.__class__.__new__(self.__class__)
182 inst._items = self._items.copy()
183 return inst
184

◆ __delitem__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__delitem__ ( self,
index )

Definition at line 214 of file GaudiHandles.py.

214 def __delitem__(self, index):
215 if isinstance(index, slice):
216 for k in self._indexToKey(index):
217 self._items.pop(k)
218 else:
219 self._items.pop(self._indexToKey(index))
220

◆ __eq__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__eq__ ( self,
other )

Definition at line 153 of file GaudiHandles.py.

153 def __eq__(self, other):
154 try:
155 return self._items == other._items
156 except AttributeError:
157 # other is e.g. plain list
158 return list(self._items.values()) == other
159

◆ __getitem__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__getitem__ ( self,
index )

Definition at line 196 of file GaudiHandles.py.

196 def __getitem__(self, index):
197 if isinstance(index, str):
198 # search by instance name
199 try:
200 return self._items[index]
201 except KeyError:
202 raise IndexError(
203 f"{self.__class__.__name__} does not have a {self.handleType.componentType} with instance name {index}"
204 )
205 else:
206 if isinstance(index, slice):
207 return [self._items[k] for k in self._indexToKey(index)]
208 else:
209 return self._items[self._indexToKey(index)]
210

◆ __iter__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__iter__ ( self)

Definition at line 211 of file GaudiHandles.py.

211 def __iter__(self):
212 yield from self._items.values()
213

◆ __len__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__len__ ( self)

Definition at line 150 of file GaudiHandles.py.

150 def __len__(self):
151 return len(self._items)
152

◆ __opt_value__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__opt_value__ ( self)

Definition at line 173 of file GaudiHandles.py.

173 def __opt_value__(self):
174 return self.toStringProperty()
175

◆ __repr__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__repr__ ( self)
Return class name with list of type/name strings as argument

Definition at line 160 of file GaudiHandles.py.

160 def __repr__(self):
161 """Return class name with list of type/name strings as argument"""
162 return (
163 self.__class__.__name__
164 + "(["
165 + ",".join(repr(h.toStringProperty()) for h in self._items.values())
166 + "])"
167 )
168

◆ __setitem__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__setitem__ ( self,
index,
value )

Definition at line 185 of file GaudiHandles.py.

185 def __setitem__(self, index, value):
186 try:
187 self._items[self._indexToKey(index)] = value
188 except TypeError as e:
189 if isinstance(index, slice):
190 raise TypeError(
191 f"Setting elements of {self.__class__.__name__} via slice is not supported"
192 )
193 else:
194 raise e
195

◆ __str__()

GaudiKernel.GaudiHandles.GaudiHandleArray.__str__ ( self)
Print in a form which can be parsed

Definition at line 169 of file GaudiHandles.py.

169 def __str__(self):
170 """Print in a form which can be parsed"""
171 return self.toStringProperty()
172

◆ _indexToKey()

GaudiKernel.GaudiHandles.GaudiHandleArray._indexToKey ( self,
index )
protected
Convert list index to dictionary key

Definition at line 176 of file GaudiHandles.py.

176 def _indexToKey(self, index):
177 """Convert list index to dictionary key"""
178 return list(self._items.keys())[index]
179

◆ index()

GaudiKernel.GaudiHandles.GaudiHandleArray.index ( self,
value,
* args )

Definition at line 230 of file GaudiHandles.py.

230 def index(self, value, *args):
231 return list(self._items.keys()).index(
232 value if isinstance(value, str) else value.getName(), *args
233 )
234

◆ insert()

GaudiKernel.GaudiHandles.GaudiHandleArray.insert ( self,
index,
value )
Only allow inserting compatible types. It accepts a string, a handle or a configurable.

Definition at line 235 of file GaudiHandles.py.

235 def insert(self, index, value):
236 """Only allow inserting compatible types. It accepts a string, a handle or a configurable."""
237 if isinstance(value, str):
238 # convert string to handle
239 value = self.__class__.handleType(value)
240 elif isinstance(value, self.__class__.handleType):
241 pass # leave handle as-is
242 elif isinstance(value, GaudiHandle):
243 # do not allow different type of handles
244 raise TypeError(
245 f"Can not add a {value.__class__.__name__} to a {self.__class__.__name__}"
246 )
247 elif value.getGaudiType() != self.__class__.handleType.componentType:
248 # assume it is a configurable: allow only correct types
249 raise TypeError(
250 f"Can not append {value.__class__.__name__} ({value.getGaudiType()}) to a {self.__class__.__name__}"
251 )
252 elif hasattr(value, "isPublic"):
253 # check public vs private if applicable for this configurable
254 pop = value.isPublic() and "Public" or "Private"
255 if value.isPublic() != self.__class__.handleType.isPublic:
256 raise TypeError(
257 f"Can not append {value.__class__.__name__} ({pop} {value.getGaudiType()}) to a {self.__class__.__name__}"
258 )
259
260 # check that an instance name appears only once in the list
261 if value.getName() not in self._items:
262 if index == len(self._items): # append
263 self._items[value.getName()] = value
264 else:
265 # for `insert` we need to rebuild the dictionary in the correct order
266 values = list(self._items.items())
267 values.insert(index, (value.getName(), value))
268 self._items = dict(values)
269 else:
270 oldValue = self._items[value.getName()]
271 _log.warning(
272 "%r with instance name %r already in %s. Not adding %r.",
273 oldValue,
274 oldValue.getName(),
275 self.__class__.__name__,
276 value,
277 )
278

◆ isPublic()

GaudiKernel.GaudiHandles.GaudiHandleArray.isPublic ( self)

Definition at line 279 of file GaudiHandles.py.

279 def isPublic(self):
280 return self.__class__.handleType.isPublic
281

◆ toStringProperty()

GaudiKernel.GaudiHandles.GaudiHandleArray.toStringProperty ( self)

Definition at line 285 of file GaudiHandles.py.

285 def toStringProperty(self):
286 return (
287 "["
288 + ",".join(repr(v.toStringProperty()) for v in self._items.values())
289 + "]"
290 )
291
292

Member Data Documentation

◆ __slots__

tuple GaudiKernel.GaudiHandles.GaudiHandleArray.__slots__ = ("_items",)
staticprivate

Definition at line 133 of file GaudiHandles.py.

◆ _items

GaudiKernel.GaudiHandles.GaudiHandleArray._items = {}
protected

Definition at line 138 of file GaudiHandles.py.

◆ handleType

GaudiKernel.GaudiHandles.GaudiHandleArray.handleType = None
static

Definition at line 134 of file GaudiHandles.py.


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