mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
py3: Replace sort(cmp=) with sort(key=)
This commit is contained in:
parent
fdc64f40ad
commit
9a676b0e06
@ -3,6 +3,7 @@ __copyright__ = '2010-2012, , Timothy Legge <timlegge at gmail.com> 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")
|
||||
|
@ -5,6 +5,7 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
__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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user