Skip to content

Commit

Permalink
style: Fix R1716: Simplify chained comparison between the operands (c…
Browse files Browse the repository at this point in the history
…hained-comparison) (OSGeo#4902)

* style: Fix R1716: Simplify chained comparison between the operands (chained-comparison)

Pylint rule: https://pylint.readthedocs.io/en/stable/user_guide/messages/refactor/chained-comparison.html

* gui: Fix remaining Pylint R1716 issues
  • Loading branch information
echoix authored Jan 2, 2025
1 parent 4538f7d commit 7e49a2b
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion display/d.text/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def text(in_text):

for i in range(36):
font(fonts[int(i % len(fonts))])
size((36 - i if ((i >= 9 and i <= 18) or i > 27) else i) % 9)
size((36 - i if ((9 <= i <= 18) or i > 27) else i) % 9)
rotate(i * 10)
color(colors[i % len(colors)])
xy(
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def msg(self, level, message, *args):
:param args: formatting params
"""
# self.SetLevel()
if self.debuglevel > 0 and level > 0 and level <= self.debuglevel:
if 0 < level <= self.debuglevel:
if args:
sys.stderr.write(
"GUI D%d/%d: " % (level, self.debuglevel)
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def write(self, s):

if "GRASS_INFO_PERCENT" in line:
value = int(line.rsplit(":", 1)[1].strip())
progressValue = value if value >= 0 and value < 100 else 0
progressValue = value if 0 <= value < 100 else 0
elif "GRASS_INFO_MESSAGE" in line:
self.type = "message"
self.message += line.split(":", 1)[1].strip() + "\n"
Expand Down
6 changes: 1 addition & 5 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,7 @@ def GetValidLayerName(name):
cIdx = 0
retNameList = list(retName)
for c in retNameList:
if (
not (c >= "A" and c <= "Z")
and not (c >= "a" and c <= "z")
and not (c >= "0" and c <= "9")
):
if not ("A" <= c <= "Z") and not ("a" <= c <= "z") and not ("0" <= c <= "9"):
retNameList[cIdx] = "_"
cIdx += 1
retName = "".join(retNameList)
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/mapwin/buffered.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,14 @@ def TextBounds(self, textinfo, relcoords=False):

boxh = math.fabs(math.sin(math.radians(rotation)) * w) + h
boxw = math.fabs(math.cos(math.radians(rotation)) * w) + h
if rotation > 0 and rotation < 90:
if 0 < rotation < 90:
bbox[1] -= boxh
relCoords = (0, boxh)
elif rotation >= 90 and rotation < 180:
elif 90 <= rotation < 180:
bbox[0] -= boxw
bbox[1] -= boxh
relCoords = (boxw, boxh)
elif rotation >= 180 and rotation < 270:
elif 180 <= rotation < 270:
bbox[0] -= boxw
relCoords = (boxw, 0)
bbox[2] = boxw
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/mapwin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def SetItemDrawOrder(self, item, drawNum):
:return: True if order was changed
:return: False if drawNum is out of range or item was not found
"""
if drawNum < len(self.itemsList) and drawNum >= 0 and item in self.itemsList:
if 0 <= drawNum < len(self.itemsList) and item in self.itemsList:
self.itemsList.insert(
drawNum, self.itemsList.pop(self.itemsList.index(item))
)
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/vdigit/wxdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ def _validLine(self, line) -> bool:
:return: True valid feature id
:return: False invalid
"""
return bool(line > 0 and line <= Vect_get_num_lines(self.poMapInfo))
return bool(0 < line <= Vect_get_num_lines(self.poMapInfo))

def SelectLinesByBox(self, bbox, ltype=None, drawSeg=False, poMapInfo=None):
"""Select vector objects by given bounding box
Expand Down Expand Up @@ -913,7 +913,7 @@ def GetRegionSelected(self):
for line in self.selected["ids"]:
area = Vect_get_centroid_area(self.poMapInfo, line)

if area > 0 and area <= nareas:
if 0 < area <= nareas:
if not Vect_get_area_box(self.poMapInfo, area, byref(lineBox)):
continue
else: # noqa: PLR5501
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/vnet/vnet_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,4 +1450,4 @@ def IsInInterval(self, from_angle, to_angle, angle) -> bool:
if angle < from_angle:
angle = math.pi * 2 + angle

return bool(angle > from_angle and angle < to_angle)
return bool(from_angle < angle < to_angle)
2 changes: 1 addition & 1 deletion gui/wxpython/vnet/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def ShowColumn(self, colName, pos):
:return: True if column was shown
:return: False if position is not valid or column is not hidden
"""
if pos < 0 and pos >= self.self.GetColumnCount():
if pos < 0 or pos >= self.GetColumnCount():
return False
if colName in self.hiddenCols:
col = self.hiddenCols[colName]
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,6 @@ disable = [
"R1713", # Consider using str.join(sequence) for concatenating strings from an iterable (consider-using-join)
"R1714", # Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable. (consider-using-in)
"R1715", # Consider using dict.get for getting values from a dict if a key is present or a default if not (consider-using-get)
"R1716", # Simplify chained comparison between the operands (chained-comparison)
"R1724", # Unnecessary "%s" after "continue", %s (no-else-continue)
"R1727", # Boolean condition '%s' will always evaluate to '%s' (condition-evals-to-constant)
"R1732", # Consider using 'with' for resource-allocating operations (consider-using-with)
Expand Down
2 changes: 1 addition & 1 deletion python/grass/imaging/images2ims.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def checkImages(images):
images2.append(im) # Ok
elif im.dtype in [np.float32, np.float64]:
theMax = im.max()
if theMax > 128 and theMax < 300:
if 128 < theMax < 300:
pass # assume 0:255
else:
im = im.copy()
Expand Down
2 changes: 1 addition & 1 deletion python/grass/imaging/images2swf.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def checkImages(images):
images2.append(im) # Ok
elif im.dtype in [np.float32, np.float64]:
theMax = im.max()
if theMax > 128 and theMax < 300:
if 128 < theMax < 300:
pass # assume 0:255
else:
im = im.copy()
Expand Down
24 changes: 12 additions & 12 deletions python/grass/temporal/spatial_extent.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,13 +868,13 @@ def cover_2d(self, extent) -> bool:

# We check that at least one edge of extent is located in self
edge_count = 0
if eW > W and eW < E:
if W < eW < E:
edge_count += 1
if eE < E and eE > W:
if W < eE < E:
edge_count += 1
if eN < N and eN > S:
if S < eN < N:
edge_count += 1
if eS > S and eS < N:
if S < eS < N:
edge_count += 1

return edge_count != 0
Expand Down Expand Up @@ -941,21 +941,21 @@ def cover(self, extent) -> bool:

# We check that at least one edge of extent is located in self
edge_count = 0
if eW > W and eW < E:
if W < eW < E:
edge_count += 1
if eE < E and eE > W:
if W < eE < E:
edge_count += 1
if eN < N and eN > S:
if S < eN < N:
edge_count += 1
if eS > S and eS < N:
if S < eS < N:
edge_count += 1
if eN < N and eN > S:
if S < eN < N:
edge_count += 1
if eS > S and eS < N:
if S < eS < N:
edge_count += 1
if eT < T and eT > B:
if B < eT < T:
edge_count += 1
if eB > B and eB < T:
if B < eB < T:
edge_count += 1

return edge_count != 0
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -3371,7 +3371,7 @@ def p_expr_time_const(self, t) -> None:
# Get map index and temporal extent.
map_index = map_list.index(map_i)
new_index = map_index + t_neighbour
if new_index < max_index and new_index >= 0:
if 0 <= new_index < max_index:
# Get neighbouring map and set temporal extent.
map_n = map_list[new_index]
map_i_t_extent = map_i.get_temporal_extent()
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_raster3d_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def p_ts_neighbor_operation(self, t) -> None:
# Get map index and temporal extent.
map_index = maplist.index(map_i)
new_index = map_index + t_neighbor
if new_index < max_index and new_index >= 0:
if 0 <= new_index < max_index:
map_i_t_extent = map_i.get_temporal_extent()
# Get neighboring map and set temporal extent.
map_n = maplist[new_index]
Expand Down
2 changes: 1 addition & 1 deletion python/grass/temporal/temporal_raster_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def p_ts_neighbour_operation(self, t) -> None:
# Get map index and temporal extent.
map_index = maplist.index(map_i)
new_index = map_index + t_neighbour
if new_index < max_index and new_index >= 0:
if 0 <= new_index < max_index:
map_i_t_extent = map_i.get_temporal_extent()
# Get neighbouring map and set temporal extent.
map_n = maplist[new_index]
Expand Down
4 changes: 2 additions & 2 deletions temporal/t.rast.accdetect/t.rast.accdetect.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def main():
prev_map = curr_map
subexpr1 = "null()"
subexpr3 = "%i" % (indicator_start)
elif i > 0 and i < num_maps - 1:
elif 0 < i < num_maps - 1:
prev_map = occurrence_maps[map.next().get_id()].get_name()
next_map = occurrence_maps[map.prev().get_id()].get_name()
# In case the previous map is null() set null() or the start
Expand Down Expand Up @@ -444,7 +444,7 @@ def main():
prev_map = curr_map
subexpr1 = "null()"
subexpr3 = "%i" % (indicator_start)
elif i > 0 and i < num_maps - 1:
elif 0 < i < num_maps - 1:
prev_map = occurrence_maps[map.prev().get_id()].get_name()
next_map = occurrence_maps[map.next().get_id()].get_name()
# In case the previous map is null() set null() or the start
Expand Down

0 comments on commit 7e49a2b

Please sign in to comment.