From f08e3359b2538cbae320e9fb9ff51e3eb0d780bb Mon Sep 17 00:00:00 2001 From: Andre Sailer Date: Fri, 19 Apr 2024 17:50:33 +0200 Subject: [PATCH] Python: fix warnings pointed out by new version of flake --- DDCore/python/dd4hepFactories.py | 4 ++-- DDDigi/python/dddigi.py | 10 +++++----- DDG4/python/DDG4.py | 13 +++++++------ DDG4/python/DDSim/DD4hepSimulation.py | 4 ++-- DDG4/python/DDSim/Helper/Meta.py | 6 +++--- DDTest/python/test_import.py | 4 ++-- etc/CreateParsers.py | 9 +++++---- examples/AlignDet/drivers/Shelf.py | 4 ++-- examples/ClientTests/scripts/MiniTelGenerate.py | 2 +- examples/ClientTests/scripts/MiniTelSetup.py | 8 ++++++-- examples/DDCodex/python/CODEX-b-alone.py | 2 +- 11 files changed, 36 insertions(+), 30 deletions(-) diff --git a/DDCore/python/dd4hepFactories.py b/DDCore/python/dd4hepFactories.py index 21a590e1a..4a77012e9 100755 --- a/DDCore/python/dd4hepFactories.py +++ b/DDCore/python/dd4hepFactories.py @@ -16,7 +16,7 @@ import optparse import logging import errno -from io import open +import io logger = logging.getLogger(__name__) @@ -42,7 +42,7 @@ def scanPath(self): def readComponents(self, fname): logger.info('+== Search component file: ' + fname) - file = open(fname, "r") + file = io.open(fname, "r") lines = file.readlines() dirname = os.path.dirname(fname) for line in lines: diff --git a/DDDigi/python/dddigi.py b/DDDigi/python/dddigi.py index 78de93030..609485cd6 100644 --- a/DDDigi/python/dddigi.py +++ b/DDDigi/python/dddigi.py @@ -203,14 +203,14 @@ def _kernel_terminate(self): def _default_adopt(self, action): - getattr(self, '__adopt')(action.get()) + self.__adopt(action.get()) # --------------------------------------------------------------------------- def _adopt_event_action(self, action): " Helper to convert DigiActions objects to DigiEventAction " proc = Interface.toEventAction(_get_action(action)) - attr = getattr(self, '__adopt') + attr = self.__adopt attr(proc) # --------------------------------------------------------------------------- @@ -218,7 +218,7 @@ def _adopt_event_action(self, action): def _adopt_container_processor(self, action, processor_argument): " Helper to convert DigiActions objects to DigiEventAction " parent = Interface.toContainerSequenceAction(_get_action(self)) - attr = getattr(parent, 'adopt_processor') + attr = parent.adopt_processor proc = Interface.toContainerProcessor(_get_action(action)) attr(proc, processor_argument) # --------------------------------------------------------------------------- @@ -226,7 +226,7 @@ def _adopt_container_processor(self, action, processor_argument): def _adopt_segment_processor(self, action, processor_argument): " Helper to convert DigiActions objects to DigiEventAction " - attr = getattr(_get_action(self), '__adopt_segment_processor') + attr = _get_action(self).__adopt_segment_processor proc = Interface.toContainerProcessor(_get_action(action)) attr(proc, processor_argument) # --------------------------------------------------------------------------- @@ -245,7 +245,7 @@ def _adopt_sequence_action(self, name, **options): def _adopt_processor(self, action, containers): proc = Interface.toContainerProcessor(_get_action(action)) - attr = getattr(_get_action(self), '__adopt_processor') + attr = _get_action(self).__adopt_processor attr(proc, containers) # --------------------------------------------------------------------------- diff --git a/DDG4/python/DDG4.py b/DDG4/python/DDG4.py index 336ec5405..69b39075b 100644 --- a/DDG4/python/DDG4.py +++ b/DDG4/python/DDG4.py @@ -254,9 +254,9 @@ def _adopt(self, action): self.__adopt(action.get()) _import_class('Sim', obj) o = getattr(current, obj) - setattr(o, '__adopt', getattr(o, 'adopt')) - setattr(o, 'adopt', _adopt) - setattr(o, 'add', _adopt) + o.__adopt = o.adopt + o.adopt = _adopt + o.add = _adopt def _setup_callback(obj): @@ -264,8 +264,8 @@ def _adopt(self, action): self.__adopt(action.get(), action.callback()) _import_class('Sim', obj) o = getattr(current, obj) - setattr(o, '__adopt', getattr(o, 'add')) - setattr(o, 'add', _adopt) + o.__adopt = o.add + o.add = _adopt _setup_callback('Geant4ActionPhase') @@ -443,7 +443,8 @@ def ui(self): \author M.Frank """ - ui_name = getattr(self.master(), 'UI') + # calls __getattr__ implicitly, which calls getKernelProperty + ui_name = self.master().UI return self.master().globalAction(ui_name) def addUserInitialization(self, worker, worker_args=None, master=None, master_args=None): diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py index 660e50a38..5d4b3a42b 100644 --- a/DDG4/python/DDSim/DD4hepSimulation.py +++ b/DDG4/python/DDSim/DD4hepSimulation.py @@ -29,7 +29,7 @@ from DDSim.Helper.UI import UI import argparse import logging -from io import open +import io logger = logging.getLogger('DDSim') @@ -110,7 +110,7 @@ def readSteeringFile(self): if not self.steeringFile: return sFileTemp = self.steeringFile - exec(compile(open(self.steeringFile).read(), self.steeringFile, 'exec'), globs, locs) + exec(compile(io.open(self.steeringFile).read(), self.steeringFile, 'exec'), globs, locs) for _name, obj in locs.items(): if isinstance(obj, DD4hepSimulation): self.__dict__ = obj.__dict__ diff --git a/DDG4/python/DDSim/Helper/Meta.py b/DDG4/python/DDSim/Helper/Meta.py index 45b406da8..c7db1cd59 100644 --- a/DDG4/python/DDSim/Helper/Meta.py +++ b/DDG4/python/DDSim/Helper/Meta.py @@ -2,9 +2,9 @@ from DDSim.Helper.ConfigHelper import ConfigHelper import datetime +import io import os import logging -from io import open logger = logging.getLogger(__name__) @@ -76,12 +76,12 @@ def addParametersToRunHeader(sim): # steeringFile content if sim.steeringFile and os.path.exists(sim.steeringFile) and os.path.isfile(sim.steeringFile): - with open(sim.steeringFile) as sFile: + with io.open(sim.steeringFile) as sFile: runHeader["SteeringFileContent"] = sFile.read() # macroFile content if sim.macroFile and os.path.exists(sim.macroFile) and os.path.isfile(sim.macroFile): - with open(sim.macroFile) as mFile: + with io.open(sim.macroFile) as mFile: runHeader["MacroFileContent"] = mFile.read() # add command line diff --git a/DDTest/python/test_import.py b/DDTest/python/test_import.py index 40796d3d2..7210895b9 100644 --- a/DDTest/python/test_import.py +++ b/DDTest/python/test_import.py @@ -44,10 +44,10 @@ def test_module(moduleName): print(traceback.print_exc()) if moduleName in ALLOWED_TO_FAIL: - warnings.warn(msg) + warnings.warn(msg, stacklevel=2) pytest.skip("WARN: " + msg) elif moduleName in GRAPHIC_MODULES: - warnings.warn(msg + "(Possibly due to system graphic libraries not present)") + warnings.warn(msg + "(Possibly due to system graphic libraries not present)", stacklevel=2) pytest.skip("WARN: " + msg + "(Possibly due to system graphic libraries not present)") else: pytest.fail("ERROR: " + msg) diff --git a/etc/CreateParsers.py b/etc/CreateParsers.py index 94273ce19..b5908a49a 100644 --- a/etc/CreateParsers.py +++ b/etc/CreateParsers.py @@ -13,8 +13,9 @@ """ from __future__ import absolute_import, unicode_literals +import io import os -from io import open + LICENSE = """// $Id$ //========================================================================== @@ -75,7 +76,7 @@ def createMappedFile(typ): fileContent = LICENSE + fileContent if os.path.exists(filename): os.remove(filename) - with open(filename, "w") as parseFile: + with io.open(filename, "w") as parseFile: parseFile.write(fileContent) @@ -92,7 +93,7 @@ def createContainerFile(typ, cont): fileContent = LICENSE + fileContent if os.path.exists(filename): os.remove(filename) - with open(filename, "w") as parseFile: + with io.open(filename, "w") as parseFile: parseFile.write(fileContent) @@ -110,7 +111,7 @@ def createMapFile(typ, mtype): fileContent = LICENSE + fileContent if os.path.exists(filename): os.remove(filename) - with open(filename, "w") as parseFile: + with io.open(filename, "w") as parseFile: parseFile.write(fileContent) diff --git a/examples/AlignDet/drivers/Shelf.py b/examples/AlignDet/drivers/Shelf.py index df1ed6015..462433e27 100644 --- a/examples/AlignDet/drivers/Shelf.py +++ b/examples/AlignDet/drivers/Shelf.py @@ -39,7 +39,7 @@ def detector_Shelf(description, det): description.material('Carbon')) b_vol.setVisAttributes(description, book.vis) x, y, z = plane.x - book.x, plane.y, -plane.z + book.z - for n in range(book.number): + for _n in range(book.number): e_vol.placeVolume(b_vol, Position(x, y, z)) # noqa: F821 z += 2 * book.z + book.getF('dz') @@ -53,7 +53,7 @@ def detector_Shelf(description, det): phv.addPhysVolID('id', det.id) de.addPlacement(phv) x, y, z = 0, book.y + plane.y - 2 * plane.getF('dy'), 0 - for n in range(plane.number): + for _n in range(plane.number): g_vol.placeVolume(e_vol, Position(x, y, z)) # noqa: F821 y += plane.getF('dy') # ---Return detector element--------------------------------------------------------------------------------- diff --git a/examples/ClientTests/scripts/MiniTelGenerate.py b/examples/ClientTests/scripts/MiniTelGenerate.py index c177f8ba7..7197e163c 100644 --- a/examples/ClientTests/scripts/MiniTelGenerate.py +++ b/examples/ClientTests/scripts/MiniTelGenerate.py @@ -33,7 +33,7 @@ def run(): cmds = [] if not args.runs: args.runs = 1 - for i in range(int(args.runs)): + for _i in range(int(args.runs)): cmds.append(run) cmds.append('/ddg4/UI/terminate') m.ui.Commands = cmds diff --git a/examples/ClientTests/scripts/MiniTelSetup.py b/examples/ClientTests/scripts/MiniTelSetup.py index 6f0a01904..14765f9c8 100644 --- a/examples/ClientTests/scripts/MiniTelSetup.py +++ b/examples/ClientTests/scripts/MiniTelSetup.py @@ -41,8 +41,12 @@ def configure(self, output_level=None): act.OutputLevel = output_level return self - def defineOutput(self, output='MiniTel_' + time.strftime('%Y-%m-%d_%H-%M')): + def defineOutput(self, output=None): + if output is None: + output = 'MiniTel_' + time.strftime('%Y-%m-%d_%H-%M') return DDG4TestSetup.Setup.defineOutput(self, output) - def defineEdm4hepOutput(self, output='MiniTel_' + time.strftime('%Y-%m-%d_%H-%M')): + def defineEdm4hepOutput(self, output=None): + if output is None: + output = 'MiniTel_' + time.strftime('%Y-%m-%d_%H-%M') return DDG4TestSetup.Setup.defineEdm4hepOutput(self, output) diff --git a/examples/DDCodex/python/CODEX-b-alone.py b/examples/DDCodex/python/CODEX-b-alone.py index 6e358c444..df02245ca 100755 --- a/examples/DDCodex/python/CODEX-b-alone.py +++ b/examples/DDCodex/python/CODEX-b-alone.py @@ -55,7 +55,7 @@ def run(): # direction=(0.866025,0,0.5), position='(0,0,12650)') # position='(0,0,0)') - setattr(gun, 'print', True) + gun.print = True """ gen = DDG4.GeneratorAction(kernel,"Geant4InputAction/Input") # gen.Input = "Geant4EventReaderHepMC|"+