From 8d5d16b6a71f02fd081fd25080be22fc6f33363a Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Fri, 11 Sep 2020 10:11:52 +0000 Subject: [PATCH 1/6] Clean up some code to make it Python 3 compatible --- .../tests/test_load_modules.py | 51 +++++++++---------- Phys/SelPy/python/SelPy/utils.py | 6 +-- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Phys/CommonParticles/tests/test_load_modules.py b/Phys/CommonParticles/tests/test_load_modules.py index 5ae75ef83..6dd5a6bb6 100755 --- a/Phys/CommonParticles/tests/test_load_modules.py +++ b/Phys/CommonParticles/tests/test_load_modules.py @@ -13,8 +13,6 @@ ''' Test loading various CommonParticles in the Analysis environment. Needs to be extended. ''' -from past.builtins import cmp -from builtins import filter __author__ = 'Juan Palacios juan.palacios@nikhef.nl' import sys @@ -46,32 +44,29 @@ def test_load_all(): if '__main__' == __name__: + # get all test function names from the current module + test_names = [k for k in locals() if 'test_' in k] - import sys - - test_names = [k for k in list(locals().keys()) if k.count('test_') > 0] - - __tests = [x for x in locals().items() if x[0] in test_names] - - message = '' - summary = '\n' - length = len(sorted(test_names, - cmp=lambda x, y: cmp(len(y), len(x)))[0]) + 2 - - for test in __tests: + # run the tests and collect results + passed = {} + for test in test_names: try: - test[1]() - message = 'PASS' + locals()[test]() + passed[test] = True except: - message = "FAIL" - summary += test[0].ljust(length) + ':' + message.rjust(10) + '\n' - - if summary.count('FAIL') > 0: - message = 'FAIL' - wr = sys.stderr.write - else: - message = 'PASS' - wr = sys.stdout.write - - summary += 'Global'.ljust(length) + ':' + message.rjust(10) + '\n\n' - wr(summary) + passed[test] = False + + # Add global success status to the results + success = all(passed.values()) + test_names.append('Global') + passed['Global'] = success + + stream = sys.stdout if success else sys.stderr + + # print report table + stream.write('\n') + max_length = max(len(x) for x in test_names) + stream.writelines("{:<{}} :{:>10}\n".format( + test, max_length, 'PASS' if passed[test] else 'FALSE') + for test in test_names) + stream.write('\n') diff --git a/Phys/SelPy/python/SelPy/utils.py b/Phys/SelPy/python/SelPy/utils.py index 586eec36b..9e3887ace 100644 --- a/Phys/SelPy/python/SelPy/utils.py +++ b/Phys/SelPy/python/SelPy/utils.py @@ -292,10 +292,10 @@ def update_dict_overlap(dict0, dict1): Replace entries from dict0 with those from dict1 that have keys present in dict0. """ - overlap_keys = filter(dict1.has_key, dict0.keys()) result = dict(dict0) - for key in overlap_keys: - result[key] = dict1[key] + for key in dict0: + if key in dict1: + result[key] = dict1[key] return result -- GitLab From 06c680576884daaf611f0e98fc443a372a5896e3 Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Sat, 12 Sep 2020 15:06:45 +0000 Subject: [PATCH 2/6] Fix iteration over locals() --- Phys/CommonParticles/tests/test_load_modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Phys/CommonParticles/tests/test_load_modules.py b/Phys/CommonParticles/tests/test_load_modules.py index 6dd5a6bb6..fcfb4106b 100755 --- a/Phys/CommonParticles/tests/test_load_modules.py +++ b/Phys/CommonParticles/tests/test_load_modules.py @@ -45,7 +45,7 @@ def test_load_all(): if '__main__' == __name__: # get all test function names from the current module - test_names = [k for k in locals() if 'test_' in k] + test_names = [k for k in list(locals()) if 'test_' in k] # run the tests and collect results passed = {} -- GitLab From 13c57cb9370fbcd99df5fdacaaa981e9477b0935 Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Mon, 14 Sep 2020 10:37:14 +0200 Subject: [PATCH 3/6] Use pytest instead of custom test runners in CommonParticles --- Phys/CommonParticles/CMakeLists.txt | 5 ++- .../test_inputlocations_consistency.qmt | 17 -------- .../commonparticles.qms/test_load_modules.qmt | 17 -------- .../tests/test_inputlocations_consistency.py | 39 ++++++------------- .../tests/test_load_modules.py | 35 +---------------- 5 files changed, 16 insertions(+), 97 deletions(-) delete mode 100644 Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_inputlocations_consistency.qmt delete mode 100644 Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_load_modules.qmt diff --git a/Phys/CommonParticles/CMakeLists.txt b/Phys/CommonParticles/CMakeLists.txt index af3212cc0..c8549f944 100644 --- a/Phys/CommonParticles/CMakeLists.txt +++ b/Phys/CommonParticles/CMakeLists.txt @@ -1,5 +1,5 @@ ############################################################################### -# (c) Copyright 2000-2018 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2020 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". # @@ -19,4 +19,5 @@ gaudi_depends_on_subdirs(Phys/DaVinciKernel gaudi_install_python_modules() -gaudi_add_test(QMTest QMTEST) +gaudi_add_test(pytest + COMMAND python -m pytest -v ${CMAKE_CURRENT_SOURCE_DIR}/tests) diff --git a/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_inputlocations_consistency.qmt b/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_inputlocations_consistency.qmt deleted file mode 100644 index 5f4d30f62..000000000 --- a/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_inputlocations_consistency.qmt +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - $COMMONPARTICLESROOT/tests/test_inputlocations_consistency.py - - diff --git a/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_load_modules.qmt b/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_load_modules.qmt deleted file mode 100644 index 55ece493f..000000000 --- a/Phys/CommonParticles/tests/qmtest/commonparticles.qms/test_load_modules.qmt +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - $COMMONPARTICLESROOT/tests/test_load_modules.py - - diff --git a/Phys/CommonParticles/tests/test_inputlocations_consistency.py b/Phys/CommonParticles/tests/test_inputlocations_consistency.py index 548932d8f..0475ba25a 100755 --- a/Phys/CommonParticles/tests/test_inputlocations_consistency.py +++ b/Phys/CommonParticles/tests/test_inputlocations_consistency.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2018 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2020 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,38 +9,23 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ############################################################################### -#$Id: test_load_modules.py,v 1.1 2010-04-22 14:44:53 jpalac Exp $ ''' Test loading various CommonParticles in the Stripping environment. Needs to be extended. ''' __author__ = 'Juan Palacios juan.palacios@nikhef.nl' -import sys -sys.path.append('../python') -from CommonParticles import StandardBasic, StandardIntermediate +def test(): + from CommonParticles import StandardBasic, StandardIntermediate -outputLocations = list(StandardBasic.locations.keys()) + list( - StandardIntermediate.locations.keys()) + outputLocations = list(StandardBasic.locations) + list( + StandardIntermediate.locations) -algs = list(StandardBasic.locations.values()) + list( - StandardIntermediate.locations.values()) + algs = list(StandardBasic.locations.values()) + list( + StandardIntermediate.locations.values()) -errors = {} - -message = "" - -for alg in algs: - if not hasattr(alg, 'Inputs'): continue - locs = alg.Inputs - for loc in locs: - if not loc in outputLocations: - message += "ERROR: Algorithm " + alg.name( - ) + ".Input " + loc + " not in output locations\n" - errors[alg.name()] = loc - -if len(errors) > 0: - sys.stderr.write('test_inputlocations_consistency: FAIL\n') - sys.stderr.write(message) -else: - sys.stdout.write('test_inputlocations_consistency: PASS\n') + for alg in algs: + if not hasattr(alg, 'Inputs'): continue + for inputLocation in alg.Inputs: + assert inputLocation in outputLocations, \ + 'Algorithm {}.Input {} not in output locations'.format(alg, inputLocation) diff --git a/Phys/CommonParticles/tests/test_load_modules.py b/Phys/CommonParticles/tests/test_load_modules.py index fcfb4106b..befdff5cb 100755 --- a/Phys/CommonParticles/tests/test_load_modules.py +++ b/Phys/CommonParticles/tests/test_load_modules.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2018 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2020 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,15 +9,11 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ############################################################################### -#$Id: test_load_modules.py,v 1.1 2010-04-22 14:44:53 jpalac Exp $ ''' Test loading various CommonParticles in the Analysis environment. Needs to be extended. ''' __author__ = 'Juan Palacios juan.palacios@nikhef.nl' -import sys -sys.path.append('../python') - def test_load_utils(): from CommonParticles import Utils @@ -41,32 +37,3 @@ def test_load_standard_intermediate(): def test_load_all(): import CommonParticles - - -if '__main__' == __name__: - # get all test function names from the current module - test_names = [k for k in list(locals()) if 'test_' in k] - - # run the tests and collect results - passed = {} - for test in test_names: - try: - locals()[test]() - passed[test] = True - except: - passed[test] = False - - # Add global success status to the results - success = all(passed.values()) - test_names.append('Global') - passed['Global'] = success - - stream = sys.stdout if success else sys.stderr - - # print report table - stream.write('\n') - max_length = max(len(x) for x in test_names) - stream.writelines("{:<{}} :{:>10}\n".format( - test, max_length, 'PASS' if passed[test] else 'FALSE') - for test in test_names) - stream.write('\n') -- GitLab From 29aa05ea19375584faded5710cfa4db916cf91e9 Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Mon, 14 Sep 2020 10:39:48 +0200 Subject: [PATCH 4/6] Fixes for Python 3 --- Phys/CommonParticles/python/CommonParticles/StdTightDplus.py | 2 +- .../JetAccessories/python/JetAccessories/ParticleFlow_Config.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Phys/CommonParticles/python/CommonParticles/StdTightDplus.py b/Phys/CommonParticles/python/CommonParticles/StdTightDplus.py index 9b24b7a59..d215fdbd2 100644 --- a/Phys/CommonParticles/python/CommonParticles/StdTightDplus.py +++ b/Phys/CommonParticles/python/CommonParticles/StdTightDplus.py @@ -60,7 +60,7 @@ locations.update(updateDoD(StdVeryTightDsplus2KKPi)) ########################################################################################## # D+ -> 3 pi is a clone of D+ -> K Pi Pi # -from StdLooseDplus import StdLooseDplus2KPiPi +from CommonParticles.StdLooseDplus import StdLooseDplus2KPiPi StdTightDplus2PiPiPi = StdLooseDplus2KPiPi.clone("StdTightDplus2PiPiPi") StdTightDplus2PiPiPi.Inputs = ["Phys/StdLoosePions/Particles"] StdTightDplus2PiPiPi.DecayDescriptor = "[D+ -> pi- pi+ pi+]cc" diff --git a/Phys/JetAccessories/python/JetAccessories/ParticleFlow_Config.py b/Phys/JetAccessories/python/JetAccessories/ParticleFlow_Config.py index d29e78975..0b1b725f7 100644 --- a/Phys/JetAccessories/python/JetAccessories/ParticleFlow_Config.py +++ b/Phys/JetAccessories/python/JetAccessories/ParticleFlow_Config.py @@ -227,7 +227,7 @@ class ParticleFlowConf(object): alg.TrackSelectorType = "DelegatingTrackSelector" alg.addTool(DelegatingTrackSelector, name="TrackSelector") tracktypes = TrackCuts.keys() - alg.TrackSelector.TrackTypes = self.TrackSelector.keys() + alg.TrackSelector.TrackTypes = list(self.TrackSelector) for type in tracktypes: self.setupTypeTrackSelector(type, alg.TrackSelector, TrackCuts) -- GitLab From 10d8306e80a23fb82a7ee858c391fa5fbc77c089 Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Mon, 14 Sep 2020 11:29:39 +0200 Subject: [PATCH 5/6] Use pytest instead of custom test runners in LoKiPhys --- Phys/LoKiPhys/CMakeLists.txt | 3 +- Phys/LoKiPhys/python/LoKiPhys/tests.py | 37 ++++-------------------- Phys/LoKiPhys/tests/qmtest/lokiphys0.qmt | 15 ---------- 3 files changed, 7 insertions(+), 48 deletions(-) delete mode 100644 Phys/LoKiPhys/tests/qmtest/lokiphys0.qmt diff --git a/Phys/LoKiPhys/CMakeLists.txt b/Phys/LoKiPhys/CMakeLists.txt index 5191b71b0..7e8207aaa 100644 --- a/Phys/LoKiPhys/CMakeLists.txt +++ b/Phys/LoKiPhys/CMakeLists.txt @@ -49,4 +49,5 @@ endif() gaudi_install_python_modules() -gaudi_add_test(QMTest QMTEST) +gaudi_add_test(pytest + COMMAND python -m pytest -v ${CMAKE_CURRENT_SOURCE_DIR}/python/LoKiPhys/tests.py) diff --git a/Phys/LoKiPhys/python/LoKiPhys/tests.py b/Phys/LoKiPhys/python/LoKiPhys/tests.py index 7f14ab122..08b55afcf 100755 --- a/Phys/LoKiPhys/python/LoKiPhys/tests.py +++ b/Phys/LoKiPhys/python/LoKiPhys/tests.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2018 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2020 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". # @@ -41,15 +41,14 @@ from LoKiCore.math import * # ============================================================================= ## The most trivial test function -def test0(): +def test_basic(): """ The most trivial test function """ from LoKiPhys.decorators import _decorated print('LoKiPhysTest: decorated objects %s' % len(_decorated)) - pass ## the test function for various "functional streamers" -def test1(): +def test_function_streamers(): """ The test function for various 'functional streamers' """ @@ -93,7 +92,7 @@ def test1(): ## tets smart references -def test2(): +def test_smart_references(): ## ## simple check @@ -117,38 +116,12 @@ def test2(): except AssertionError as e: print(e) - print(80 * '*') - ## test monitoring stuff -def test3(): +def test_monitoring(): p = LHCb.Particle() a = monitor(PT) print(a(p)) - - print(80 * '*') - - -# ============================================================================= -## Perform all known tests -def testAll(): - """ - Perform all known tests - """ - test0() - test1() - test2() ## smart referemnces - test3() - - -# ============================================================================= -if '__main__' == __name__: - testAll() - # ============================================================================= - - # ============================================================================= - # The END - # ============================================================================= diff --git a/Phys/LoKiPhys/tests/qmtest/lokiphys0.qmt b/Phys/LoKiPhys/tests/qmtest/lokiphys0.qmt deleted file mode 100644 index 9693e1545..000000000 --- a/Phys/LoKiPhys/tests/qmtest/lokiphys0.qmt +++ /dev/null @@ -1,15 +0,0 @@ - - - - - ../../python/LoKiPhys/tests.py - -- GitLab From 956e26d964eec0a1eb3d840c78fdbc265d2c9ea5 Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Mon, 14 Sep 2020 11:45:42 +0200 Subject: [PATCH 6/6] Fix Python 3 compatibility of LoKiPhys --- Phys/LoKiPhys/python/LoKiPhys/Phys.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Phys/LoKiPhys/python/LoKiPhys/Phys.py b/Phys/LoKiPhys/python/LoKiPhys/Phys.py index 59bfee6b8..2853d7ad2 100755 --- a/Phys/LoKiPhys/python/LoKiPhys/Phys.py +++ b/Phys/LoKiPhys/python/LoKiPhys/Phys.py @@ -1,6 +1,6 @@ #!/usr/bin/env python ############################################################################### -# (c) Copyright 2000-2018 CERN for the benefit of the LHCb Collaboration # +# (c) Copyright 2000-2020 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". # @@ -400,7 +400,8 @@ for t in (LHCb.Particle, LHCb.Vertex, LHCb.VertexBase, LHCb.RecVertex, LHCb.Track, LHCb.CaloDigit, LHCb.CaloCluster, LHCb.CaloHypo): _SR = cpp.SmartRef(t) - _SR.__nonzero__ = lambda s: bool(s.target()) + _SR.__bool__ = lambda s: bool(s.target()) + _SR.__nonzero__ = _SR.__bool__ # Python 2 compatibility _SR.__repr__ = _LCF._repr_SR_ _SR.__str__ = _LCF._str_SR_ _SR.__getattr__ = _LCF._getattr_SR_ -- GitLab