From 07bb59fc7fc64a0ed5d413e5a7e97e25e3d8a407 Mon Sep 17 00:00:00 2001 From: erodrigu <eduardo.rodrigues@cern.ch> Date: Sun, 7 May 2023 17:38:52 +0200 Subject: [PATCH 1/5] autopep8 on StrippingUtils --- .../python/StrippingUtils/LineBuilderTests.py | 89 ++++--- .../python/StrippingUtils/Utils.py | 249 ++++++++++-------- 2 files changed, 185 insertions(+), 153 deletions(-) diff --git a/Phys/StrippingUtils/python/StrippingUtils/LineBuilderTests.py b/Phys/StrippingUtils/python/StrippingUtils/LineBuilderTests.py index ec02d48d3..b26d5d8a4 100644 --- a/Phys/StrippingUtils/python/StrippingUtils/LineBuilderTests.py +++ b/Phys/StrippingUtils/python/StrippingUtils/LineBuilderTests.py @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -29,7 +29,8 @@ from py.test import raises from copy import deepcopy from StrippingConf.Configuration import StrippingLine -def test_line_builder(builderType, conf_dict) : + +def test_line_builder(builderType, conf_dict): """ Run all tests on a line builder, given a configuration dictionary. All line builders should pass this test. @@ -42,99 +43,111 @@ def test_line_builder(builderType, conf_dict) : test_second_instance_does_not_overwrite_lines(builderType, conf_dict) test_configuration_not_dictlike_raises(builderType, 0) test_bad_configuration_raises(builderType, conf_dict) - -def test_build_many_instances(builderType, conf_dict) : + + +def test_build_many_instances(builderType, conf_dict): print 'test_make_many_instances', builderType.__name__, '...' """ Test that an arbitrary number of instances can be instantiated with different names. Access the lines of each instance to catch on-demand line building. """ - baseName = builderType.__name__ - for n in '0123456789' : + baseName = builderType.__name__ + for n in '0123456789': b = builderType(baseName + n, conf_dict) lines = b.lines() test_linebuilder_instance(b) -def test_second_instance_does_not_overwrite_lines(builderType, conf_dict) : - print 'test_second_instance_does_not_overwrite_lines', builderType.__name__, '...' + +def test_second_instance_does_not_overwrite_lines(builderType, conf_dict): + print 'test_second_instance_does_not_overwrite_lines', builderType.__name__, '...' b0 = builderType(builderType.__name__+'FirstBuilder', conf_dict) b0lines = b0.lines() b1 = builderType(builderType.__name__+'SecondBuilder', conf_dict) assert b0.lines() == b0lines - -def test_linebuilder_instance(b) : + + +def test_linebuilder_instance(b): test_lines(b) test_cannot_modify_lines(b) test_line_locations(b) test_lineNames_method(b) test_outputLocations_method(b) test_lineFromName_method(b) - -def test_duplicate_name_raises(builderType, conf_dict) : + + +def test_duplicate_name_raises(builderType, conf_dict): print 'test_duplicate_name_raises', builderType.__name__, '...' - name=builderType.__name__+'Instance' + name = builderType.__name__+'Instance' b0 = builderType(name, conf_dict) raises(Exception, builderType, name, conf_dict) - -def test_default_raises(builderType) : + + +def test_default_raises(builderType): print 'test_default_raises', builderType.__name__, '...' raises(Exception, builderType) -def test_single_constructor_argument_raises(builderType, conf_dict) : + +def test_single_constructor_argument_raises(builderType, conf_dict): print 'test_single_constructor_argument_raises', builderType.__name__, '...' raises(Exception, builderType, conf_dict) raises(Exception, builderType, 'SomeCrazyName') -def test_configuration_not_dictlike_raises(builderType, conf_dict) : + +def test_configuration_not_dictlike_raises(builderType, conf_dict): print 'test_configuration_not_dictlike_raises', builderType.__name__, '...' raises(AttributeError, builderType, 'TestBadConfDict', conf_dict) -def test_bad_configuration_raises(builderType, good_conf_dict) : + +def test_bad_configuration_raises(builderType, good_conf_dict): print 'test_bad_configuration_raises', builderType.__name__, '...' assert hasattr(good_conf_dict, 'keys') assert hasattr(good_conf_dict, '__getitem__') bad_conf_dict = deepcopy(good_conf_dict) bad_conf_dict['BAD_KEY'] = 0. raises(KeyError, builderType, 'TestBadConfKeys', bad_conf_dict) - -def test_line(line) : + + +def test_line(line): assert type(line) == StrippingLine -def test_line_location(line, allowEmptyLocation=True) : + +def test_line_location(line, allowEmptyLocation=True): """ Test that the output location of a line called StrippingXYZ is .../XYZ. If allowEmptyLocation = True accept an empty string. """ - if allowEmptyLocation : - if not line.outputLocation() or line.outputLocation() =='': + if allowEmptyLocation: + if not line.outputLocation() or line.outputLocation() == '': return assert 'Stripping'+line.outputLocation().split('/')[-2] == line.name() -def test_lines(builder) : + +def test_lines(builder): print 'test_lines', type(builder).__name__, '...' lines = builder.lines() - for line in lines : + for line in lines: test_line(line) -def test_cannot_modify_lines(builder) : + +def test_cannot_modify_lines(builder): """ Test that the builder.lines() field cannot be modified. If builder.lines() returns a list-like type that supports += [...] syntax, check that no modification ocurrs. Else, all is good. Check that there is no change anyway. """ - print 'test_cannot_modify_lines', type(builder).__name__, '...' + print 'test_cannot_modify_lines', type(builder).__name__, '...' orig_lines = builder.lines() mod_lines = builder.lines() - try : + try: mod_lines += ['BAD LINE'] assert builder.lines() != mod_lines - except TypeError : + except TypeError: pass assert builder.lines() == orig_lines - -def test_line_locations(builder, allowEmptyLocation=True) : + +def test_line_locations(builder, allowEmptyLocation=True): """ Test that the output locations of a set of lines called StrippingXYZ, StrippingABC, .. is .../XYZ .../ABC, etc. @@ -142,20 +155,22 @@ def test_line_locations(builder, allowEmptyLocation=True) : """ print 'test_line_locations', type(builder).__name__, '...' lines = builder.lines() - for line in lines : + for line in lines: test_line_location(line, allowEmptyLocation) def test_lineNames_method(builder): names = [l.name() for l in builder.lines()] - for name in names : + for name in names: assert name in builder.lineNames() -def test_outputLocations_method(builder) : + +def test_outputLocations_method(builder): locations = [line.outputLocation() for line in builder.lines()] - for location in locations : + for location in locations: assert location in builder.outputLocations() -def test_lineFromName_method(builder) : - for line in builder.lines() : + +def test_lineFromName_method(builder): + for line in builder.lines(): assert line == builder.lineFromName(line.name()) diff --git a/Phys/StrippingUtils/python/StrippingUtils/Utils.py b/Phys/StrippingUtils/python/StrippingUtils/Utils.py index b18d224ab..f4395e8ff 100644 --- a/Phys/StrippingUtils/python/StrippingUtils/Utils.py +++ b/Phys/StrippingUtils/python/StrippingUtils/Utils.py @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -21,14 +21,17 @@ __all__ = ('checkConfig', 'lineFromName' ) -import inspect, os, subprocess +import inspect +import os +import subprocess from os import environ from pprint import * from Gaudi.Configuration import * from StrippingConf.Configuration import StrippingLine from pprint import pformat -def checkConfig(reference_keys, configuration) : + +def checkConfig(reference_keys, configuration): """ Check that all the required configuration parameters are present in configuration.keys(), and that the number of keys in configuration @@ -40,7 +43,7 @@ def checkConfig(reference_keys, configuration) : ref_diff = ref_keys.difference(config_keys) config_diff = config_keys.difference(ref_keys) - if len(ref_diff) > 0 or len(config_diff) > 0 : + if len(ref_diff) > 0 or len(config_diff) > 0: msg = '''Missing/extra keys! Keys in reference: ''' + pformat(ref_keys) + ''' @@ -52,56 +55,60 @@ Keys in config but not in reference: ''' + pformat(config_diff) raise Exception(msg) -def lineNames(lineBuilder) : + +def lineNames(lineBuilder): """ Return a list with all the names of the lines handled by a lineBuilder. """ return tuple([line.name() for line in lineBuilder.lines()]) -def outputLocations(lineBuilder) : + +def outputLocations(lineBuilder): """ Return a list with all the output locations of the lines handled by a lineBuilder. """ return tuple([line.outputLocation() for line in lineBuilder.lines()]) -def lineFromName(lineBuilder, lineName) : + +def lineFromName(lineBuilder, lineName): """ Get the line that has a given name. If not available, return None. """ - lines = filter(lambda x: x.name()==lineName , lineBuilder.lines()) - if len(lines)>1 : + lines = filter(lambda x: x.name() == lineName, lineBuilder.lines()) + if len(lines) > 1: raise Exception("More than one line with name ", lineName) - if len(lines) == 0 : return None + if len(lines) == 0: + return None return lines[0] -class LineBuilder(object) : +class LineBuilder(object): """ """ - # Keep a list of all builder instances so a line can be traced back to its + # Keep a list of all builder instances so a line can be traced back to its # builder, config and module. Disabled by default to save memory. keepregistry = False builderregistry = [] @staticmethod - def find_builder_from_line(line) : + def find_builder_from_line(line): '''Find the builder that made the given line.''' - for builder in LineBuilder.builderregistry : - if line in builder._lines : + for builder in LineBuilder.builderregistry: + if line in builder._lines: return builder return None - + @staticmethod - def find_builder_from_config(config) : + def find_builder_from_config(config): '''Find the builder that used the given config.''' - if 'CONFIG' in config : + if 'CONFIG' in config: config = config['CONFIG'] - for builder in LineBuilder.builderregistry : - if builder._config == config : + for builder in LineBuilder.builderregistry: + if builder._config == config: return builder return None @@ -111,56 +118,58 @@ class LineBuilder(object) : outputLocations = outputLocations lineFromName = lineFromName - def __init__(self, + def __init__(self, name, - config) : - - try : + config): + + try: checkConfig(self.configKeys(), config) - except Exception as excpt : + except Exception as excpt: msg = 'Config check failed for builder ' + name + '\n' + excpt.message raise Exception(msg) self._config = config self._name = name self._lines = [] - if LineBuilder.keepregistry : + if LineBuilder.keepregistry: LineBuilder.builderregistry.append(self) - def configKeys(self) : + def configKeys(self): return self.__configuration_keys__ - def name(self) : + def name(self): return self._name - def configurationParameter(self, key) : + def configurationParameter(self, key): return self._config[key] - def registerLine(self, line) : + def registerLine(self, line): self.checkLine(line) self._lines += [line] - def checkLine(self, line) : + def checkLine(self, line): assert type(line) == StrippingLine # add other checks here. For example, outputLocations. # Do all lines have one? - - def lines(self) : + + def lines(self): return tuple(self._lines) - - def keys ( self ) : return self.configKeys() - def has_key ( self , key ) : - return item in self.configKeys() and self._config.has_key ( key ) - def __getitem__ ( self , key ) : return self.configurationParameter ( key) - def __contains__ ( self , key ) : return self.has_key ( item ) - - + + def keys(self): return self.configKeys() + + def has_key(self, key): + return item in self.configKeys() and self._config.has_key(key) + + def __getitem__(self, key): return self.configurationParameter(key) + def __contains__(self, key): return self.has_key(item) + + class MasterLineBuilder(LineBuilder): """ Configure more than one line builder from the same class at once """ - + def __init__(self, name, config, @@ -168,121 +177,129 @@ class MasterLineBuilder(LineBuilder): """ config should be a dictionary or a map {SlaveSuffix: config_slave} """ - self.__slave_builders__={} + self.__slave_builders__ = {} self._config = config self._name = name - + if not issubclass(SlaveBuilderClass, LineBuilder): raise TypeError, "The slave class must inherit from a LineBuilder" - - self.__configuration_keys__=SlaveBuilderClass.__configuration_keys__ - - #check all the configs + + self.__configuration_keys__ = SlaveBuilderClass.__configuration_keys__ + + # check all the configs for slaveLine in config.keys(): try: config[slaveLine].keys() except AttributeError: raise KeyError, "config key "+slaveLine+" does not hold a correctly formed config for the slave" - + checkConfig(self.configKeys(), config[slaveLine]) - - #then build all slaves + + # then build all slaves for slaveLine in config.keys(): - self.__slave_builders__[slaveLine]=SlaveBuilderClass(name+slaveLine,config[slaveLine]) - - + self.__slave_builders__[slaveLine] = SlaveBuilderClass( + name+slaveLine, config[slaveLine]) + def lines(self): """ Return a tuple of the lines from all slaves """ - _lines=[] + _lines = [] for slave in self.__slave_builders__: - _lines+=list(self.__slave_builders__[slave].lines()) + _lines += list(self.__slave_builders__[slave].lines()) return tuple(_lines) - - #add slave helper functions + + # add slave helper functions def slaves(self): """ Return a tuple of the LineBuilders which are slaves of this builder """ - _slaves=[] + _slaves = [] for slave in self.__slave_builders__: _slaves.append(self.__slave_builders__[slave]) return tuple(_slaves) - - def slave(self,name): + + def slave(self, name): """ Return a reference to the Builder which is a slave to this with Suffix=name """ return self.__slave_builders__[name] - - #remove certain methods which should not be called - def registerLine(self, line): - raise Exception("Only the LineBuilders should register lines, not the masters ", self.name) - - def configurationParameter(self, key) : - raise Exception("Only the LineBuilders have configuration parameters, not the masters ", self.name) + # remove certain methods which should not be called + def registerLine(self, line): + raise Exception( + "Only the LineBuilders should register lines, not the masters ", self.name) + def configurationParameter(self, key): + raise Exception( + "Only the LineBuilders have configuration parameters, not the masters ", self.name) -def getLineBuildersFromModule(confModule) : +def getLineBuildersFromModule(confModule): """ Extract all the line builders from a given module. Return as a class name : class dictionary. """ - lbs = [getattr( confModule, x) for x in confModule.__dict__.keys()] - lbs = filter(lambda lb : inspect.isclass(lb), lbs) - lbs = filter(lambda lb : issubclass(lb, LineBuilder), lbs) + lbs = [getattr(confModule, x) for x in confModule.__dict__.keys()] + lbs = filter(lambda lb: inspect.isclass(lb), lbs) + lbs = filter(lambda lb: issubclass(lb, LineBuilder), lbs) lineBuilderDict = {} - for lb in lbs : - if lb.__name__ != LineBuilder.__name__ : + for lb in lbs: + if lb.__name__ != LineBuilder.__name__: lineBuilderDict[lb.__name__] = lb return lineBuilderDict -def getBuilderConfFromModule(confModule) : + +def getBuilderConfFromModule(confModule): """ Extract all the line builders from a given module. Return as a class name : class dictionary. """ - lbs = [getattr( confModule, x) for x in confModule.__dict__.keys()] - lbs = filter(lambda lb : inspect.isclass(lb), lbs) - lbs = filter(lambda lb : issubclass(lb, LineBuilder), lbs) + lbs = [getattr(confModule, x) for x in confModule.__dict__.keys()] + lbs = filter(lambda lb: inspect.isclass(lb), lbs) + lbs = filter(lambda lb: issubclass(lb, LineBuilder), lbs) builderConfDict = {} allowedkeys = ['BUILDERTYPE', 'CONFIG', 'NAME', 'STREAMS', 'WGs'] allowedkeysshort = ['BUILDERTYPE', 'CONFIG', 'STREAMS', 'WGs'] if confModule.__dict__.has_key('default_config'): - if isinstance(confModule.__dict__['default_config'],dict): - tmpKeys = confModule.default_config.keys() - tmpKeys.sort(key=lambda x: x[0]) - if "NAME" in tmpKeys: - for tmpKey in tmpKeys: - if tmpKey not in allowedkeys: - log.error("The key %(wk)s is not allowed in the default_config for %(mn)s"%{'wk':tmpKey, 'mn':confModule.__name__}) - log.error("Allowed keys are: "+",".join(allowedkeys)) - break - if tmpKeys == allowedkeys: - builderConfDict[confModule.default_config['NAME']] = confModule.default_config - else: - for k,d in confModule.default_config.iteritems(): - if not isinstance(d,dict): - log.error("The default_config of %s.py is not a dictionary"%confModule.__name__) - break - tmpKeys = d.keys() + if isinstance(confModule.__dict__['default_config'], dict): + tmpKeys = confModule.default_config.keys() tmpKeys.sort(key=lambda x: x[0]) - for tmpKey in tmpKeys: - if tmpKey not in allowedkeysshort: - log.error("The key %(wk)s is not allowed in the default_config for %(mn)s"%{'wk':tmpKey, 'mn':confModule.__name__}) - log.error("Allowed keys are: "+",".join(allowedkeysshort)) - break - if not tmpKeys == allowedkeysshort: - log.error("The default_config of %s.py has incorrect keys"%confModule.__name__) - break - builderConfDict[k] = d + if "NAME" in tmpKeys: + for tmpKey in tmpKeys: + if tmpKey not in allowedkeys: + log.error("The key %(wk)s is not allowed in the default_config for %(mn)s" % { + 'wk': tmpKey, 'mn': confModule.__name__}) + log.error("Allowed keys are: "+",".join(allowedkeys)) + break + if tmpKeys == allowedkeys: + builderConfDict[confModule.default_config['NAME'] + ] = confModule.default_config + else: + for k, d in confModule.default_config.iteritems(): + if not isinstance(d, dict): + log.error( + "The default_config of %s.py is not a dictionary" % confModule.__name__) + break + tmpKeys = d.keys() + tmpKeys.sort(key=lambda x: x[0]) + for tmpKey in tmpKeys: + if tmpKey not in allowedkeysshort: + log.error("The key %(wk)s is not allowed in the default_config for %(mn)s" % { + 'wk': tmpKey, 'mn': confModule.__name__}) + log.error("Allowed keys are: " + + ",".join(allowedkeysshort)) + break + if not tmpKeys == allowedkeysshort: + log.error( + "The default_config of %s.py has incorrect keys" % confModule.__name__) + break + builderConfDict[k] = d return builderConfDict -def getBuilderAndConfFromModule(confModule) : + +def getBuilderAndConfFromModule(confModule): """ Extract all the line builders and their default configuration from a given module. @@ -290,19 +307,19 @@ def getBuilderAndConfFromModule(confModule) : """ buildersConf = {} lineBuilders = getLineBuildersFromModule(confModule) - _confs = getBuilderConfFromModule(confModule) - for name,conf in _confs.iteritems(): - if conf['BUILDERTYPE'] in lineBuilders.keys(): - buildersConf[name] = conf - else: - print '[WARNING] The LinBuilder %s is not defined in the module %s' % (conf['BUILDERTYPE'], confModule.__name__ ) + _confs = getBuilderConfFromModule(confModule) + for name, conf in _confs.iteritems(): + if conf['BUILDERTYPE'] in lineBuilders.keys(): + buildersConf[name] = conf + else: + print '[WARNING] The LinBuilder %s is not defined in the module %s' % (conf['BUILDERTYPE'], confModule.__name__) + + return lineBuilder, buildersConf - return lineBuilder,buildersConf -def create_archive(source, dest) : - if not os.path.exists(dest) : +def create_archive(source, dest): + if not os.path.exists(dest): os.makedirs(dest) return subprocess.call('rsync -ar --delete {0} {1}'.format(os.path.join(source, '*'), dest), - shell = True) - + shell=True) -- GitLab From 96cac36f3861c6617c31aef6acfc1649e37b6e79 Mon Sep 17 00:00:00 2001 From: erodrigu <eduardo.rodrigues@cern.ch> Date: Sun, 7 May 2023 17:45:49 +0200 Subject: [PATCH 2/5] Fix some Flake8 warningspython/StrippingUtils/__init__.py --- Phys/StrippingUtils/python/StrippingUtils/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Phys/StrippingUtils/python/StrippingUtils/__init__.py b/Phys/StrippingUtils/python/StrippingUtils/__init__.py index e8f5a09d2..5f2834e1e 100644 --- a/Phys/StrippingUtils/python/StrippingUtils/__init__.py +++ b/Phys/StrippingUtils/python/StrippingUtils/__init__.py @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -8,4 +8,3 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ############################################################################### -from Utils import * -- GitLab From f52d09ecb58accb953f602e27a0cf5b9c98fb3d7 Mon Sep 17 00:00:00 2001 From: erodrigu <eduardo.rodrigues@cern.ch> Date: Sun, 7 May 2023 17:52:33 +0200 Subject: [PATCH 3/5] Please Flake8 for python/DisplVertices/JetFunctions.py --- .../python/DisplVertices/JetFunctions.py | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Phys/DisplVertices/python/DisplVertices/JetFunctions.py b/Phys/DisplVertices/python/DisplVertices/JetFunctions.py index 4777f488c..24603f4e5 100644 --- a/Phys/DisplVertices/python/DisplVertices/JetFunctions.py +++ b/Phys/DisplVertices/python/DisplVertices/JetFunctions.py @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -9,35 +9,36 @@ # or submit itself to any jurisdiction. # ############################################################################### """ -Common definitions of a list of PF/jet functions to make the configuration more readable +Common definitions of a list of PF/jet functions +to make the configuration more readable. """ # FIXME: (MCl) workaround for ROOT issue with autoparse of enums. import cppyy if not hasattr(cppyy.gbl.LHCb, 'ParticleFlowType'): try: cppyy.gbl.gROOT.ProcessLine('#include "Kernel/JetEnums.h"') - except: + except: # noqa from GaudiPython import loaddict loaddict("JetAccessoriesDict") from LoKiCore.decorators import LHCb from LoKiCore.math import max from LoKiPhys.decorators import INFO -## selection by ParticleFlowType +# selection by ParticleFlowType PFTYPE = INFO(LHCb.ParticleFlowType.KEY, -1.) -## JetID variables from ExtraInfo -JNTRACKS = INFO(LHCb.JetIDInfo.Ntracks , -1.) -JN90 = INFO(LHCb.JetIDInfo.N90 , -1.) -JMTF = INFO(LHCb.JetIDInfo.MTF , -1.) -JMNF = INFO(LHCb.JetIDInfo.MNF , -1.) -JMPT = INFO(LHCb.JetIDInfo.MPT , -1.) -JCPF = INFO(LHCb.JetIDInfo.CPF , -1.) -JWIDTH = INFO(LHCb.JetIDInfo.JetWidth , -1.) -JNSATECAL = INFO(LHCb.JetIDInfo.NSatCalo , -1.) -JNSATHCAL = INFO(LHCb.JetIDInfo.NSatCalo , -1.) -JNSATCALO = INFO(LHCb.JetIDInfo.NSatCalo , -1.) -JNWITHPVINFO = INFO(LHCb.JetIDInfo.NHasPV , -1.) +# JetID variables from ExtraInfo +JNTRACKS = INFO(LHCb.JetIDInfo.Ntracks, -1.) +JN90 = INFO(LHCb.JetIDInfo.N90, -1.) +JMTF = INFO(LHCb.JetIDInfo.MTF, -1.) +JMNF = INFO(LHCb.JetIDInfo.MNF, -1.) +JMPT = INFO(LHCb.JetIDInfo.MPT, -1.) +JCPF = INFO(LHCb.JetIDInfo.CPF, -1.) +JWIDTH = INFO(LHCb.JetIDInfo.JetWidth, -1.) +JNSATECAL = INFO(LHCb.JetIDInfo.NSatCalo, -1.) +JNSATHCAL = INFO(LHCb.JetIDInfo.NSatCalo, -1.) +JNSATCALO = INFO(LHCb.JetIDInfo.NSatCalo, -1.) +JNWITHPVINFO = INFO(LHCb.JetIDInfo.NHasPV, -1.) JNWITHPVINFOCHI24 = INFO(LHCb.JetIDInfo.NIPChi2Inf4, -1.) -JMPF = max( JMTF , JMNF ) +JMPF = max(JMTF, JMNF) -- GitLab From 8c35e0bd02470c1eee6d232bab0239467436fe6c Mon Sep 17 00:00:00 2001 From: erodrigu <eduardo.rodrigues@cern.ch> Date: Tue, 9 May 2023 03:13:37 +0200 Subject: [PATCH 4/5] autopep8 part of StrippingConf --- .../StrippingConfigurableUser.py | 27 +++--- .../python/StrippingConf/Utils.py | 85 ++++++++++--------- .../StrippingConf/tests/test_strippingconf.py | 38 ++++----- .../StrippingConf/tests/test_strippingline.py | 63 +++++++------- 4 files changed, 111 insertions(+), 102 deletions(-) mode change 100755 => 100644 Phys/StrippingConf/tests/test_strippingline.py diff --git a/Phys/StrippingConf/python/StrippingConf/StrippingConfigurableUser.py b/Phys/StrippingConf/python/StrippingConf/StrippingConfigurableUser.py index 6aff886dd..f10e7d7a0 100755 --- a/Phys/StrippingConf/python/StrippingConf/StrippingConfigurableUser.py +++ b/Phys/StrippingConf/python/StrippingConf/StrippingConfigurableUser.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -14,21 +14,22 @@ # StrippingConfigurableUser # -__all__ = ( - 'StrippingConfigurableUser' - ) +__all__ = ( + 'StrippingConfigurableUser' +) from GaudiConf.Configuration import * from Configurables import LHCbConfigurableUser -class StrippingConfigurableUser ( LHCbConfigurableUser ) : - def getProps(self) : +class StrippingConfigurableUser (LHCbConfigurableUser): + + def getProps(self): + """ + From HltLinesConfigurableUser + @todo Should be shared between Hlt and stripping """ - From HltLinesConfigurableUser - @todo Should be shared between Hlt and stripping - """ - d = dict() - for (k,v) in self.getDefaultProperties().iteritems() : - d[k] = getattr(self,k) if hasattr(self,k) else v - return d + d = dict() + for (k, v) in self.getDefaultProperties().iteritems(): + d[k] = getattr(self, k) if hasattr(self, k) else v + return d diff --git a/Phys/StrippingConf/python/StrippingConf/Utils.py b/Phys/StrippingConf/python/StrippingConf/Utils.py index 4b5e5794b..dc34518f1 100644 --- a/Phys/StrippingConf/python/StrippingConf/Utils.py +++ b/Phys/StrippingConf/python/StrippingConf/Utils.py @@ -16,38 +16,41 @@ __author__ = 'Michael Alexander <michael.thomas.alexander@cern.ch>' from Gaudi.Configuration import log + def _limitCombinatoricsMaxCandidates(configurable, MaxCandidates, - MaxCombinations, force, incidentName) : + MaxCombinations, force, incidentName): '''Limit combinatorics of a configurable assuming it has MaxCandidates, StopAtMaxCandidates, MaxCombinations, StopAtMaxCombinations and StopIncidentType attributes.''' didsomething = False - if (force or not configurable.isPropertySet('MaxCandidates')) and None != MaxCandidates : - log.debug( "Forcing MaxCandidates settings for " + configurable.name() ) + if (force or not configurable.isPropertySet('MaxCandidates')) and None != MaxCandidates: + log.debug("Forcing MaxCandidates settings for " + configurable.name()) configurable.MaxCandidates = MaxCandidates configurable.StopAtMaxCandidates = True didsomething = True - if (force or not configurable.isPropertySet('MaxCombinations')) and None != MaxCombinations : - log.debug( "Forcing MaxCombinations settings for " + configurable.name() ) + if (force or not configurable.isPropertySet('MaxCombinations')) and None != MaxCombinations: + log.debug("Forcing MaxCombinations settings for " + configurable.name()) configurable.MaxCombinations = MaxCombinations configurable.StopAtMaxCombinations = True didsomething = True - if didsomething : + if didsomething: configurable.StopIncidentType = incidentName - return didsomething + return didsomething + -def _limitCombinatoricsMaxParticles(configurable, MaxCandidates, force, incidentName) : +def _limitCombinatoricsMaxParticles(configurable, MaxCandidates, force, incidentName): '''Limit combinatorics for a configurable assuming it's got MaxParticles & StopIncidentType attributes.''' - if (force or not configurable.isPropertySet('MaxParticles')) and None != MaxCandidates : - log.debug( "Forcing MaxParticles settings for " + configurable.name() ) + if (force or not configurable.isPropertySet('MaxParticles')) and None != MaxCandidates: + log.debug("Forcing MaxParticles settings for " + configurable.name()) configurable.MaxParticles = MaxCandidates configurable.StopIncidentType = incidentName return True return False -## List of Configurables that will fail-fast on limitCombinatorics + +# List of Configurables that will fail-fast on limitCombinatorics # Added by hand. WHITELIST = ( 'AddExtraInfo', @@ -72,63 +75,65 @@ WHITELIST = ( 'VeloEventShapeCutsS20p3', ) - -def limitCombinatorics( configurable, - MaxCandidates, - MaxCombinations, - force, - incidentName = 'ExceedsCombinatoricsLimit' ) : + +def limitCombinatorics(configurable, + MaxCandidates, + MaxCombinations, + force, + incidentName='ExceedsCombinatoricsLimit'): """ - This function has the highest call counts. So it needs careful optimization. - Because the action is reactive, it's best to order the case of most-probable to the least-probable to exit the check as early as possible. """ - ## Check the name by string is MUCH FASTER than by class. + # Check the name by string is MUCH FASTER than by class. classname = configurable.__class__.__name__ - - ## Early abort for these class which doesn't need limitation. + + # Early abort for these class which doesn't need limitation. if classname in WHITELIST: return False - ## Early dealing with nested + # Early dealing with nested if classname == 'GaudiSequencer': val = False for conf in configurable.Members: - val |= limitCombinatorics( conf, MaxCandidates, MaxCombinations, force, incidentName ) + val |= limitCombinatorics( + conf, MaxCandidates, MaxCombinations, force, incidentName) return val - if classname in ('CombineParticles','DaVinci__N3BodyDecays','DaVinci__N4BodyDecays', 'DaVinci__N5BodyDecays'): + if classname in ('CombineParticles', 'DaVinci__N3BodyDecays', 'DaVinci__N4BodyDecays', 'DaVinci__N5BodyDecays'): return _limitCombinatoricsMaxCandidates(configurable, MaxCandidates, MaxCombinations, force, incidentName) if classname == 'StrippingAlg': val = False - stages = [ 'Filter1' ] - for conf in [ getattr( configurable, stage ) for stage in stages if hasattr(configurable, stage) ] : - val |= limitCombinatorics( conf, MaxCandidates, MaxCombinations, force, incidentName ) - if val: - configurable.IncidentsToBeFlagged += [ incidentName ] + stages = ['Filter1'] + for conf in [getattr(configurable, stage) for stage in stages if hasattr(configurable, stage)]: + val |= limitCombinatorics( + conf, MaxCandidates, MaxCombinations, force, incidentName) + if val: + configurable.IncidentsToBeFlagged += [incidentName] return val if classname in ('SubPIDMMFilter', 'SubstitutePID'): return _limitCombinatoricsMaxParticles(configurable, MaxCandidates, force, incidentName) - ## Generic handling (slow) - if hasattr(type(configurable),'StopAtMaxCandidates') and hasattr(type(configurable),'MaxCandidates') and \ - hasattr(type(configurable),'StopAtMaxCombinations') and hasattr(type(configurable),'MaxCombinations'): + # Generic handling (slow) + if hasattr(type(configurable), 'StopAtMaxCandidates') and hasattr(type(configurable), 'MaxCandidates') and \ + hasattr(type(configurable), 'StopAtMaxCombinations') and hasattr(type(configurable), 'MaxCombinations'): return _limitCombinatoricsMaxCandidates(configurable, MaxCandidates, MaxCombinations, force, incidentName) - if hasattr(configurable, 'MaxParticles') and hasattr(configurable, 'StopIncidentType') : - return _limitCombinatoricsMaxParticles(configurable, MaxCandidates, force, incidentName) + if hasattr(configurable, 'MaxParticles') and hasattr(configurable, 'StopIncidentType'): + return _limitCombinatoricsMaxParticles(configurable, MaxCandidates, force, incidentName) - ## Generic handling (slow) - if hasattr( configurable, 'Members' ) : - #print classname + # Generic handling (slow) + if hasattr(configurable, 'Members'): + # print classname val = False - for i in getattr( configurable, 'Members' ) : - val |= limitCombinatorics( i, MaxCandidates, MaxCombinations, force, incidentName ) + for i in getattr(configurable, 'Members'): + val |= limitCombinatorics( + i, MaxCandidates, MaxCombinations, force, incidentName) return val - #log.warning('Not able to set MaxCandidates and MaxCombinations to algorithm '+str(type(configurable))+'/'+configurable.name()) + # log.warning('Not able to set MaxCandidates and MaxCombinations to algorithm '+str(type(configurable))+'/'+configurable.name()) return False - diff --git a/Phys/StrippingConf/tests/test_strippingconf.py b/Phys/StrippingConf/tests/test_strippingconf.py index 0c9677382..bb4da0bf2 100755 --- a/Phys/StrippingConf/tests/test_strippingconf.py +++ b/Phys/StrippingConf/tests/test_strippingconf.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -40,47 +40,47 @@ def test_dumplicated_outputLocation_in_single_stream_raises_Exception() : raises( Exception, StrippingConf, Streams = [stream] ) """ -def test_dumplicated_outputLocation_in_different_streams_raises_Exception() : + +def test_dumplicated_outputLocation_in_different_streams_raises_Exception(): filter = FilterDesktop('FilterX') - line0 = StrippingLine('Line0b', algos = [filter]) + line0 = StrippingLine('Line0b', algos=[filter]) + + line1 = StrippingLine('Line1b', algos=[filter]) - line1 = StrippingLine('Line1b', algos = [filter]) + stream0 = StrippingStream('Stream0', Lines=[line0]) - stream0 = StrippingStream('Stream0', Lines = [line0]) + stream1 = StrippingStream('Stream0', Lines=[line1]) - stream1 = StrippingStream('Stream0', Lines = [line1]) + raises(Exception, StrippingConf, Streams=[stream0, stream1]) - raises( Exception, StrippingConf, Streams = [stream0,stream1] ) - -if '__main__' == __name__ : +if '__main__' == __name__: import sys - test_names = filter(lambda k : k.count('test_') > 0, locals().keys()) + test_names = filter(lambda k: k.count('test_') > 0, locals().keys()) - __tests = filter( lambda x : x[0] in test_names, locals().items()) - + __tests = filter(lambda x: x[0] in test_names, locals().items()) message = '' summary = '\n' length = len(sorted(test_names, - cmp = lambda x,y : cmp(len(y),len(x)))[0]) +2 - - for test in __tests : - try : + cmp=lambda x, y: cmp(len(y), len(x)))[0]) + 2 + + for test in __tests: + try: test[1]() message = 'PASS' - except : + except: message = "FAIL" summary += test[0].ljust(length) + ':' + message.rjust(10) + '\n' - if summary.count('FAIL') > 0 : + if summary.count('FAIL') > 0: message = 'FAIL' wr = sys.stderr.write - else : + else: message = 'PASS' wr = sys.stdout.write diff --git a/Phys/StrippingConf/tests/test_strippingline.py b/Phys/StrippingConf/tests/test_strippingline.py old mode 100755 new mode 100644 index 040c7c8ff..dab7945a2 --- a/Phys/StrippingConf/tests/test_strippingline.py +++ b/Phys/StrippingConf/tests/test_strippingline.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -20,74 +20,77 @@ from StrippingConf.Configuration import StrippingLine, StrippingStream, Strippin from PhysSelPython.Wrappers import AutomaticData -def test_instantiate_empty_line() : + +def test_instantiate_empty_line(): line = StrippingLine('EmptyLine') assert line.name() == 'StrippingEmptyLine' assert line.subname() == 'EmptyLine' assert line.isAppended() == False assert line.outputLocation() == None assert line.selection() == None - -def test_instantiate_from_Selection_object() : - selection = AutomaticData(Location = 'Phys/Selection/Particles') - line = StrippingLine('SelectionLine', algos = [selection]) + + +def test_instantiate_from_Selection_object(): + selection = AutomaticData(Location='Phys/Selection/Particles') + line = StrippingLine('SelectionLine', algos=[selection]) assert line.name() == 'StrippingSelectionLine' assert line.subname() == 'SelectionLine' assert line.isAppended() == False assert line.outputLocation() == 'Phys/SelectionLine/Particles' assert line.selection() != None assert line.selection().outputLocation() == line.outputLocation() - -def test_duplicate_name_raises_ValueError() : - line0 = StrippingLine('DuplicateLine') - raises(ValueError, StrippingLine, 'DuplicateLine' ) -def test_filterMembers() : +def test_duplicate_name_raises_ValueError(): + _ = StrippingLine('DuplicateLine') + raises(ValueError, StrippingLine, 'DuplicateLine') + + +def test_filterMembers(): ''' It is necessary to construct a StrippingStream and a StrippingConf to test the filterMembers of the line. The line by itself is useless. ''' - selection = AutomaticData(Location = 'Phys/Selection/Particles') + selection = AutomaticData(Location='Phys/Selection/Particles') name = 'FilterMembersTest' - line = StrippingLine('FilterMembersTest', algos = [selection]) + line = StrippingLine('FilterMembersTest', algos=[selection]) assert line.name() == 'Stripping' + name assert line.subname() == name assert line.isAppended() == False assert line.outputLocation() == 'Phys/FilterMembersTest/Particles' - stream = StrippingStream('stream', Lines = [line]) - conf = StrippingConf('conf', Streams = [stream]) - assert [m.name() for m in line.filterMembers()] == ['checkPVmin1', - #'SelFilterPhys_Selection_Particles', - 'SELECT:Phys/Selection', - 'FilterMembersTest'] + stream = StrippingStream('stream', Lines=[line]) + conf = StrippingConf('conf', Streams=[stream]) + assert [m.name() for m in line.filterMembers()] == ['checkPVmin1', + # 'SelFilterPhys_Selection_Particles', + 'SELECT:Phys/Selection', + 'FilterMembersTest'] + -if '__main__' == __name__ : +if '__main__' == __name__: import sys - test_names = filter(lambda k : k.count('test_') > 0, locals().keys()) + test_names = filter(lambda k: k.count('test_') > 0, locals().keys()) - __tests = filter( lambda x : x[0] in test_names, locals().items()) - + __tests = filter(lambda x: x[0] in test_names, locals().items()) message = '' summary = '\n' length = len(sorted(test_names, - cmp = lambda x,y : cmp(len(y),len(x)))[0]) +2 - - for test in __tests : - try : + cmp=lambda x, y: cmp(len(y), len(x)))[0]) + 2 + + for test in __tests: + try: test[1]() message = 'PASS' - except : + except: message = "FAIL" summary += test[0].ljust(length) + ':' + message.rjust(10) + '\n' - if summary.count('FAIL') > 0 : + if summary.count('FAIL') > 0: message = 'FAIL' wr = sys.stderr.write - else : + else: message = 'PASS' wr = sys.stdout.write -- GitLab From 5d4ca51dad298530c0d548ba69edc1eecaa3ce7d Mon Sep 17 00:00:00 2001 From: erodrigu <eduardo.rodrigues@cern.ch> Date: Tue, 9 May 2023 03:18:52 +0200 Subject: [PATCH 5/5] Lint Phys/StandardParticles --- .../python/StandardParticles/__init__.py | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Phys/StandardParticles/python/StandardParticles/__init__.py b/Phys/StandardParticles/python/StandardParticles/__init__.py index 310e8212f..1f277f5b3 100644 --- a/Phys/StandardParticles/python/StandardParticles/__init__.py +++ b/Phys/StandardParticles/python/StandardParticles/__init__.py @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2019 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2023 CERN for the benefit of the LHCb Collaboration # # # # This software is distributed under the terms of the GNU General Public # # Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". # @@ -8,36 +8,40 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ############################################################################### -"""Module containing AutomaticDatas corresponding to common particles from CommonParticles.StandardBasic. +""" +Module containing AutomaticDatas corresponding to common particles +from CommonParticles.StandardBasic. """ +from PhysSelPython.Wrappers import AutomaticData +from CommonParticles import StandardBasic, StandardIntermediate __author__ = 'Juan Palacios palacios@physik.uzh.ch' +from sys import modules +_this = modules[__name__] + __all__ = ['addParticleModule', 'selections', 'locations'] -from sys import modules -_this = modules[__name__] -from CommonParticles import StandardBasic, StandardIntermediate -from PhysSelPython.Wrappers import AutomaticData - selections = [] -locations = {} +locations = {} + -def addParticleModule(particleModule) : +def addParticleModule(particleModule): """ Add all the common particles in particleModule so Selections module. """ - for loc in particleModule.locations.keys() : + for loc in particleModule.locations.keys(): name = loc.split('/')[-2] global __all__ __all__ += [name] global selections - sel = AutomaticData(Location = loc) + sel = AutomaticData(Location=loc) selections += [sel] setattr(_this, name, sel) + def getStdPartAlgorithms(): """ Create the dictionary with the algorithms @@ -54,7 +58,7 @@ addParticleModule(StandardIntermediate) getStdPartAlgorithms() -if __name__ == '__main__' : +if __name__ == '__main__': print '\nStandardParticles: available Selections:\n' - for sel in selections : + for sel in selections: print '\t', sel.name() -- GitLab