From fc9b37843c3edff46c5397e66a8bb0c31de97526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sun, 30 Jun 2024 19:48:17 +0000 Subject: [PATCH] style: Fixes if-stmt-min-max (PLR1730) Concerns Pylint rules "consider-using-min-builtin / R1730" and "consider-using-max-builtin / R1731" Using `ruff check --output-format=concise --select PLR1730 --preview --fix`. --- gui/wxpython/core/gcmd.py | 6 ++-- gui/wxpython/dbmgr/base.py | 6 ++-- gui/wxpython/gcp/manager.py | 18 ++++------ gui/wxpython/gcp/statusbar.py | 3 +- gui/wxpython/gmodeler/canvas.py | 3 +- gui/wxpython/gmodeler/panels.py | 12 +++---- gui/wxpython/gui_core/prompt.py | 6 ++-- gui/wxpython/image2target/ii2t_manager.py | 18 ++++------ gui/wxpython/image2target/ii2t_statusbar.py | 3 +- gui/wxpython/location_wizard/dialogs.py | 6 ++-- gui/wxpython/modules/colorrules.py | 12 +++---- gui/wxpython/nviz/tools.py | 3 +- gui/wxpython/photo2image/ip2i_manager.py | 18 ++++------ gui/wxpython/photo2image/ip2i_statusbar.py | 3 +- gui/wxpython/vnet/dialogs.py | 6 ++-- python/grass/benchmark/runners.py | 3 +- python/grass/imaging/images2swf.py | 3 +- python/grass/jupyter/region.py | 12 +++---- python/grass/temporal/temporal_granularity.py | 36 +++++++------------ scripts/d.correlate/d.correlate.py | 12 +++---- scripts/d.rast.edit/d.rast.edit.py | 18 ++++------ scripts/d.rast.leg/d.rast.leg.py | 6 ++-- scripts/r.tileset/r.tileset.py | 12 +++---- temporal/t.rast.what/t.rast.what.py | 3 +- 24 files changed, 76 insertions(+), 152 deletions(-) diff --git a/gui/wxpython/core/gcmd.py b/gui/wxpython/core/gcmd.py index 138936fb4f5..993a1edb6b7 100644 --- a/gui/wxpython/core/gcmd.py +++ b/gui/wxpython/core/gcmd.py @@ -238,8 +238,7 @@ def _recv(self, which, maxsize): try: x = msvcrt.get_osfhandle(conn.fileno()) (read, nAvail, nMessage) = PeekNamedPipe(x, 0) - if maxsize < nAvail: - nAvail = maxsize + nAvail = min(maxsize, nAvail) if nAvail > 0: (errCode, read) = ReadFile(x, nAvail, None) except ValueError: @@ -301,8 +300,7 @@ def _recv(self, which, maxsize): def recv_some(p, t=0.1, e=1, tr=5, stderr=0): - if tr < 1: - tr = 1 + tr = max(tr, 1) x = time.time() + t y = [] r = "" diff --git a/gui/wxpython/dbmgr/base.py b/gui/wxpython/dbmgr/base.py index 3db3bf40918..309cf2ff574 100644 --- a/gui/wxpython/dbmgr/base.py +++ b/gui/wxpython/dbmgr/base.py @@ -357,10 +357,8 @@ def LoadData(self, layer, columns=None, where=None, sql=None): i = 0 for col in columns: width = self.columns[col]["length"] * 6 # FIXME - if width < 60: - width = 60 - if width > 300: - width = 300 + width = max(width, 60) + width = min(width, 300) self.SetColumnWidth(col=i, width=width) i += 1 diff --git a/gui/wxpython/gcp/manager.py b/gui/wxpython/gcp/manager.py index 3e948dc3c91..580f0e4c938 100644 --- a/gui/wxpython/gcp/manager.py +++ b/gui/wxpython/gcp/manager.py @@ -2236,14 +2236,10 @@ def GetNewExtent(self, region, map=None): e, n = errlist[i].split() fe = float(e) fn = float(n) - if fe < newreg["w"]: - newreg["w"] = fe - if fe > newreg["e"]: - newreg["e"] = fe - if fn < newreg["s"]: - newreg["s"] = fn - if fn > newreg["n"]: - newreg["n"] = fn + newreg["w"] = min(fe, newreg["w"]) + newreg["e"] = max(fe, newreg["e"]) + newreg["s"] = min(fn, newreg["s"]) + newreg["n"] = max(fn, newreg["n"]) return newreg @@ -2292,10 +2288,8 @@ def AdjustMap(self, newreg): # LL locations if self.Map.projinfo["proj"] == "ll": - if newreg["n"] > 90.0: - newreg["n"] = 90.0 - if newreg["s"] < -90.0: - newreg["s"] = -90.0 + newreg["n"] = min(newreg["n"], 90.0) + newreg["s"] = max(newreg["s"], -90.0) ce = newreg["w"] + (newreg["e"] - newreg["w"]) / 2 cn = newreg["s"] + (newreg["n"] - newreg["s"]) / 2 diff --git a/gui/wxpython/gcp/statusbar.py b/gui/wxpython/gcp/statusbar.py index 1424f7f4846..3c5ee378a1e 100644 --- a/gui/wxpython/gcp/statusbar.py +++ b/gui/wxpython/gcp/statusbar.py @@ -97,8 +97,7 @@ def Update(self): and sets the spin limits accordingly.""" self.statusbar.SetStatusText("") maximum = self.mapFrame.GetListCtrl().GetItemCount() - if maximum < 1: - maximum = 1 + maximum = max(maximum, 1) self.widget.SetRange(0, maximum) self.Show() diff --git a/gui/wxpython/gmodeler/canvas.py b/gui/wxpython/gmodeler/canvas.py index 9742df75d73..51976e443ad 100644 --- a/gui/wxpython/gmodeler/canvas.py +++ b/gui/wxpython/gmodeler/canvas.py @@ -103,8 +103,7 @@ def GetNewShapePos(self, yoffset=50): ymax = 20 for item in self.GetDiagram().GetShapeList(): y = item.GetY() + item.GetBoundingBoxMin()[1] - if y > ymax: - ymax = y + ymax = max(y, ymax) return (self.GetSize()[0] // 2, ymax + yoffset) diff --git a/gui/wxpython/gmodeler/panels.py b/gui/wxpython/gmodeler/panels.py index 71a243cfc59..b198dace36a 100644 --- a/gui/wxpython/gmodeler/panels.py +++ b/gui/wxpython/gmodeler/panels.py @@ -989,14 +989,10 @@ def OnExportImage(self, event): xmax = x + w / 2 ymin = y - h / 2 ymax = y + h / 2 - if xmin < xminImg: - xminImg = xmin - if xmax > xmaxImg: - xmaxImg = xmax - if ymin < yminImg: - yminImg = ymin - if ymax > ymaxImg: - ymaxImg = ymax + xminImg = min(xmin, xminImg) + xmaxImg = max(xmax, xmaxImg) + yminImg = min(ymin, yminImg) + ymaxImg = max(ymax, ymaxImg) size = wx.Size(int(xmaxImg - xminImg) + 50, int(ymaxImg - yminImg) + 50) bitmap = EmptyBitmap(width=size.width, height=size.height) diff --git a/gui/wxpython/gui_core/prompt.py b/gui/wxpython/gui_core/prompt.py index fa5364bbdf0..e56b73ebe58 100644 --- a/gui/wxpython/gui_core/prompt.py +++ b/gui/wxpython/gui_core/prompt.py @@ -480,10 +480,8 @@ def OnKeyPressed(self, event): self.cmdindex = self.cmdindex - 1 if event.GetKeyCode() == wx.WXK_DOWN: self.cmdindex = self.cmdindex + 1 - if self.cmdindex < 0: - self.cmdindex = 0 - if self.cmdindex > len(self.cmdbuffer) - 1: - self.cmdindex = len(self.cmdbuffer) - 1 + self.cmdindex = max(self.cmdindex, 0) + self.cmdindex = min(self.cmdindex, len(self.cmdbuffer) - 1) try: # without strip causes problem on windows diff --git a/gui/wxpython/image2target/ii2t_manager.py b/gui/wxpython/image2target/ii2t_manager.py index 2828a3b0a58..2c1487ebf63 100644 --- a/gui/wxpython/image2target/ii2t_manager.py +++ b/gui/wxpython/image2target/ii2t_manager.py @@ -2174,14 +2174,10 @@ def GetNewExtent(self, region, map=None): e, n = errlist[i].split() fe = float(e) fn = float(n) - if fe < newreg["w"]: - newreg["w"] = fe - if fe > newreg["e"]: - newreg["e"] = fe - if fn < newreg["s"]: - newreg["s"] = fn - if fn > newreg["n"]: - newreg["n"] = fn + newreg["w"] = min(fe, newreg["w"]) + newreg["e"] = max(fe, newreg["e"]) + newreg["s"] = min(fn, newreg["s"]) + newreg["n"] = max(fn, newreg["n"]) return newreg @@ -2230,10 +2226,8 @@ def AdjustMap(self, newreg): # LL locations if self.Map.projinfo["proj"] == "ll": - if newreg["n"] > 90.0: - newreg["n"] = 90.0 - if newreg["s"] < -90.0: - newreg["s"] = -90.0 + newreg["n"] = min(newreg["n"], 90.0) + newreg["s"] = max(newreg["s"], -90.0) ce = newreg["w"] + (newreg["e"] - newreg["w"]) / 2 cn = newreg["s"] + (newreg["n"] - newreg["s"]) / 2 diff --git a/gui/wxpython/image2target/ii2t_statusbar.py b/gui/wxpython/image2target/ii2t_statusbar.py index 1424f7f4846..3c5ee378a1e 100644 --- a/gui/wxpython/image2target/ii2t_statusbar.py +++ b/gui/wxpython/image2target/ii2t_statusbar.py @@ -97,8 +97,7 @@ def Update(self): and sets the spin limits accordingly.""" self.statusbar.SetStatusText("") maximum = self.mapFrame.GetListCtrl().GetItemCount() - if maximum < 1: - maximum = 1 + maximum = max(maximum, 1) self.widget.SetRange(0, maximum) self.Show() diff --git a/gui/wxpython/location_wizard/dialogs.py b/gui/wxpython/location_wizard/dialogs.py index 8c48d75cf18..da706b96599 100644 --- a/gui/wxpython/location_wizard/dialogs.py +++ b/gui/wxpython/location_wizard/dialogs.py @@ -703,11 +703,9 @@ def __init__( width = max(width, w) height = height + 5 - if height > 400: - height = 400 + height = min(height, 400) width = width + 5 - if width > 400: - width = 400 + width = min(width, 400) # # VListBox for displaying and selecting transformations diff --git a/gui/wxpython/modules/colorrules.py b/gui/wxpython/modules/colorrules.py index 85d36bcd00b..21dc93476f6 100644 --- a/gui/wxpython/modules/colorrules.py +++ b/gui/wxpython/modules/colorrules.py @@ -728,10 +728,8 @@ def ReadColorTable(self, ctable): self.rulesPanel.mainPanel.FindWindowById(count + 2000).SetColour(rgb) # range try: - if float(value) < minim: - minim = float(value) - if float(value) > maxim: - maxim = float(value) + minim = min(float(value), minim) + maxim = max(float(value), maxim) except ValueError: # nv, default pass count += 1 @@ -1619,10 +1617,8 @@ def LoadRulesFromColumn(self): else: col1, col2 = record.split(sep) - if float(col1) < minim: - minim = float(col1) - if float(col1) > maxim: - maxim = float(col1) + minim = min(float(col1), minim) + maxim = max(float(col1), maxim) # color rules list should only have unique values of col1, not all # records diff --git a/gui/wxpython/nviz/tools.py b/gui/wxpython/nviz/tools.py index 407e6bb0470..486fb104348 100644 --- a/gui/wxpython/nviz/tools.py +++ b/gui/wxpython/nviz/tools.py @@ -2790,8 +2790,7 @@ def UpdateFrameIndex( frameCount = anim.GetFrameCount() if index >= frameCount: index = frameCount - 1 - if index < 0: - index = 0 + index = max(index, 0) if sliderWidget: slider = self.FindWindowById(self.win["anim"]["frameIndex"]["slider"]) diff --git a/gui/wxpython/photo2image/ip2i_manager.py b/gui/wxpython/photo2image/ip2i_manager.py index fc89994607a..34346f39cae 100644 --- a/gui/wxpython/photo2image/ip2i_manager.py +++ b/gui/wxpython/photo2image/ip2i_manager.py @@ -1455,14 +1455,10 @@ def GetNewExtent(self, region, map=None): e, n = errlist[i].split() fe = float(e) fn = float(n) - if fe < newreg["w"]: - newreg["w"] = fe - if fe > newreg["e"]: - newreg["e"] = fe - if fn < newreg["s"]: - newreg["s"] = fn - if fn > newreg["n"]: - newreg["n"] = fn + newreg["w"] = min(fe, newreg["w"]) + newreg["e"] = max(fe, newreg["e"]) + newreg["s"] = min(fn, newreg["s"]) + newreg["n"] = max(fn, newreg["n"]) return newreg @@ -1511,10 +1507,8 @@ def AdjustMap(self, newreg): # LL locations if self.Map.projinfo["proj"] == "ll": - if newreg["n"] > 90.0: - newreg["n"] = 90.0 - if newreg["s"] < -90.0: - newreg["s"] = -90.0 + newreg["n"] = min(newreg["n"], 90.0) + newreg["s"] = max(newreg["s"], -90.0) ce = newreg["w"] + (newreg["e"] - newreg["w"]) / 2 cn = newreg["s"] + (newreg["n"] - newreg["s"]) / 2 diff --git a/gui/wxpython/photo2image/ip2i_statusbar.py b/gui/wxpython/photo2image/ip2i_statusbar.py index 3415906aeee..336dcd6903f 100644 --- a/gui/wxpython/photo2image/ip2i_statusbar.py +++ b/gui/wxpython/photo2image/ip2i_statusbar.py @@ -97,8 +97,7 @@ def Update(self): and sets the spin limits accordingly.""" self.statusbar.SetStatusText("") maximum = self.mapFrame.GetListCtrl().GetItemCount() - if maximum < 1: - maximum = 1 + maximum = max(maximum, 1) self.widget.SetRange(0, maximum) self.Show() diff --git a/gui/wxpython/vnet/dialogs.py b/gui/wxpython/vnet/dialogs.py index 03b48b698bb..e8bbd4520a7 100644 --- a/gui/wxpython/vnet/dialogs.py +++ b/gui/wxpython/vnet/dialogs.py @@ -1743,8 +1743,7 @@ def AddStatusItem(self, text, key, priority): if item["key"] == statusTextItem["key"]: self.statusItems.remove(item) self.statusItems.append(statusTextItem) - if self.maxPriority < statusTextItem["priority"]: - self.maxPriority = statusTextItem["priority"] + self.maxPriority = max(self.maxPriority, statusTextItem["priority"]) self._updateStatus() def _updateStatus(self): @@ -1770,8 +1769,7 @@ def RemoveStatusItem(self, key): if update: for item in self.statusItems: self.maxPriority = 0 - if self.maxPriority < item["priority"]: - self.maxPriority = item["priority"] + self.maxPriority = max(self.maxPriority, item["priority"]) self._updateStatus() diff --git a/python/grass/benchmark/runners.py b/python/grass/benchmark/runners.py index d436349063c..e733e9b64de 100644 --- a/python/grass/benchmark/runners.py +++ b/python/grass/benchmark/runners.py @@ -55,8 +55,7 @@ def benchmark_single(module, label, repeat=5): measured_times.append(module.time) avg = time_sum / repeat - if avg < min_avg: - min_avg = avg + min_avg = min(avg, min_avg) print(f"\nResult - {avg}s") print("\u2500" * term_size.columns) diff --git a/python/grass/imaging/images2swf.py b/python/grass/imaging/images2swf.py index da03c40f354..72354e751a6 100644 --- a/python/grass/imaging/images2swf.py +++ b/python/grass/imaging/images2swf.py @@ -341,8 +341,7 @@ def twitsToBits(arr): maxlen = 1 for i in arr: tmp = len(signedIntToBits(i * 20)) - if tmp > maxlen: - maxlen = tmp + maxlen = max(tmp, maxlen) # build array bits = intToBits(maxlen, 5) diff --git a/python/grass/jupyter/region.py b/python/grass/jupyter/region.py index 30e71385343..67eb64104a9 100644 --- a/python/grass/jupyter/region.py +++ b/python/grass/jupyter/region.py @@ -114,14 +114,10 @@ def _set_bbox(self, env): west = float(bbox["ll_w"]) north = float(bbox["ll_n"]) east = float(bbox["ll_e"]) - if self._bbox[0][0] > south: - self._bbox[0][0] = south - if self._bbox[0][1] > west: - self._bbox[0][1] = west - if self._bbox[1][0] < north: - self._bbox[1][0] = north - if self._bbox[1][1] < east: - self._bbox[1][1] = east + self._bbox[0][0] = min(self._bbox[0][0], south) + self._bbox[0][1] = min(self._bbox[0][1], west) + self._bbox[1][0] = max(self._bbox[1][0], north) + self._bbox[1][1] = max(self._bbox[1][1], east) class RegionManagerFor2D: diff --git a/python/grass/temporal/temporal_granularity.py b/python/grass/temporal/temporal_granularity.py index 3b1b005d533..207404cf3d2 100644 --- a/python/grass/temporal/temporal_granularity.py +++ b/python/grass/temporal/temporal_granularity.py @@ -948,55 +948,43 @@ def compute_common_absolute_time_granularity_simple(gran_list): if gran in ["seconds", "second"]: has_seconds = True - if min_gran > 0: - min_gran = 0 - if max_gran < 0: - max_gran = 0 + min_gran = min(min_gran, 0) + max_gran = max(max_gran, 0) seconds.append(int(num)) if gran in ["minutes", "minute"]: has_minutes = True - if min_gran > 1: - min_gran = 1 - if max_gran < 1: - max_gran = 1 + min_gran = min(min_gran, 1) + max_gran = max(max_gran, 1) minutes.append(int(num)) if gran in ["hours", "hour"]: has_hours = True - if min_gran > 2: - min_gran = 2 - if max_gran < 2: - max_gran = 2 + min_gran = min(min_gran, 2) + max_gran = max(max_gran, 2) hours.append(int(num)) if gran in ["days", "day"]: has_days = True - if min_gran > 3: - min_gran = 3 - if max_gran < 3: - max_gran = 3 + min_gran = min(min_gran, 3) + max_gran = max(max_gran, 3) days.append(int(num)) if gran in ["months", "month"]: has_months = True - if min_gran > 4: - min_gran = 4 - if max_gran < 4: - max_gran = 4 + min_gran = min(min_gran, 4) + max_gran = max(max_gran, 4) months.append(int(num)) if gran in ["years", "year"]: has_years = True - if min_gran > 5: - min_gran = 5 - if max_gran < 5: - max_gran = 5 + min_gran = min(min_gran, 5) + max_gran = max(max_gran, 5) years.append(int(num)) diff --git a/scripts/d.correlate/d.correlate.py b/scripts/d.correlate/d.correlate.py index 596d5ae6bf2..af681cce545 100755 --- a/scripts/d.correlate/d.correlate.py +++ b/scripts/d.correlate/d.correlate.py @@ -83,14 +83,10 @@ def main(): minx = maxx = x miny = maxy = y first = False - if minx > x: - minx = x - if maxx < x: - maxx = x - if miny > y: - miny = y - if maxy < y: - maxy = y + minx = min(minx, x) + maxx = max(maxx, x) + miny = min(miny, y) + maxy = max(maxy, y) ifile.close() kx = 100.0 / (maxx - minx + 1) diff --git a/scripts/d.rast.edit/d.rast.edit.py b/scripts/d.rast.edit/d.rast.edit.py index 4824a17a81c..2a55a1dde50 100755 --- a/scripts/d.rast.edit/d.rast.edit.py +++ b/scripts/d.rast.edit/d.rast.edit.py @@ -483,10 +483,8 @@ def initialize(self): for k, f in wind_keys.values(): self.total[k] = (f)(reg[k]) - if self.cols > self.total["cols"]: - self.cols = self.total["cols"] - if self.rows > self.total["rows"]: - self.rows = self.total["rows"] + self.cols = min(self.cols, self.total["cols"]) + self.rows = min(self.rows, self.total["rows"]) tempbase = grass.tempfile() grass.try_remove(tempbase) @@ -627,14 +625,10 @@ def change_window(self): del wait def force_window(self): - if self.origin_x < 0: - self.origin_x = 0 - if self.origin_x > self.total["cols"] - self.cols: - self.origin_x = self.total["cols"] - self.cols - if self.origin_y < 0: - self.origin_y = 0 - if self.origin_y > self.total["rows"] - self.rows: - self.origin_y = self.total["rows"] - self.rows + self.origin_x = max(self.origin_x, 0) + self.origin_x = min(self.origin_x, self.total["cols"] - self.cols) + self.origin_y = max(self.origin_y, 0) + self.origin_y = min(self.origin_y, self.total["rows"] - self.rows) def update_status(self, row, col): self.status["row"] = row diff --git a/scripts/d.rast.leg/d.rast.leg.py b/scripts/d.rast.leg/d.rast.leg.py index 7f29ec12e29..855bf85e684 100755 --- a/scripts/d.rast.leg/d.rast.leg.py +++ b/scripts/d.rast.leg/d.rast.leg.py @@ -126,10 +126,8 @@ def main(): ncats = len(cats.strip().split("\n")) # Only need to adjust legend size if number of categories is between 1 and 10 - if ncats < 2: - ncats = 2 - if ncats > 10: - ncats = 10 + ncats = max(ncats, 2) + ncats = min(ncats, 10) VSpacing = 100 - (ncats * 10) + 10 diff --git a/scripts/r.tileset/r.tileset.py b/scripts/r.tileset/r.tileset.py index 13e970aeaab..ac5cd5c07a0 100644 --- a/scripts/r.tileset/r.tileset.py +++ b/scripts/r.tileset/r.tileset.py @@ -137,14 +137,10 @@ def pointsToBbox(points): if not min_y: min_y = max_y = point[1] - if min_x > point[0]: - min_x = point[0] - if max_x < point[0]: - max_x = point[0] - if min_y > point[1]: - min_y = point[1] - if max_y < point[1]: - max_y = point[1] + min_x = min(min_x, point[0]) + max_x = max(max_x, point[0]) + min_y = min(min_y, point[1]) + max_y = max(max_y, point[1]) bbox["n"] = max_y bbox["s"] = min_y diff --git a/temporal/t.rast.what/t.rast.what.py b/temporal/t.rast.what/t.rast.what.py index ffb1c884893..799d34582bc 100755 --- a/temporal/t.rast.what/t.rast.what.py +++ b/temporal/t.rast.what/t.rast.what.py @@ -248,8 +248,7 @@ def main(options, flags): else: gscript.error(_("Please specify points or coordinates")) - if len(maps) < nprocs: - nprocs = len(maps) + nprocs = min(len(maps), nprocs) # The module queue for parallel execution process_queue = pymod.ParallelModuleQueue(int(nprocs))