From 977a7f5843f74d20b191705e94cb6112c7c19fd6 Mon Sep 17 00:00:00 2001 From: Attila Krasznahorkay <Attila.Krasznahorkay@cern.ch> Date: Thu, 30 Aug 2018 14:11:20 +0200 Subject: [PATCH] Made the standalone version of AlgSequence a bit more flexible. Taught it how to accept insert requests, and how to delete an existing algorithm/sequence from the sequence. Did not introduce a __delitem__ function, since Athena configurables don't have such a function defined on them either. Former-commit-id: 6f8fbf4af3f4c3904d541e2d06c37ebc2dd9f9c3 --- .../AnaAlgorithm/python/AlgSequence.py | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py b/PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py index 8d96ad3c3d2..29e8588e50c 100644 --- a/PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py +++ b/PhysicsAnalysis/D3PDTools/AnaAlgorithm/python/AlgSequence.py @@ -40,6 +40,19 @@ except ImportError: return self._name + def insert( self, index, algOrSeq ): + """Insert one algorithm/sequence into this sequence + + This allows us to extend existing sequences with a greater + flexibility. + + Keyword arguments: + index -- The index to insert the algorithm/sequence under + algOrSeq -- The object to insert + """ + + return self.__iadd__( algOrSeq, index = index ) + def __getitem__( self, index ): """Return one algorithm/sequence from the sequence by index @@ -75,6 +88,30 @@ except ImportError: raise AttributeError( 'Algorithm/sequence with name "%s" was not ' \ 'found' % name ) + def __delattr__( self, name ): + """Remove one algorithm/sequence from this sequence, by name + + This is to allow removing algorithms (or even sequences) from this + sequence in case that would be needed. + + Keyword arguments: + name -- The name of the algorithm/sequence to delete from the + sequence + """ + + # Look up the algorithm by name: + for algOrSeq in self._algsAndSequences: + if algOrSeq.name() == name: + # If we found it, remove it: + self._algsAndSequences.remove( algOrSeq ) + return + pass + + # If no algorithm/sequence with this name was found, that's a + # problem: + raise AttributeError( 'Algorithm/sequence with name "%s" was not ' \ + 'found' % name ) + def __iter__( self ): """Create an iterator over all the algorithms of this sequence @@ -86,7 +123,7 @@ except ImportError: # Create the iterator to process the internal list of algorithms: return AlgSequenceIterator( self._algsAndSequences ) - def __iadd__( self, algOrSeq ): + def __iadd__( self, algOrSeq, index = None ): """Add one algorithm/sequence to the sequence This function is used to add one algorithm (or algorithm sequence) @@ -111,7 +148,11 @@ except ImportError: pass # Add the algorithm/sequence to the internal list: - self._algsAndSequences.append( algOrSeq ) + if not index: + self._algsAndSequences.append( algOrSeq ) + else: + self._algsAndSequences.insert( index, algOrSeq ) + pass # Return the modified object: return self @@ -293,6 +334,22 @@ except ImportError: self.assertEqual( algNames, [ 'Algorithm1', 'Algorithm2' ] ) return + ## Test the insertion of one algorithm + def test_insertAlg( self ): + self.seq.insert( 1, AnaAlgorithmConfig( 'AlgType3/Algorithm3' ) ) + self.assertEqual( len( self.seq ), 3 ) + self.assertEqual( self.seq[ 0 ].name(), 'Algorithm1' ) + self.assertEqual( self.seq[ 1 ].name(), 'Algorithm3' ) + self.assertEqual( self.seq[ 2 ].name(), 'Algorithm2' ) + return + + ## Test the deletion of an algorithm + def test_deleteAlg( self ): + del self.seq.Algorithm1 + self.assertEqual( len( self.seq ), 1 ) + self.assertEqual( self.seq[ 0 ].name(), 'Algorithm2' ) + return + pass ## Test case for a sequence with algorithms and sub-sequences -- GitLab