Skip to content

Commit

Permalink
style: Fix unnecessary-map (C417) (OSGeo#4012)
Browse files Browse the repository at this point in the history
* style: Fix unnecessary-map (C417)

* Apply suggestions from code review

* style: Change outer quotes to avoid escaping inner quotes for Q003
  • Loading branch information
echoix authored Jul 11, 2024
1 parent a3543cd commit 4aa61d7
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 42 deletions.
2 changes: 1 addition & 1 deletion gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ def _projInfo(self):

for line in ret.splitlines():
if ":" in line:
key, val = map(lambda x: x.strip(), line.split(":", 1))
key, val = (x.strip() for x in line.split(":", 1))
if key in {"units"}:
val = val.lower()
projinfo[key] = val
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def _parseFormats(output, writableOnly=False):
patt = re.compile(r"\(rw\+?\)$", re.IGNORECASE)

for line in output.splitlines():
key, name = map(lambda x: x.strip(), line.strip().split(":", 1))
key, name = (x.strip() for x in line.strip().split(":", 1))
if writableOnly and not patt.search(key):
continue

Expand Down Expand Up @@ -843,7 +843,7 @@ def StoreEnvVariable(key, value=None, envFile=None):
for line in fd.readlines():
line = line.rstrip(os.linesep)
try:
k, v = map(lambda x: x.strip(), line.split(" ", 1)[1].split("=", 1))
k, v = (x.strip() for x in line.split(" ", 1)[1].split("=", 1))
except Exception as e:
sys.stderr.write(
_("%s: line skipped - unable to parse '%s'\nReason: %s\n")
Expand Down
10 changes: 3 additions & 7 deletions gui/wxpython/gmodeler/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,15 +944,11 @@ def Populate(self, data):
items = self.frame.GetModel().GetItems(objType=ModelAction)
if isinstance(self.shape, ModelCondition):
if self.GetLabel() == "ElseBlockList":
shapeItems = map(
lambda x: x.GetId(), self.shape.GetItems(items)["else"]
)
shapeItems = (x.GetId() for x in self.shape.GetItems(items)["else"])
else:
shapeItems = map(
lambda x: x.GetId(), self.shape.GetItems(items)["if"]
)
shapeItems = (x.GetId() for x in self.shape.GetItems(items)["if"])
else:
shapeItems = map(lambda x: x.GetId(), self.shape.GetItems(items))
shapeItems = (x.GetId() for x in self.shape.GetItems(items))
else:
shapeItems = []

Expand Down
18 changes: 7 additions & 11 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def Validate(self):
cmd = action.GetLog(string=False)

task = GUI(show=None).ParseCommand(cmd=cmd)
errList += map(lambda x: cmd[0] + ": " + x, task.get_cmd_error())
errList += (cmd[0] + ": " + x for x in task.get_cmd_error())

# check also variables
for opt in cmd[1:]:
Expand Down Expand Up @@ -695,7 +695,7 @@ def Run(self, log, onDone, parent=None):
parent=parent,
message=_("Variables below not defined:")
+ "\n\n"
+ "\n".join(map(lambda x: "%s: %s (%s)" % (x[0], x[1], x[2]), err)),
+ "\n".join(f"{x[0]}: {x[1]} ({x[2]})" for x in err),
)
return

Expand Down Expand Up @@ -735,9 +735,7 @@ def Run(self, log, onDone, parent=None):

# split condition
# TODO: this part needs some better solution
condVar, condText = map(
lambda x: x.strip(), re.split(r"\s* in \s*", cond)
)
condVar, condText = (x.strip() for x in re.split(r"\s* in \s*", cond))
pattern = re.compile("%" + condVar)
# for vars()[condVar] in eval(condText): ?
vlist = []
Expand Down Expand Up @@ -2668,9 +2666,7 @@ def _writeItem(self, item, ignoreBlock=True, variables={}):
value = '"' + value + '"'
cond = pattern.sub(value, cond)
if isinstance(item, ModelLoop):
condVar, condText = map(
lambda x: x.strip(), re.split(r"\s* in \s*", cond)
)
condVar, condText = (x.strip() for x in re.split(r"\s* in \s*", cond))
cond = "%sfor %s in " % (" " * self.indent, condVar)
if condText[0] == "`" and condText[-1] == "`":
task = GUI(show=None).ParseCommand(cmd=utils.split(condText[1:-1]))
Expand Down Expand Up @@ -3499,21 +3495,21 @@ def cleanup():
r""" %s("g.remove", flags="f", type="raster",
name=%s)
"""
% (run_command, ",".join(map(lambda x: '"' + x + '"', rast)))
% (run_command, ",".join(f'"{x}"' for x in rast3d))
)
if vect:
self.fd.write(
r""" %s("g.remove", flags="f", type="vector",
name=%s)
"""
% (run_command, ",".join(map(lambda x: '"' + x + '"', vect)))
% (run_command, ",".join(('"' + x + '"' for x in vect)))
)
if rast3d:
self.fd.write(
r""" %s("g.remove", flags="f", type="raster_3d",
name=%s)
"""
% (run_command, ",".join(map(lambda x: '"' + x + '"', rast3d)))
% (run_command, ",".join(f'"{x}"' for x in rast3d))
)
if not rast and not vect and not rast3d:
self.fd.write(" pass\n")
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@ def OnCheckItem(index=None, flag=None, event=None):
tab[section].SetupScrolling(True, True, 10, 10)
tab[section].Layout()
minsecsizes = tabsizer[section].GetSize()
maxsizes = list(map(lambda x: max(maxsizes[x], minsecsizes[x]), (0, 1)))
maxsizes = [max(maxsizes[x], minsecsizes[x]) for x in (0, 1)]

# TODO: be less arbitrary with these 600
self.panelMinHeight = 100
Expand Down
8 changes: 4 additions & 4 deletions gui/wxpython/gui_core/gselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2291,8 +2291,8 @@ def hasRastSameProjAsLocation(dsn, table=None):
lines = ret.splitlines()
projectionMatch = "0"
if lines:
bandNumber, bandType, projectionMatch = map(
lambda x: x.strip(), lines[0].split(",")
bandNumber, bandType, projectionMatch = (
x.strip() for x in lines[0].split(",")
)

return projectionMatch
Expand Down Expand Up @@ -2321,8 +2321,8 @@ def getProjMatchCaption(projectionMatch):

layerId = 1
for line in ret.splitlines():
layerName, featureType, projectionMatch, geometryColumn = map(
lambda x: x.strip(), line.split(",")
layerName, featureType, projectionMatch, geometryColumn = (
x.strip() for x in line.split(",")
)
projectionMatchCaption = getProjMatchCaption(projectionMatch)
grassName = GetValidLayerName(layerName)
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/lmgr/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,8 +1981,8 @@ def AddOrUpdateMap(self, mapName, ltype):
self.AddMaps([mapName], ltype, check=True)
else:
display = self.GetMapDisplay()
mapLayers = map(
lambda x: x.GetName(), display.GetMap().GetListOfLayers(ltype=ltype)
mapLayers = (
x.GetName() for x in display.GetMap().GetListOfLayers(ltype=ltype)
)
if mapName in mapLayers:
display.GetWindow().UpdateMap(render=True)
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/main_window/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,8 +2129,8 @@ def AddOrUpdateMap(self, mapName, ltype):
self.AddMaps([mapName], ltype, check=True)
else:
display = self.GetMapDisplay()
mapLayers = map(
lambda x: x.GetName(), display.GetMap().GetListOfLayers(ltype=ltype)
mapLayers = (
x.GetName() for x in display.GetMap().GetListOfLayers(ltype=ltype)
)
if mapName in mapLayers:
display.GetWindow().UpdateMap(render=True)
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/modules/colorrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def ReadColorTable(self, ctable):
minim = maxim = count = 0
for line in ctable.splitlines():
try:
value, color = map(lambda x: x.strip(), line.split(" "))
value, color = (x.strip() for x in line.split(" "))
except ValueError:
GMessage(parent=self, message=_("Invalid color table format"))
self.rulesPanel.Clear()
Expand Down
4 changes: 1 addition & 3 deletions gui/wxpython/timeline/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,7 @@ def SetDatasets(self, datasets):
GError(parent=self, message=str(error), showTraceback=False)
return
self.datasets = datasets
self.datasetSelect.SetValue(
",".join(map(lambda x: x[0] + "@" + x[1], datasets))
)
self.datasetSelect.SetValue(",".join(f"{x[0]}@{x[1]}" for x in datasets))
self._redraw()

def Show3D(self, show):
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/tplot/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,15 +1268,15 @@ def SetDatasets(
except:
self.coorval.SetValue(",".join(coors))
if self.datasetsV:
vdatas = ",".join(map(lambda x: x[0] + "@" + x[1], self.datasetsV))
vdatas = ",".join(f"{x[0]}@{x[1]}" for x in self.datasetsV)
self.datasetSelectV.SetValue(vdatas)
if attr:
self.attribute.SetValue(attr)
if cats:
self.cats.SetValue(cats)
if self.datasetsR:
self.datasetSelectR.SetValue(
",".join(map(lambda x: x[0] + "@" + x[1], self.datasetsR))
",".join(f"{x[0]}@{x[1]}" for x in self.datasetsR)
)
if title:
self.title.SetValue(title)
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ ignore = [
"C405", # unnecessary-literal-set
"C414", # unnecessary-double-cast-or-process
"C416", # unnecessary-comprehension
"C417", # unnecessary-map
"C419", # unnecessary-comprehension-in-call
"COM812", # missing-trailing-comma
"COM818", # trailing-comma-on-bare-tuple
Expand Down
6 changes: 2 additions & 4 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ def get_real_command(cmd):
if os.path.splitext(cmd)[1] == ".py":
cmd = cmd[:-3]
# PATHEXT is necessary to check on Windows (force lowercase)
pathext = list(
map(lambda x: x.lower(), os.environ["PATHEXT"].split(os.pathsep))
)
pathext = [x.lower() for x in os.environ["PATHEXT"].split(os.pathsep)]
if ".py" not in pathext:
# we assume that PATHEXT contains always '.py'
os.environ["PATHEXT"] = ".py;" + os.environ["PATHEXT"]
Expand Down Expand Up @@ -1287,7 +1285,7 @@ def region_env(region3d=False, flags=None, env=None, **kwargs):
with open(windfile, "r") as fd:
grass_region = ""
for line in fd.readlines():
key, value = map(lambda x: x.strip(), line.split(":", 1))
key, value = (x.strip() for x in line.split(":", 1))
if kwargs and key not in {"proj", "zone"}:
continue
if (
Expand Down
2 changes: 1 addition & 1 deletion python/grass/semantic_label/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def find_file(self, semantic_label):
shortcut
and config[root]["shortcut"].upper() == shortcut.upper()
and band.upper()
in map(lambda x: x.upper(), config[root]["bands"].keys())
in (x.upper() for x in config[root]["bands"].keys())
):
return filename

Expand Down

0 comments on commit 4aa61d7

Please sign in to comment.