convert sort and cmp functions to Python3 python3
authorGP Orcullo <kinsamanka@gmail.com>
Fri, 28 Oct 2022 17:01:10 +0800
branchpython3
changeset 3759 f713566d5d01
parent 3758 bc71b19b45ff
child 3760 7270e7e092f8
convert sort and cmp functions to Python3
IDEFrame.py
PLCGenerator.py
controls/FolderTree.py
controls/IDBrowser.py
controls/LogViewer.py
editors/Viewer.py
graphics/GraphicCommons.py
graphics/LD_Objects.py
graphics/SFC_Objects.py
--- a/IDEFrame.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/IDEFrame.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import sys
 import base64
 
@@ -155,7 +157,8 @@
     for tab in tabs:
         if tab["pos"][0] == rect.x:
             others = [t for t in tabs if t != tab]
-            others.sort(lambda x, y: cmp(x["pos"][0], y["pos"][0]))
+            others.sort(key=cmp_to_key(lambda x, y: eq(x["pos"][0],
+                                                       y["pos"][0])))
             for other in others:
                 if other["pos"][1] == tab["pos"][1] and \
                    other["size"][1] == tab["size"][1] and \
@@ -170,7 +173,8 @@
 
         elif tab["pos"][1] == rect.y:
             others = [t for t in tabs if t != tab]
-            others.sort(lambda x, y: cmp(x["pos"][1], y["pos"][1]))
+            others.sort(key=cmp_to_key(lambda x, y: eq(x["pos"][1],
+                                                       y["pos"][1])))
             for other in others:
                 if other["pos"][0] == tab["pos"][0] and \
                    other["size"][0] == tab["size"][0] and \
@@ -807,7 +811,7 @@
                         if tab_infos is not None:
                             tab["pages"].append((tab_infos, page_idx == child.GetActivePage()))
                     tabs.append(tab)
-        tabs.sort(lambda x, y: cmp(x["pos"], y["pos"]))
+        tabs.sort(key=cmp_to_key(lambda x, y: eq(x["pos"], y["pos"])))
         size = notebook.GetSize()
         return ComputeTabsLayout(tabs, wx.Rect(1, 1, size[0] - NOTEBOOK_BORDER, size[1] - NOTEBOOK_BORDER))
 
--- a/PLCGenerator.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/PLCGenerator.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import re
 from functools import reduce
 
@@ -77,9 +79,9 @@
     ax, ay = int(a.getx()), int(a.gety())
     bx, by = int(b.getx()), int(b.gety())
     if abs(ay - by) < 10:
-        return cmp(ax, bx)
+        return eq(ax, bx)
     else:
-        return cmp(ay, by)
+        return eq(ay, by)
 
 
 def JoinList(separator, mylist):
@@ -993,9 +995,9 @@
                     otherInstances["connectors"].append(instance)
                 elif isinstance(instance, CoilClass):
                     otherInstances["outVariables&coils"].append(instance)
-            orderedInstances.sort()
-            otherInstances["outVariables&coils"].sort(SortInstances)
-            otherInstances["blocks"].sort(SortInstances)
+            orderedInstances.sort(key=lambda n: n[0])
+            otherInstances["outVariables&coils"].sort(key=cmp_to_key(SortInstances))
+            otherInstances["blocks"].sort(key=cmp_to_key(SortInstances))
             instances = [instance for (executionOrderId, instance) in orderedInstances]
             instances.extend(otherInstances["outVariables&coils"] + otherInstances["blocks"] + otherInstances["connectors"])
             for instance in instances:
--- a/controls/FolderTree.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/controls/FolderTree.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import os
 
 import wx
@@ -34,7 +36,7 @@
 
 def sort_folder(x, y):
     if x[1] == y[1]:
-        return cmp(x[0], y[0])
+        return eq(x[0], y[0])
     elif x[1] != FILE:
         return -1
     else:
@@ -135,7 +137,7 @@
                           os.path.splitext(filename)[1] == self.CurrentFilter):
                         items.append((filename, FILE, None))
         if recursive:
-            items.sort(sort_folder)
+            items.sort(key=cmp_to_key(sort_folder))
         return items
 
     def SetFilter(self, filter):
--- a/controls/IDBrowser.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/controls/IDBrowser.py	Fri Oct 28 17:01:10 2022 +0800
@@ -4,6 +4,7 @@
 # See COPYING file for copyrights details.
 
 
+from operator import eq
 import wx
 import wx.dataview as dv
 import PSKManagement as PSK
@@ -43,9 +44,9 @@
         row1 = self.GetRow(item1)
         row2 = self.GetRow(item2)
         if col == 0:
-            return cmp(int(self.data[row1][col]), int(self.data[row2][col]))
+            return eq(int(self.data[row1][col]), int(self.data[row2][col]))
         else:
-            return cmp(self.data[row1][col], self.data[row2][col])
+            return eq(self.data[row1][col], self.data[row2][col])
 
     def DeleteRows(self, rows):
         rows = list(rows)
--- a/controls/LogViewer.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/controls/LogViewer.py	Fri Oct 28 17:01:10 2022 +0800
@@ -24,6 +24,7 @@
 
 
 from datetime import datetime
+from operator import eq
 from time import time as gettime
 from weakref import proxy
 
@@ -241,8 +242,8 @@
 
     def __cmp__(self, other):
         if self.Date == other.Date:
-            return cmp(self.Seconds, other.Seconds)
-        return cmp(self.Date, other.Date)
+            return eq(self.Seconds, other.Seconds)
+        return eq(self.Date, other.Date)
 
     def GetFullText(self):
         date = self.Date.replace(second=int(self.Seconds))
--- a/editors/Viewer.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/editors/Viewer.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import math
 from time import time as gettime
 from threading import Lock
@@ -223,9 +225,9 @@
     x1, y1 = block_infos1[0].GetPosition()
     x2, y2 = block_infos2[0].GetPosition()
     if y1 == y2:
-        return cmp(x1, x2)
+        return eq(x1, x2)
     else:
-        return cmp(y1, y2)
+        return eq(y1, y2)
 
 # -------------------------------------------------------------------------------
 #                       Graphic elements Viewer base class
@@ -920,11 +922,13 @@
         wires = list(self.Wires.keys())
         comments = list(self.Comments.values())
         if sort_blocks:
-            blocks.sort(lambda x, y: cmp(x.GetId(), y.GetId()))
+            blocks.sort(key=cmp_to_key(lambda x, y: eq(x.GetId(), y.GetId())))
         if sort_wires:
-            wires.sort(lambda x, y: cmp(self.Wires[x], self.Wires[y]))
+            wires.sort(key=cmp_to_key(lambda x, y: eq(self.Wires[x],
+                                                      self.Wires[y])))
         if sort_comments:
-            comments.sort(lambda x, y: cmp(x.GetId(), y.GetId()))
+            comments.sort(key=cmp_to_key(lambda x, y: eq(x.GetId(),
+                                                         y.GetId())))
         return blocks + wires + comments
 
     def GetContinuationByName(self, name):
@@ -3509,7 +3513,7 @@
                         block = self.Blocks.get(infos[2])
                         if block is not None:
                             blocks.append((block, (infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT)))
-            blocks.sort(sort_blocks)
+            blocks.sort(key=cmp_to_key(sort_blocks))
             self.SearchResults.extend([infos for block, infos in blocks])
             self.CurrentFindHighlight = None
 
--- a/graphics/GraphicCommons.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/graphics/GraphicCommons.py	Fri Oct 28 17:01:10 2022 +0800
@@ -294,7 +294,7 @@
             distances.append((sqrt((self.Pos.x + connector_pos.x - position.x) ** 2 +
                                    (self.Pos.y + connector_pos.y - position.y) ** 2),
                               connector))
-        distances.sort()
+        distances.sort(key=lambda n: n[0])
         if len(distances) > 0:
             return distances[0][1]
         return None
--- a/graphics/LD_Objects.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/graphics/LD_Objects.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import wx
 
 from graphics.GraphicCommons import *
@@ -157,7 +159,7 @@
             for connect in self.Connectors:
                 connect_pos = connect.GetRelPosition()
                 connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny))
-        self.Connectors.sort(lambda x, y: cmp(x.Pos.y, y.Pos.y))
+        self.Connectors.sort(key=cmp_to_key(lambda x, y: eq(x.Pos.y, y.Pos.y)))
         maxy = 0
         for connect in self.Connectors:
             connect_pos = connect.GetRelPosition()
--- a/graphics/SFC_Objects.py	Fri Oct 28 15:19:24 2022 +0800
+++ b/graphics/SFC_Objects.py	Fri Oct 28 17:01:10 2022 +0800
@@ -23,6 +23,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 
+from functools import cmp_to_key
+from operator import eq
 import wx
 
 from graphics.GraphicCommons import *
@@ -1239,8 +1241,8 @@
             for output in self.Outputs:
                 output_pos = output.GetRelPosition()
                 output.SetPosition(wx.Point(output_pos.x - minx, output_pos.y))
-        self.Inputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x))
-        self.Outputs.sort(lambda x, y: cmp(x.Pos.x, y.Pos.x))
+        self.Inputs.sort(key=cmp_to_key(lambda x, y: eq(x.Pos.y, y.Pos.y)))
+        self.Outputs.sort(key=cmp_to_key(lambda x, y: eq(x.Pos.y, y.Pos.y)))
         self.Pos.x += minx
         self.Size[0] = maxx - minx
         connector.MoveConnected()