Skip to content

Commit

Permalink
style: Fix reimplemented-operator (FURB118) (#4408)
Browse files Browse the repository at this point in the history
* style: Fix reimplemented-operator (FURB118)

Ruff rule: https://docs.astral.sh/ruff/rules/reimplemented-operator/

Replaced lambdas with `operator.itemgetter()` or `operator.add()`. These are implemented more efficiently in C and have a Python fallback.

* utils/g.html2man: Replace children function with operator.itemgetter
  • Loading branch information
echoix committed Sep 28, 2024
1 parent ae5e4dd commit 0ea5e37
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 24 deletions.
5 changes: 3 additions & 2 deletions gui/wxpython/animation/temporal_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""

import datetime
from operator import itemgetter

import grass.script as gs
import grass.temporal as tgis
Expand Down Expand Up @@ -195,9 +196,9 @@ def GetLabelsAndMaps(self):
# by a temporary dataset, I don't know how it would work with point
# data
if self.temporalType == TemporalType.ABSOLUTE:
timestamps = sorted(list(labelListSet), key=lambda x: x[0])
timestamps = sorted(list(labelListSet), key=itemgetter(0))
else:
timestamps = sorted(list(labelListSet), key=lambda x: x[0])
timestamps = sorted(list(labelListSet), key=itemgetter(0))

newMapLists = []
for mapList, labelList in zip(mapLists, labelLists):
Expand Down
3 changes: 2 additions & 1 deletion gui/wxpython/gui_core/ghelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import sys
import wx
from wx.html import HtmlWindow
from operator import itemgetter

try:
from wx.lib.agw.hyperlink import HyperLinkCtrl
Expand Down Expand Up @@ -450,7 +451,7 @@ def _pageContributors(self, extra=False):
text = StaticText(parent=contribwin, id=wx.ID_ANY, label=item)
text.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
contribBox.Add(text)
for vals in sorted(contribs, key=lambda x: x[0]):
for vals in sorted(contribs, key=itemgetter(0)):
for item in vals:
contribBox.Add(
StaticText(parent=contribwin, id=wx.ID_ANY, label=item)
Expand Down
10 changes: 6 additions & 4 deletions gui/wxpython/mapwin/buffered.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import wx

from operator import itemgetter

from grass.pydispatch.signal import Signal

from core.globalvar import wxPythonPhoenix
Expand Down Expand Up @@ -462,10 +464,10 @@ def Draw(
brush = wx.TRANSPARENT_BRUSH
pdc.SetBrush(brush)
pdc.DrawPolygon(points=coords)
x = min(coords, key=lambda x: x[0])[0]
y = min(coords, key=lambda x: x[1])[1]
w = max(coords, key=lambda x: x[0])[0] - x
h = max(coords, key=lambda x: x[1])[1] - y
x = min(coords, key=itemgetter(0))[0]
y = min(coords, key=itemgetter(1))[1]
w = max(coords, key=itemgetter(0))[0] - x
h = max(coords, key=itemgetter(1))[1] - y
pdc.SetIdBounds(drawid, Rect(x, y, w, h))

elif pdctype == "circle": # draw circle
Expand Down
7 changes: 3 additions & 4 deletions gui/wxpython/psmap/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import os
import string
from copy import deepcopy
from operator import itemgetter
from pathlib import Path

import wx
Expand Down Expand Up @@ -3623,9 +3624,7 @@ def _vectorLegend(self, notebook):
self.vectorListCtrl.InsertColumn(0, _("Vector map"))
self.vectorListCtrl.InsertColumn(1, _("Label"))
if self.vectorId:
vectors = sorted(
self.instruction[self.vectorId]["list"], key=lambda x: x[3]
)
vectors = sorted(self.instruction[self.vectorId]["list"], key=itemgetter(3))

for vector in vectors:
index = self.vectorListCtrl.InsertItem(
Expand Down Expand Up @@ -4456,7 +4455,7 @@ def updateDialog(self):
if self.instruction.FindInstructionByType("vector"):
vectors = sorted(
self.instruction.FindInstructionByType("vector")["list"],
key=lambda x: x[3],
key=itemgetter(3),
)
self.vectorListCtrl.DeleteAllItems()
for vector in vectors:
Expand Down
5 changes: 2 additions & 3 deletions gui/wxpython/timeline/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import wx
from functools import reduce
from operator import add

try:
import matplotlib as mpl
Expand Down Expand Up @@ -495,9 +496,7 @@ def _checkDatasets(self, datasets):
]
# flatten this list
if allDatasets:
allDatasets = reduce(
lambda x, y: x + y, reduce(lambda x, y: x + y, allDatasets)
)
allDatasets = reduce(add, reduce(add, allDatasets))
mapsets = tgis.get_tgis_c_library_interface().available_mapsets()
allDatasets = [
i
Expand Down
5 changes: 2 additions & 3 deletions gui/wxpython/tplot/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

from gui_core.widgets import GNotebook
from gui_core.wrap import CheckBox, TextCtrl, Button, StaticText
from operator import add

ALPHA = 0.5
COLORS = ["b", "g", "r", "c", "m", "y", "k"]
Expand Down Expand Up @@ -1153,9 +1154,7 @@ def _checkDatasets(self, datasets, typ):
]
# flatten this list
if allDatasets:
allDatasets = reduce(
lambda x, y: x + y, reduce(lambda x, y: x + y, allDatasets)
)
allDatasets = reduce(add, reduce(add, allDatasets))
mapsets = tgis.get_tgis_c_library_interface().available_mapsets()
allDatasets = [
i
Expand Down
4 changes: 3 additions & 1 deletion man/build_full_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sys
import os

from operator import itemgetter

from build_html import (
html_dir,
grass_version,
Expand Down Expand Up @@ -50,7 +52,7 @@
prefix = cmd.split(".")[0]
if prefix not in [item[0] for item in classes]:
classes.append((prefix, class_labels.get(prefix, prefix)))
classes.sort(key=lambda tup: tup[0])
classes.sort(key=itemgetter(0))

# begin full index:
filename = "full_index.html"
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ ignore = [
"FBT001", # boolean-type-hint-positional-argument
"FBT002", # boolean-default-value-positional-argument
"FBT003", # boolean-positional-value-in-call
"FURB118", # reimplemented-operator
"I001", # unsorted-imports
"ISC003", # explicit-string-concatenation
"PERF203", # try-except-in-loop
Expand Down
3 changes: 2 additions & 1 deletion python/grass/imaging/images2ims.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""

import os
from operator import itemgetter

try:
import numpy as np
Expand Down Expand Up @@ -214,7 +215,7 @@ def readIms(filename, asNumpy=True):
images.append((im.copy(), nr))

# Sort images
images.sort(key=lambda x: x[1])
images.sort(key=itemgetter(1))
images = [im[0] for im in images]

# Convert to numpy if needed
Expand Down
4 changes: 3 additions & 1 deletion scripts/g.search.modules/g.search.modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import os
import sys

from operator import itemgetter

from grass.script import core as grass
from grass.exceptions import CalledModuleError

Expand Down Expand Up @@ -283,7 +285,7 @@ def _search_module(
}
)

return sorted(found_modules, key=lambda k: k["name"])
return sorted(found_modules, key=itemgetter("name"))


def _basic_search(pattern, name, description, module_keywords) -> bool:
Expand Down
4 changes: 3 additions & 1 deletion scripts/v.report/v.report.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

import sys
import os
from operator import itemgetter

import grass.script as gs
from grass.script.utils import separator, decode

Expand Down Expand Up @@ -134,7 +136,7 @@ def main():
if p.returncode != 0:
sys.exit(1)

records1.sort(key=lambda r: r[catcol])
records1.sort(key=itemgetter(catcol))

if len(records1) == 0:
try:
Expand Down
4 changes: 2 additions & 2 deletions utils/g.html2man/rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from operator import itemgetter


def match(node, tag, attr=None, val=None):
Expand Down Expand Up @@ -26,8 +27,7 @@ def find(node, tag, attr=None, val=None):
raise ValueError("child not found")


def children(node):
return node[2]
children = itemgetter(2)


def text(node):
Expand Down

0 comments on commit 0ea5e37

Please sign in to comment.