]> git.phdru.name Git - bookmarks_db.git/commitdiff
Fix(Py3): Fix `cmp` compatibility
authorOleg Broytman <phd@phdru.name>
Sat, 16 Sep 2023 19:40:15 +0000 (22:40 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 12 Nov 2023 19:21:09 +0000 (22:21 +0300)
sort_db.py

index 4418973011c9b1444c2670fdf4e9eebf1efb8566..fce1ac4ba0e6cca48138afc3cc5efe1af6ca6105 100755 (executable)
@@ -19,23 +19,16 @@ __license__ = "GNU GPL"
 import sys
 
 
-class SortBy(object):
+class SortKey(object):
     def __init__(self, sort_by):
         self.sort_by = sort_by
 
-    def __call__(self, o1, o2):
+    def __call__(self, o1):
         try:
-            attr1 = int(getattr(o1, self.sort_by))
+            return int(getattr(o1, self.sort_by))
         except (AttributeError, TypeError, ValueError):
             return 1
 
-        try:
-            attr2 = int(getattr(o2, self.sort_by))
-        except (AttributeError, TypeError, ValueError):
-            return -1
-
-        return cmp(attr1, attr2)
-
 
 def walk_linear(linear, walker):
     for object in linear:
@@ -91,8 +84,8 @@ def run():
     linear = root_folder.linear
     del linear[0]  # exclude root folder from sorting
 
-    by = SortBy(sort_by)
-    linear.sort(by)
+    sort_key = SortKey(sort_by)
+    linear.sort(key=sort_key)
 
     from writers import writer
     output_filename = "%s-sorted_by-%s" % (writer.filename, sort_by)