ATLAS Offline Software
Loading...
Searching...
No Matches
python.AlgSequence.AlgSequenceIterator Class Reference
Collaboration diagram for python.AlgSequence.AlgSequenceIterator:

Public Member Functions

 __init__ (self, sequence)
 __iter__ (self)
 __next__ (self)

Protected Attributes

 _sequence = sequence
int _index = 0
 _iterator = None

Detailed Description

Iterator over a standalone algorithm sequence

This custom class is needed to implement a "recursive iteration", which
would loop over all algorithms in a sequence that itself may have
sub-sequences inside of it.

Definition at line 216 of file PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py.

Constructor & Destructor Documentation

◆ __init__()

python.AlgSequence.AlgSequenceIterator.__init__ ( self,
sequence )
Constructor for the algorithm sequence iterator

Keyword arguments:
  sequence -- The sequence to iterate over (recursively)

Definition at line 224 of file PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py.

224 def __init__( self, sequence ):
225 """Constructor for the algorithm sequence iterator
226
227 Keyword arguments:
228 sequence -- The sequence to iterate over (recursively)
229 """
230
231 # Set up the internal variables:
232 self._sequence = sequence
233 self._index = 0
234 self._iterator = None
235
236 return
237

Member Function Documentation

◆ __iter__()

python.AlgSequence.AlgSequenceIterator.__iter__ ( self)
Function making this iterator iterable itself

Definition at line 238 of file PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py.

238 def __iter__( self ):
239 """Function making this iterator iterable itself
240 """
241 return self
242

◆ __next__()

python.AlgSequence.AlgSequenceIterator.__next__ ( self)
Function implementing the recursive iteration over an AlgSequence

This is where most of the logic is. The iterator loops over the
elements of the AlgSequence that was given to it, one by one. When
it finds an element in the AlgSequence that itself is also an
AlgSequence, then it creates a helper iterator object that would
process that sub-sequence, and continue the iteration using that
helper.

The end result is that the iteration should loop over every
algorithm in the sequence and its sub-sequences.

Definition at line 243 of file PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py.

243 def __next__( self ):
244 """Function implementing the recursive iteration over an AlgSequence
245
246 This is where most of the logic is. The iterator loops over the
247 elements of the AlgSequence that was given to it, one by one. When
248 it finds an element in the AlgSequence that itself is also an
249 AlgSequence, then it creates a helper iterator object that would
250 process that sub-sequence, and continue the iteration using that
251 helper.
252
253 The end result is that the iteration should loop over every
254 algorithm in the sequence and its sub-sequences.
255 """
256
257 # First off, check whether we reached the end of the sequence.
258 if self._index >= len( self._sequence ):
259 raise StopIteration()
260
261 # If not, check whether we are currently iterating over a
262 # sub-sequence.
263 if self._iterator:
264 try:
265 return self._iterator.__next__()
266 except StopIteration:
267 # If the sub-sequence is exhaused, then switch to the
268 # next element in our sequence, and call this function
269 # recursively.
270 self._index += 1
271 self._iterator = None
272 return self.__next__()
273
274 # If we are not iterating over a sub-sequence at the moment, let's
275 # just take the next element of our sequence.
276 element = self._sequence[ self._index ]
277
278 # If this element is a sequence itself, then switch to "sub-sequence
279 # iterating mode", and call this function recursively in that mode.
280 if isinstance( element, AlgSequence ):
281 self._iterator = AlgSequenceIterator( element )
282 return self.__next__()
283
284 # Apparently it's an algorithm we found. So update the internal
285 # index, and simply return the algorithm.
286 self._index += 1
287 return element
288
289 #

Member Data Documentation

◆ _index

int python.AlgSequence.AlgSequenceIterator._index = 0
protected

◆ _iterator

python.AlgSequence.AlgSequenceIterator._iterator = None
protected

◆ _sequence

python.AlgSequence.AlgSequenceIterator._sequence = sequence
protected

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