From 9a676b0e06bf8b31d0aa4be27c938a19e409ede2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Mar 2019 08:08:14 +0530 Subject: [PATCH] py3: Replace sort(cmp=) with sort(key=) --- src/calibre/devices/kobo/books.py | 8 ++++---- src/calibre/devices/usbms/books.py | 8 ++++---- src/calibre/devices/usbms/device.py | 26 ++++++------------------- src/calibre/ebooks/oeb/base.py | 6 +----- src/calibre/gui2/dialogs/smartdevice.py | 22 +++++++-------------- 5 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/calibre/devices/kobo/books.py b/src/calibre/devices/kobo/books.py index acad03892f..4cd1399157 100644 --- a/src/calibre/devices/kobo/books.py +++ b/src/calibre/devices/kobo/books.py @@ -3,6 +3,7 @@ __copyright__ = '2010-2012, , Timothy Legge and David Fo __docformat__ = 'restructuredtext en' import os, time, sys +from functools import cmp_to_key from calibre.constants import preferred_encoding, DEBUG from calibre import isbytestring, force_unicode @@ -14,7 +15,7 @@ from calibre.devices.usbms.books import CollectionsBookList from calibre.utils.config_base import prefs from calibre.devices.usbms.driver import debug_print from calibre.ebooks.metadata import author_to_author_sort -from polyglot.builtins import unicode_type, string_or_bytes +from polyglot.builtins import unicode_type, string_or_bytes, iteritems, itervalues class Book(Book_): @@ -305,9 +306,8 @@ class KTCollectionsBookList(CollectionsBookList): # same as above -- no sort_key needed here return cmp(xx[2], yy[2]) - for category, lpaths in collections.items(): - books = lpaths.values() - books.sort(cmp=none_cmp) + for category, lpaths in iteritems(collections): + books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) result[category] = [x[0] for x in books] # debug_print("KTCollectionsBookList:get_collections - result=", result.keys()) debug_print("KTCollectionsBookList:get_collections - end") diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index c2ad189341..fe052e42f5 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -5,6 +5,7 @@ __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' import os, re, time, sys +from functools import cmp_to_key from calibre.ebooks.metadata import title_sort from calibre.ebooks.metadata.book.base import Metadata @@ -14,7 +15,7 @@ from calibre.constants import preferred_encoding from calibre import isbytestring, force_unicode from calibre.utils.config_base import tweaks from calibre.utils.icu import sort_key -from polyglot.builtins import string_or_bytes +from polyglot.builtins import string_or_bytes, iteritems, itervalues class Book(Metadata): @@ -306,9 +307,8 @@ class CollectionsBookList(BookList): except TypeError: return 0 - for category, lpaths in collections.items(): - books = lpaths.values() - books.sort(cmp=none_cmp) + for category, lpaths in iteritems(collections): + books = sorted(itervalues(lpaths), key=cmp_to_key(none_cmp)) result[category] = [x[0] for x in books] return result diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index b025ca7abf..3e2e57c933 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -358,7 +358,7 @@ class Device(DeviceConfig, DevicePlugin): g['p'] = 0 return map(int, (g.get('m'), g.get('p'))) - def dcmp(x, y): + def cmp_key(x): ''' Sorting based on the following scheme: - disks without partitions are first @@ -367,18 +367,11 @@ class Device(DeviceConfig, DevicePlugin): disk number, then on partition number ''' x = x.rpartition('/')[-1] - y = y.rpartition('/')[-1] - x, y = nums(x), nums(y) - if x[1] == 0 and y[1] > 0: - return cmp(1, 2) - if x[1] > 0 and y[1] == 0: - return cmp(2, 1) - ans = cmp(x[0], y[0]) - if ans == 0: - ans = cmp(x[1], y[1]) - return ans + disk_num, part_num = nums(x) + has_part = 1 if part_num > 0 else 0 + return has_part, disk_num, part_num - matches.sort(cmp=dcmp) + matches.sort(key=cmp_key) drives = {'main':matches[0]} if len(matches) > 1: drives['carda'] = matches[1] @@ -711,14 +704,7 @@ class Device(DeviceConfig, DevicePlugin): except dbus.exceptions.DBusException as e: continue - def ocmp(x,y): - if x['node'] < y['node']: - return -1 - if x['node'] > y['node']: - return 1 - return 0 - - vols.sort(cmp=ocmp) + vols.sort(key=lambda x: x['node']) if verbose: print("FBSD: ", vols) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index f5aee08109..39568c1429 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -1268,12 +1268,8 @@ class Manifest(object): return elem def to_opf2(self, parent=None): - - def sort(x, y): - return cmp(x.href, y.href) - elem = element(parent, OPF('manifest')) - for item in sorted(self.items, cmp=sort): + for item in sorted(self.items, key=lambda x: x.href): media_type = item.media_type if media_type in OEB_DOCS: media_type = XHTML_MIME diff --git a/src/calibre/gui2/dialogs/smartdevice.py b/src/calibre/gui2/dialogs/smartdevice.py index 324f9af2fe..7fa88cf3f1 100644 --- a/src/calibre/gui2/dialogs/smartdevice.py +++ b/src/calibre/gui2/dialogs/smartdevice.py @@ -10,22 +10,14 @@ from PyQt5.Qt import (QDialog, QLineEdit, Qt) from calibre.gui2 import error_dialog from calibre.gui2.dialogs.smartdevice_ui import Ui_Dialog from calibre.utils.mdns import get_all_ips -from polyglot.builtins import itervalues, unicode_type +from polyglot.builtins import itervalues, unicode_type, map -def _cmp_ipaddr(l, r): - lparts = ['%3s'%x for x in l.split('.')] - rparts = ['%3s'%x for x in r.split('.')] - - if lparts[0] in ['192', '170', ' 10']: - if rparts[0] not in ['192', '170', '10']: - return -1 - return cmp(rparts, lparts) - - if rparts[0] in ['192', '170', ' 10']: - return 1 - - return cmp(lparts, rparts) +def ipaddr_sort_key(ipaddr): + if '.' in ipaddr: + parts = tuple(map(int, ipaddr.split('.'))) + is_private = parts[0] in (192, 170, 10) + return (0 if is_private else 1), parts def get_all_ip_addresses(): @@ -34,7 +26,7 @@ def get_all_ip_addresses(): for addrs in iface: if 'broadcast' in addrs and addrs['addr'] != '127.0.0.1': ipaddrs.append(addrs['addr']) - ipaddrs.sort(cmp=_cmp_ipaddr) + ipaddrs.sort(key=ipaddr_sort_key) return ipaddrs