Skip to content

Commit

Permalink
Python: fix warnings pointed out by new version of flake
Browse files Browse the repository at this point in the history
  • Loading branch information
andresailer committed Apr 19, 2024
1 parent 6144a4e commit f08e335
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions DDCore/python/dd4hepFactories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import optparse
import logging
import errno
from io import open
import io

logger = logging.getLogger(__name__)

Expand All @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions DDDigi/python/dddigi.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,30 @@ 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)
# ---------------------------------------------------------------------------


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)
# ---------------------------------------------------------------------------


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)
# ---------------------------------------------------------------------------
Expand All @@ -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)
# ---------------------------------------------------------------------------

Expand Down
13 changes: 7 additions & 6 deletions DDG4/python/DDG4.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ 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):
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')
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions DDG4/python/DDSim/DD4hepSimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from DDSim.Helper.UI import UI
import argparse
import logging
from io import open
import io

logger = logging.getLogger('DDSim')

Expand Down Expand Up @@ -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__
Expand Down
6 changes: 3 additions & 3 deletions DDG4/python/DDSim/Helper/Meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions DDTest/python/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 5 additions & 4 deletions etc/CreateParsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"""

from __future__ import absolute_import, unicode_literals
import io
import os
from io import open


LICENSE = """// $Id$
//==========================================================================
Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand All @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions examples/AlignDet/drivers/Shelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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---------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion examples/ClientTests/scripts/MiniTelGenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions examples/ClientTests/scripts/MiniTelSetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion examples/DDCodex/python/CODEX-b-alone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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|"+
Expand Down

0 comments on commit f08e335

Please sign in to comment.