Skip to content

Commit

Permalink
Modernize Python syntax, resolves Python 3.12 \. Syntax warning (OS…
Browse files Browse the repository at this point in the history
…Geo#3316)

* Fix unnecessary future import

See
https://docs.astral.sh/ruff/rules/unnecessary-future-import/ https://github.com/asottile/pyupgrade?tab=readme-ov-file#__future__-import-removal

* python: Fix remaining useless object inheritance

See https://docs.astral.sh/ruff/rules/useless-object-inheritance/

* Fix some checks of type, where the type is already a primitive.

See https://docs.astral.sh/ruff/rules/type-of-primitive/

* Fix remaining check of type of primitive.

Might need a second look to make sure the message protocol handled strings correctly. The check was different than others.

See https://docs.astral.sh/ruff/rules/type-of-primitive/

* Fix unnecessary calls to functions instead of native literals

See https://docs.astral.sh/ruff/rules/native-literals/

* Use generator expressions when list comprehension is immediately unpacked.

See https://docs.astral.sh/ruff/rules/unpacked-list-comprehension/

* Use builtin open instead of python3' alias io.open

See https://docs.astral.sh/ruff/rules/open-alias/

* Use `yield from` when a simple yield in a for loop is used. Supported since Python 3.3.

See https://docs.astral.sh/ruff/rules/yield-in-for-loop/

* Convert additional `yield` usage inside a list to a `yield from`

* Removed extra parentheses where obviously unneeded

Some other cases were flagged and could have been automatically fixed, but I wasn't confident enough to apply them now.

See https://docs.astral.sh/ruff/rules/extraneous-parentheses/

* Removed extra parentheses in `.BestSize((self.xx.GetBestSize()))` calls

Some existing calls already called it without the extra parentheses. From the wxPython docs, I understand that GetBestSize returns a wx.Size object, and the BestSize() can accept wx.size, a tuple of (x,y), or two arguments, like x and y separately (at least for https://docs.wxpython.org/wx.lib.agw.aui.framemanager.AuiPaneInfo.html?highlight=bestsize#wx.lib.agw.aui.framemanager.AuiPaneInfo.BestSize)

* Removed a Python 2.6 version block for json.loads kwargs

See https://docs.astral.sh/ruff/rules/outdated-version-block/

* Use raw strings when using `\.`

The sequence `\.` is an undefined escape sequence, but can be found in regex-strings. In regex strings, we usually want the backslash character and the dot character, instead of escaping dot like if it was a tab character `\t`. This is what raw strings (strings prefixed with r) do: they treat backslashes as literal characters.

In Python 3.12, using `\.` in a string (not raw strings) raises `SyntaxWarning: invalid escape sequence '\.'`

* Apply suggestion from code review

* Revert "Convert additional `yield` usage inside a list to a `yield from`"

This reverts commit 3b2bb8c.
  • Loading branch information
echoix authored Jan 3, 2024
1 parent 6b4c665 commit 47aa6e1
Show file tree
Hide file tree
Showing 39 changed files with 67 additions and 82 deletions.
2 changes: 1 addition & 1 deletion display/d.text/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 (i >= 9 and i <= 18 or i > 27) else i) % 9)
rotate(i * 10)
color(colors[i % len(colors)])
xy(
Expand Down
6 changes: 3 additions & 3 deletions doc/gui/wxpython/example/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def AddToolbar(self, name):
.CloseButton(False)
.Layer(1)
.Row(1)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

if name == "MiscToolbar":
Expand All @@ -243,7 +243,7 @@ def AddToolbar(self, name):
.CloseButton(False)
.Layer(1)
.Row(1)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

if name == "MainToolbar":
Expand All @@ -264,7 +264,7 @@ def AddToolbar(self, name):
.CloseButton(False)
.Layer(1)
.Row(1)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

def GetMapToolbar(self):
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/animation/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _addToolbars(self):
.Layer(2)
.Row(1)
.Position(0)
.BestSize((self.toolbars["mainToolbar"].GetBestSize())),
.BestSize(self.toolbars["mainToolbar"].GetBestSize()),
)

self.toolbars["animationToolbar"] = AnimationToolbar(self)
Expand All @@ -200,7 +200,7 @@ def _addToolbars(self):
.Layer(2)
.Row(1)
.Position(1)
.BestSize((self.toolbars["animationToolbar"].GetBestSize())),
.BestSize(self.toolbars["animationToolbar"].GetBestSize()),
)
self.controller.SetAnimationToolbar(self.toolbars["animationToolbar"])

Expand All @@ -220,7 +220,7 @@ def _addToolbars(self):
.Layer(2)
.Row(1)
.Position(2)
.BestSize((self.toolbars["miscToolbar"].GetBestSize())),
.BestSize(self.toolbars["miscToolbar"].GetBestSize()),
)

def SetAnimations(self, layerLists):
Expand Down
8 changes: 4 additions & 4 deletions gui/wxpython/core/globalvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def UpdateGRASSAddOnCommands(eList=None):
os.environ["PATH"] = os.path.join(GUIDIR, "scripts") + os.pathsep + os.environ["PATH"]

ignoredCmdPattern = (
"^d\..*|^r[3]?\.mapcalc$|^i.group$|^r.import$|"
"^r.external$|^r.external.out$|"
"^v.import$|^v.external$|^v.external.out$|"
"^cd$|^cd .*"
r"^d\..*|^r[3]?\.mapcalc$|^i.group$|^r.import$|"
r"^r.external$|^r.external.out$|"
r"^v.import$|^v.external$|^v.external.out$|"
r"^cd$|^cd .*"
)
3 changes: 1 addition & 2 deletions gui/wxpython/core/layerlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def MoveLayerDown(self, layer):
self._list.insert(idx + 1, lr)

def __iter__(self):
for layer in self._list:
yield layer
yield from self._list

def __getitem__(self, index):
return self._list[index]
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ def RenderingFailed(self, cmd, error):
self.renderingFailed.emit(cmd=cmd, error=error)


class Map(object):
class Map:
def __init__(self, gisrc=None):
"""Map composition (stack of map layers and overlays)
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gcp/mapdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def AddToolbar(self, name):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars["map"].GetSize())),
.BestSize(self.toolbars["map"].GetSize()),
)

# GCP display
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _createMenuItem(
try:
cmd = utils.split(str(command))
except UnicodeError:
cmd = utils.split(EncodeString((command)))
cmd = utils.split(EncodeString(command))
# disable only grass commands which are not present (e.g.
# r.in.lidar)
if (
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ def OnLoadEpsgCodes(self, event):
)
return

if isinstance(self.epsgCodeDict, type("")):
if isinstance(self.epsgCodeDict, str):
wx.MessageBox(
parent=self,
message=_("Unable to read EPSG codes: %s") % self.epsgCodeDict,
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gui_core/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _runCmd(self, cmdString):
try:
cmd = utils.split(str(cmdString))
except UnicodeError:
cmd = utils.split(EncodeString((cmdString)))
cmd = utils.split(EncodeString(cmdString))
cmd = list(map(DecodeString, cmd))

self.promptRunCmd.emit(cmd={"cmd": cmd, "cmdString": str(cmdString)})
Expand Down
4 changes: 2 additions & 2 deletions gui/wxpython/gui_core/simplelmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _layout(self):
.Name("checklist")
.CenterPane()
.CloseButton(False)
.BestSize((self._checkList.GetBestSize())),
.BestSize(self._checkList.GetBestSize()),
)
paneInfo = (
wx.aui.AuiPaneInfo()
Expand All @@ -114,7 +114,7 @@ def _layout(self):
.CloseButton(False)
.Layer(1)
.Gripper(False)
.BestSize((self._toolbar.GetBestSize()))
.BestSize(self._toolbar.GetBestSize())
)
if self._style & SIMPLE_LMGR_TB_LEFT:
paneInfo.Left()
Expand Down
12 changes: 6 additions & 6 deletions gui/wxpython/iclass/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(1)
.Position(0)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

if name == "iClass":
Expand All @@ -335,7 +335,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(2)
.Position(0)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

if name == "iClassMisc":
Expand All @@ -357,7 +357,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(1)
.Position(1)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

if name == "vdigit":
Expand Down Expand Up @@ -397,7 +397,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(2)
.Position(1)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

self._mgr.Update()
Expand Down Expand Up @@ -445,7 +445,7 @@ def _addPaneToolbar(self, name, position):
.Center()
.Layer(0)
.Position(position)
.BestSize((self.toolbars[name].GetBestSize())),
.BestSize(self.toolbars[name].GetBestSize()),
)

def _addPaneMapWindow(self, name, position):
Expand Down Expand Up @@ -496,7 +496,7 @@ def UpdateActive(self, win):
mapTb.Enable("zoomBack", enable=(len(self.MapWindow.zoomhistory) > 1))

if mapTb.GetActiveMap() != (win == self.secondMapWindow):
mapTb.SetActiveMap((win == self.secondMapWindow))
mapTb.SetActiveMap(win == self.secondMapWindow)
self.StatusbarUpdate()

def ActivateFirstMap(self, event=None):
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/image2target/ii2t_mapdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def AddToolbar(self, name):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars["map"].GetSize())),
.BestSize(self.toolbars["map"].GetSize()),
)

# GCP display
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/iscatt/iscatt_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ def idScattToidBands(scatt_id, n_bands):
n_b1 = n_bands - 1

band_1 = (int)(
(2 * n_b1 + 1 - sqrt(((2 * n_b1 + 1) * (2 * n_b1 + 1) - 8 * scatt_id))) / 2
(2 * n_b1 + 1 - sqrt((2 * n_b1 + 1) * (2 * n_b1 + 1) - 8 * scatt_id)) / 2
)

band_2 = int(
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/lmgr/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def show_menu_errors(messages):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars[toolbar].GetBestSize())),
.BestSize(self.toolbars[toolbar].GetBestSize()),
)

self._auimgr.GetPane("toolbarNviz").Hide()
Expand Down Expand Up @@ -947,9 +947,9 @@ def _switchPage(self, notification):

def RunSpecialCmd(self, command):
"""Run command from command line, check for GUI wrappers"""
if re.compile("^d\..*").search(command[0]):
if re.compile(r"^d\..*").search(command[0]):
self.RunDisplayCmd(command)
elif re.compile("r[3]?\.mapcalc").search(command[0]):
elif re.compile(r"r[3]?\.mapcalc").search(command[0]):
self.OnMapCalculator(event=None, cmd=command)
elif command[0] == "i.group":
self.OnEditImageryGroups(event=None, cmd=command)
Expand Down
8 changes: 3 additions & 5 deletions gui/wxpython/location_wizard/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def Sorter(self, key1, key2):
item1 = self.itemDataMap[key1][self._col]
item2 = self.itemDataMap[key2][self._col]

if isinstance(item1, type("")) or isinstance(item2, type("")):
if isinstance(item1, str) or isinstance(item2, str):
cmpVal = locale.strcoll(str(item1), str(item2))
else:
cmpVal = cmp(item1, item2)
Expand Down Expand Up @@ -1700,7 +1700,7 @@ def EnableNext(self, enable=True):
def OnTextChange(self, event):
value = self.searchb.GetValue()
if value == "":
self.tlink.SetURL(str("https://epsg.io/"))
self.tlink.SetURL("https://epsg.io/")
self.epsgcode = None
self.epsgdesc = self.epsgparams = ""
self.searchb.ChangeValue("")
Expand Down Expand Up @@ -2392,9 +2392,7 @@ def OnEnterPage(self, event):
self.parent.custompage.customstring
+ self.parent.custompage.custom_dtrans_string
)
self.lproj4string.SetLabel(
("%s" % combo_str.replace(" +", os.linesep + "+"))
)
self.lproj4string.SetLabel("%s" % combo_str.replace(" +", os.linesep + "+"))

self.lprojection.SetLabel(label)

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/main_window/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def BuildPanes(self):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars[toolbar].GetBestSize())),
.BestSize(self.toolbars[toolbar].GetBestSize()),
)

self._auimgr.AddPane(
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/mapdisp/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def openATM(selection):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars["vdigit"].GetBestSize())),
.BestSize(self.toolbars["vdigit"].GetBestSize()),
)
# change mouse to draw digitized line
self.MapWindow.mouse["box"] = "point"
Expand Down Expand Up @@ -628,7 +628,7 @@ def AddToolbar(self, name, fixed=False):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars["map"].GetBestSize())),
.BestSize(self.toolbars["map"].GetBestSize()),
)

# nviz
Expand Down Expand Up @@ -1683,7 +1683,7 @@ def AddRDigit(self):
.CloseButton(False)
.Layer(2)
.DestroyOnClose()
.BestSize((self.toolbars["rdigit"].GetBestSize())),
.BestSize(self.toolbars["rdigit"].GetBestSize()),
)
self._mgr.Update()

Expand Down
12 changes: 6 additions & 6 deletions gui/wxpython/mapswipe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(1)
.Position(0)
.BestSize((self.toolbars["swipeMain"].GetBestSize())),
.BestSize(self.toolbars["swipeMain"].GetBestSize()),
)

if name == "swipeMap":
Expand All @@ -295,7 +295,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(1)
.Position(1)
.BestSize((self.toolbars["swipeMap"].GetBestSize())),
.BestSize(self.toolbars["swipeMap"].GetBestSize()),
)

if name == "swipeMisc":
Expand All @@ -317,7 +317,7 @@ def AddToolbar(self, name):
.Layer(2)
.Row(1)
.Position(2)
.BestSize((self.toolbars["swipeMisc"].GetBestSize())),
.BestSize(self.toolbars["swipeMisc"].GetBestSize()),
)

def _addPanes(self):
Expand All @@ -334,7 +334,7 @@ def _addPanes(self):
.CloseButton(False)
.Center()
.Layer(1)
.BestSize((self.splitter.GetBestSize())),
.BestSize(self.splitter.GetBestSize()),
)

# sliders
Expand All @@ -353,7 +353,7 @@ def _addPanes(self):
.RightDockable(False)
.Bottom()
.Layer(1)
.BestSize((self.sliderH.GetBestSize())),
.BestSize(self.sliderH.GetBestSize()),
)

self._mgr.AddPane(
Expand All @@ -371,7 +371,7 @@ def _addPanes(self):
.RightDockable(True)
.Right()
.Layer(1)
.BestSize((self.sliderV.GetBestSize())),
.BestSize(self.sliderV.GetBestSize()),
)

# statusbar
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/mapwin/decorations.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def GetPlacement(self, screensize):
for param in self._cmd:
if not param.startswith("at"):
continue
x, y = [float(number) for number in param.split("=")[1].split(",")]
x, y = (float(number) for number in param.split("=")[1].split(","))
x = int((x / 100.0) * screensize[0])
y = int((1 - y / 100.0) * screensize[1])

Expand Down Expand Up @@ -304,9 +304,9 @@ def GetPlacement(self, screensize):
if param == self._defaultAt:
b, t, l, r = 5, 50, 7, 10
else:
b, t, l, r = [
b, t, l, r = (
float(number) for number in param.split("=")[1].split(",")
] # pylint: disable-msg=W0612
) # pylint: disable-msg=W0612
x = int((l / 100.0) * screensize[0])
y = int((1 - t / 100.0) * screensize[1])

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/photo2image/ip2i_mapdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def AddToolbar(self, name):
.TopDockable(True)
.CloseButton(False)
.Layer(2)
.BestSize((self.toolbars["map"].GetSize())),
.BestSize(self.toolbars["map"].GetSize()),
)

# GCP display
Expand Down
Loading

0 comments on commit 47aa6e1

Please sign in to comment.