diff --git a/manual/custom.py b/manual/custom.py index 2c9b31d514..b35ff29234 100644 --- a/manual/custom.py +++ b/manual/custom.py @@ -179,7 +179,7 @@ def generate_ebook_convert_help(preamble, app): raw += '\n\n.. contents::\n :local:' raw += '\n\n' + options - for pl in sorted(input_format_plugins(), key=lambda x:x.name): + for pl in sorted(input_format_plugins(), key=lambda x: x.name): parser, plumber = create_option_parser(['ebook-convert', 'dummyi.'+sorted(pl.file_types)[0], 'dummyo.epub', '-h'], default_log) groups = [(pl.name+ ' Options', '', g.option_list) for g in @@ -279,7 +279,7 @@ def cli_docs(app): info(bold('creating CLI documentation...')) documented_cmds, undocumented_cmds = get_cli_docs() - documented_cmds.sort(cmp=lambda x, y: cmp(x[0], y[0])) + documented_cmds.sort(key=lambda x: x[0]) undocumented_cmds.sort() documented = [' '*4 + c[0] for c in documented_cmds] diff --git a/src/calibre/customize/profiles.py b/src/calibre/customize/profiles.py index 2c7fead69f..1038d448be 100644 --- a/src/calibre/customize/profiles.py +++ b/src/calibre/customize/profiles.py @@ -233,7 +233,7 @@ input_profiles = [InputProfile, SonyReaderInput, SonyReader300Input, HanlinV5Input, CybookG3Input, CybookOpusInput, KindleInput, IlliadInput, IRexDR1000Input, IRexDR800Input, NookInput] -input_profiles.sort(cmp=lambda x,y:cmp(x.name.lower(), y.name.lower())) +input_profiles.sort(key=lambda x: x.name.lower()) # }}} @@ -870,4 +870,4 @@ output_profiles = [ KindlePaperWhite3Output, KindleOasisOutput ] -output_profiles.sort(cmp=lambda x,y:cmp(x.name.lower(), y.name.lower())) +output_profiles.sort(key=lambda x: x.name.lower()) diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index 1229cbf712..8bb9b5814f 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -727,9 +727,9 @@ def initialize_plugins(perf=False): # ipython sys.stdout, sys.stderr = ostdout, ostderr if perf: - for x in sorted(times, key=lambda x:times[x]): + for x in sorted(times, key=lambda x: times[x]): print('%50s: %.3f'%(x, times[x])) - _initialized_plugins.sort(cmp=lambda x,y:cmp(x.priority, y.priority), reverse=True) + _initialized_plugins.sort(key=lambda x: x.priority, reverse=True) reread_filetype_plugins() reread_metadata_plugins() diff --git a/src/calibre/db/cli/cmd_list_categories.py b/src/calibre/db/cli/cmd_list_categories.py index 0280455f66..6165c50877 100644 --- a/src/calibre/db/cli/cmd_list_categories.py +++ b/src/calibre/db/cli/cmd_list_categories.py @@ -149,9 +149,7 @@ def main(opts, args, dbctx): (not report_on or k in report_on) ] - categories.sort( - cmp=lambda x, y: cmp(x if x[0] != '#' else x[1:], y if y[0] != '#' else y[1:]) - ) + categories.sort(key=lambda x: x if x[0] != '#' else x[1:]) def fmtr(v): v = v or 0 diff --git a/src/calibre/devices/__init__.py b/src/calibre/devices/__init__.py index d3ee2b0f19..5ab3b38dee 100644 --- a/src/calibre/devices/__init__.py +++ b/src/calibre/devices/__init__.py @@ -82,8 +82,7 @@ def debug(ioreg_to_tmp=False, buf=None, plugins=None, out = partial(prints, file=buf) devplugins = device_plugins() if plugins is None else plugins - devplugins = list(sorted(devplugins, cmp=lambda - x,y:cmp(x.__class__.__name__, y.__class__.__name__))) + devplugins = list(sorted(devplugins, key=lambda x: x.__class__.__name__)) if plugins is None: for d in devplugins: try: diff --git a/src/calibre/devices/prs505/sony_cache.py b/src/calibre/devices/prs505/sony_cache.py index c8db25b73a..0e5a90aaee 100644 --- a/src/calibre/devices/prs505/sony_cache.py +++ b/src/calibre/devices/prs505/sony_cache.py @@ -321,7 +321,7 @@ class XMLCache(object): # Only rebase ids of nodes that are immediate children of the # record root (that way playlist/itemnodes are unaffected items = root.xpath('child::*[@id]') - items.sort(cmp=lambda x,y:cmp(int(x.get('id')), int(y.get('id')))) + items.sort(key=lambda x: int(x.get('id'))) idmap = {} for i, item in enumerate(items): old = int(item.get('id')) diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index a7eb0b3567..b025ca7abf 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -537,7 +537,7 @@ class Device(DeviceConfig, DevicePlugin): if sz > 0: nodes.append((x.split('/')[-1], sz)) - nodes.sort(cmp=lambda x, y: cmp(x[1], y[1])) + nodes.sort(key=lambda x: x[1]) if not nodes: return node return nodes[-1][0] diff --git a/src/calibre/devices/winusb.py b/src/calibre/devices/winusb.py index 0a7647cce0..dd927e5df4 100644 --- a/src/calibre/devices/winusb.py +++ b/src/calibre/devices/winusb.py @@ -995,8 +995,7 @@ def develop(): # {{{ drive_letters = set() pprint(usb_devices) print() - devplugins = list(sorted(device_plugins(), cmp=lambda - x,y:cmp(x.__class__.__name__, y.__class__.__name__))) + devplugins = list(sorted(device_plugins(), key=lambda x: x.__class__.__name__)) for dev in devplugins: dev.startup() for dev in devplugins: diff --git a/src/calibre/ebooks/conversion/plumber.py b/src/calibre/ebooks/conversion/plumber.py index 25ad577c8b..5d8e46fd67 100644 --- a/src/calibre/ebooks/conversion/plumber.py +++ b/src/calibre/ebooks/conversion/plumber.py @@ -821,7 +821,7 @@ OptionRecommendation(name='search_replace', if not html_files: raise ValueError(_('Could not find an e-book inside the archive')) html_files = [(f, os.stat(f).st_size) for f in html_files] - html_files.sort(cmp=lambda x, y: cmp(x[1], y[1])) + html_files.sort(key=lambda x: x[1]) html_files = [f[0] for f in html_files] for q in ('toc', 'index'): for f in html_files: diff --git a/src/calibre/ebooks/lit/writer.py b/src/calibre/ebooks/lit/writer.py index fd292f4a03..490c06b42f 100644 --- a/src/calibre/ebooks/lit/writer.py +++ b/src/calibre/ebooks/lit/writer.py @@ -670,8 +670,7 @@ class LitWriter(object): def _build_dchunks(self): ddata = [] directory = list(self._directory) - directory.sort(cmp=lambda x, y: - cmp(x.name.lower(), y.name.lower())) + directory.sort(key=lambda x: x.name.lower()) qrn = 1 + (1 << 2) dchunk = io.BytesIO() dcount = 0 diff --git a/src/calibre/ebooks/metadata/lit.py b/src/calibre/ebooks/metadata/lit.py index 330bf8cc86..d2f078487d 100644 --- a/src/calibre/ebooks/metadata/lit.py +++ b/src/calibre/ebooks/metadata/lit.py @@ -32,7 +32,7 @@ def get_metadata(stream): except: pass break - covers.sort(cmp=lambda x, y:cmp(len(x[0]), len(y[0])), reverse=True) + covers.sort(key=lambda x: len(x[0]), reverse=True) idx = 0 if len(covers) > 1: if covers[1][1] == covers[0][1]+'-standard': diff --git a/src/calibre/ebooks/metadata/meta.py b/src/calibre/ebooks/metadata/meta.py index de773ec8c6..7a8299dc19 100644 --- a/src/calibre/ebooks/metadata/meta.py +++ b/src/calibre/ebooks/metadata/meta.py @@ -41,8 +41,7 @@ def metadata_from_formats(formats, force_read_metadata=False, pattern=None): def _metadata_from_formats(formats, force_read_metadata=False, pattern=None): mi = MetaInformation(None, None) - formats.sort(cmp=lambda x,y: cmp(METADATA_PRIORITIES[path_to_ext(x)], - METADATA_PRIORITIES[path_to_ext(y)])) + formats.sort(key=lambda x: METADATA_PRIORITIES[path_to_ext(x)]) extensions = list(map(path_to_ext, formats)) if 'opf' in extensions: opf = formats[extensions.index('opf')] @@ -241,4 +240,3 @@ def forked_read_metadata(path, tdir): opf = metadata_to_opf(mi, default_lang='und') with lopen(os.path.join(tdir, 'metadata.opf'), 'wb') as f: f.write(opf) - diff --git a/src/calibre/ebooks/oeb/transforms/guide.py b/src/calibre/ebooks/oeb/transforms/guide.py index 29c5f2a93a..104a2fc1d5 100644 --- a/src/calibre/ebooks/oeb/transforms/guide.py +++ b/src/calibre/ebooks/oeb/transforms/guide.py @@ -28,7 +28,7 @@ class Clean(object): else: covers.append([self.oeb.guide[x], len(item.data)]) - covers.sort(cmp=lambda x,y:cmp(x[1], y[1]), reverse=True) + covers.sort(key=lambda x: x[1], reverse=True) if covers: ref = covers[0][0] if len(covers) > 1: @@ -53,4 +53,3 @@ class Clean(object): if item.title and item.title.lower() == 'start': continue self.oeb.guide.remove(x) - diff --git a/src/calibre/ebooks/pdf/reflow.py b/src/calibre/ebooks/pdf/reflow.py index a7708ea8ce..337147d8a7 100644 --- a/src/calibre/ebooks/pdf/reflow.py +++ b/src/calibre/ebooks/pdf/reflow.py @@ -169,7 +169,7 @@ class Column(object): self._post_add() def _post_add(self): - self.elements.sort(cmp=lambda x,y:cmp(x.bottom,y.bottom)) + self.elements.sort(key=lambda x: x.bottom) self.top = self.elements[0].top self.bottom = self.elements[-1].bottom self.left, self.right = sys.maxint, 0 @@ -260,7 +260,7 @@ class Region(object): def add(self, columns): if not self.columns: - for x in sorted(columns, cmp=lambda x,y: cmp(x.left, y.left)): + for x in sorted(columns, key=lambda x: x.left): self.columns.append(x) else: for i in range(len(columns)): @@ -458,7 +458,7 @@ class Page(object): self.elements = list(self.texts) for img in page.xpath('descendant::img'): self.elements.append(Image(img, self.opts, self.log, idc)) - self.elements.sort(cmp=lambda x,y:cmp(x.top, y.top)) + self.elements.sort(key=lambda x: x.top) def coalesce_fragments(self): @@ -580,7 +580,7 @@ class Page(object): def sort_into_columns(self, elem, neighbors): neighbors.add(elem) - neighbors = sorted(neighbors, cmp=lambda x,y:cmp(x.left, y.left)) + neighbors = sorted(neighbors, key=lambda x: x.left) if self.opts.verbose > 3: self.log.debug('Neighbors:', [x.to_html() for x in neighbors]) columns = [Column()] @@ -595,7 +595,7 @@ class Page(object): if not added: columns.append(Column()) columns[-1].add(x) - columns.sort(cmp=lambda x,y:cmp(x.left, y.left)) + columns.sort(key=lambda x: x.left) return columns def find_elements_in_row_of(self, x): diff --git a/src/calibre/gui2/actions/choose_library.py b/src/calibre/gui2/actions/choose_library.py index a8adb593e4..a562958470 100644 --- a/src/calibre/gui2/actions/choose_library.py +++ b/src/calibre/gui2/actions/choose_library.py @@ -51,8 +51,7 @@ class LibraryUsageStats(object): # {{{ def write_stats(self): locs = list(self.stats.keys()) - locs.sort(cmp=lambda x, y: cmp(self.stats[x], self.stats[y]), - reverse=True) + locs.sort(key=lambda x: self.stats[x], reverse=True) for key in locs[500:]: self.stats.pop(key) gprefs.set('library_usage_stats', self.stats) diff --git a/src/calibre/gui2/dialogs/catalog.py b/src/calibre/gui2/dialogs/catalog.py index 1f32406b76..f10eab4384 100644 --- a/src/calibre/gui2/dialogs/catalog.py +++ b/src/calibre/gui2/dialogs/catalog.py @@ -93,7 +93,7 @@ class Catalog(QDialog, Ui_Dialog): else: info("No dynamic tab resources found for %s" % name) - self.widgets = sorted(self.widgets, cmp=lambda x,y:cmp(x.TITLE, y.TITLE)) + self.widgets = sorted(self.widgets, key=lambda x: x.TITLE) # Generate a sorted list of installed catalog formats/sync_enabled pairs fmts = sorted([x[0] for x in self.fmts]) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index b36f91798c..d058e396e0 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -323,7 +323,7 @@ class BooksModel(QAbstractTableModel): # {{{ return 100000 return self.db.field_metadata[name]['rec_index'] - self.column_map.sort(cmp=lambda x,y: cmp(col_idx(x), col_idx(y))) + self.column_map.sort(key=lambda x: col_idx(x)) for col in self.column_map: if col in self.orig_headers: self.headers[col] = self.orig_headers[col] diff --git a/src/calibre/gui2/library/views.py b/src/calibre/gui2/library/views.py index a49f7c778f..a5ce76815d 100644 --- a/src/calibre/gui2/library/views.py +++ b/src/calibre/gui2/library/views.py @@ -1031,7 +1031,7 @@ class BooksView(QTableView): # {{{ h.visualIndex(x) > -1] if not pairs: pairs = [(0, 0)] - pairs.sort(cmp=lambda x,y:cmp(x[1], y[1])) + pairs.sort(key=lambda x: x[1]) i = pairs[0][0] index = self.model().index(row, i) if for_sync: diff --git a/src/calibre/gui2/preferences/columns.py b/src/calibre/gui2/preferences/columns.py index 0cd0fe5bea..3d346c41fb 100644 --- a/src/calibre/gui2/preferences/columns.py +++ b/src/calibre/gui2/preferences/columns.py @@ -69,7 +69,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): state = self.columns_state(defaults) self.hidden_cols = state['hidden_columns'] positions = state['column_positions'] - colmap.sort(cmp=lambda x,y: cmp(positions[x], positions[y])) + colmap.sort(key=lambda x: positions[x]) self.opt_columns.clear() db = model.db @@ -248,12 +248,10 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): if 'ondevice' in hidden_cols: hidden_cols.remove('ondevice') - def col_pos(x, y): - xidx = config_cols.index(x) if x in config_cols else sys.maxint - yidx = config_cols.index(y) if y in config_cols else sys.maxint - return cmp(xidx, yidx) + def col_pos(x): + return config_cols.index(x) if x in config_cols else sys.maxint positions = {} - for i, col in enumerate((sorted(model.column_map, cmp=col_pos))): + for i, col in enumerate((sorted(model.column_map, key=col_pos))): positions[col] = i state = {'hidden_columns': hidden_cols, 'column_positions':positions} self.gui.library_view.apply_state(state) diff --git a/src/calibre/gui2/preferences/look_feel.py b/src/calibre/gui2/preferences/look_feel.py index 0fb52d9a97..258ffcdc15 100644 --- a/src/calibre/gui2/preferences/look_feel.py +++ b/src/calibre/gui2/preferences/look_feel.py @@ -456,7 +456,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): if l != lang] if lang != 'en': items.append(('en', get_esc_lang('en'))) - items.sort(cmp=lambda x, y: cmp(x[1].lower(), y[1].lower())) + items.sort(key=lambda x: x[1].lower()) choices = [(y, x) for x, y in items] # Default language is the autodetected one choices = [(get_language(lang), lang)] + choices diff --git a/src/calibre/gui2/preferences/main.py b/src/calibre/gui2/preferences/main.py index 3b8cc99c45..43f4386d5b 100644 --- a/src/calibre/gui2/preferences/main.py +++ b/src/calibre/gui2/preferences/main.py @@ -171,7 +171,7 @@ class Browser(QScrollArea): # {{{ self.category_names = category_names categories = list(category_map.keys()) - categories.sort(cmp=lambda x, y: cmp(category_map[x], category_map[y])) + categories.sort(key=lambda x: category_map[x]) self.category_map = OrderedDict() for c in categories: @@ -181,7 +181,7 @@ class Browser(QScrollArea): # {{{ self.category_map[plugin.category].append(plugin) for plugins in self.category_map.values(): - plugins.sort(cmp=lambda x, y: cmp(x.name_order, y.name_order)) + plugins.sort(key=lambda x: x.name_order) self.widgets = [] self._layout = QVBoxLayout() diff --git a/src/calibre/gui2/preferences/plugboard.py b/src/calibre/gui2/preferences/plugboard.py index 5005e87735..65dea1c87d 100644 --- a/src/calibre/gui2/preferences/plugboard.py +++ b/src/calibre/gui2/preferences/plugboard.py @@ -34,17 +34,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.db = gui.library_view.model().db def initialize(self): - def field_cmp(x, y): - if x.startswith('#'): - if y.startswith('#'): - return cmp(x.lower(), y.lower()) - else: - return 1 - elif y.startswith('#'): - return -1 - else: - return cmp(x.lower(), y.lower()) - ConfigWidgetBase.initialize(self) self.current_plugboards = copy.deepcopy(self.db.prefs.get('plugboards',{})) @@ -73,7 +62,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): if n not in self.disabled_devices: self.disabled_devices.append(n) - self.devices.sort(cmp=lambda x, y: cmp(x.lower(), y.lower())) + self.devices.sort(key=lambda x: x.lower()) self.devices.insert(1, plugboard_save_to_disk_value) self.devices.insert(1, plugboard_content_server_value) self.device_to_formats_map[plugboard_content_server_value] = \ diff --git a/src/calibre/gui2/preferences/plugins.py b/src/calibre/gui2/preferences/plugins.py index a683eccf4d..c3c777f1e9 100644 --- a/src/calibre/gui2/preferences/plugins.py +++ b/src/calibre/gui2/preferences/plugins.py @@ -61,7 +61,7 @@ class PluginModel(QAbstractItemModel, AdaptSQP): # {{{ self.categories = sorted(self._data.keys()) for plugins in self._data.values(): - plugins.sort(cmp=lambda x, y: cmp(x.name.lower(), y.name.lower())) + plugins.sort(key=lambda x: x.name.lower()) def universal_set(self): ans = set([]) diff --git a/src/calibre/gui2/wizard/__init__.py b/src/calibre/gui2/wizard/__init__.py index ec61d6c0fe..e137babbbe 100644 --- a/src/calibre/gui2/wizard/__init__.py +++ b/src/calibre/gui2/wizard/__init__.py @@ -427,7 +427,7 @@ def get_manufacturers(): def get_devices_of(manufacturer): ans = [d for d in get_devices() if d.manufacturer == manufacturer] - return sorted(ans, cmp=lambda x,y:cmp(x.name, y.name)) + return sorted(ans, key=lambda x: x.name) class ManufacturerModel(QAbstractListModel): @@ -682,7 +682,7 @@ class LibraryPage(QWizardPage, LibraryUI): if l != lang] if lang != 'en': items.append(('en', get_esc_lang('en'))) - items.sort(cmp=lambda x, y: cmp(x[1], y[1])) + items.sort(key=lambda x: x[1]) for item in items: self.language.addItem(item[1], (item[0])) self.language.blockSignals(False) diff --git a/src/calibre/library/custom_columns.py b/src/calibre/library/custom_columns.py index 56e53ee91b..ad2ae31e39 100644 --- a/src/calibre/library/custom_columns.py +++ b/src/calibre/library/custom_columns.py @@ -216,7 +216,7 @@ class CustomColumns(object): if data['is_multiple'] and data['datatype'] == 'text': ans = ans.split(data['multiple_seps']['cache_to_list']) if ans else [] if data['display'].get('sort_alpha', False): - ans.sort(cmp=lambda x,y:cmp(x.lower(), y.lower())) + ans.sort(key=lambda x:x.lower()) return ans def get_custom_extra(self, idx, label=None, num=None, index_is_id=False): @@ -243,7 +243,7 @@ class CustomColumns(object): if data['is_multiple'] and data['datatype'] == 'text': ans = ans.split(data['multiple_seps']['cache_to_list']) if ans else [] if data['display'].get('sort_alpha', False): - ans.sort(cmp=lambda x,y:cmp(x.lower(), y.lower())) + ans.sort(key=lambda x: x.lower()) if data['datatype'] != 'series': return (ans, None) ign,lt = self.custom_table_names(data['num']) diff --git a/src/calibre/library/database.py b/src/calibre/library/database.py index b303833439..8ce65a023e 100644 --- a/src/calibre/library/database.py +++ b/src/calibre/library/database.py @@ -1018,7 +1018,7 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE; if not ans: return [] ans = [id[0] for id in ans] - ans.sort(cmp=lambda x, y: cmp(self.series_index(x, True), self.series_index(y, True))) + ans.sort(key=lambda x: self.series_index(x, True)) return ans def books_in_series_of(self, index, index_is_id=False): diff --git a/src/calibre/utils/smtp.py b/src/calibre/utils/smtp.py index d840cc4c37..8da8cc7bd2 100644 --- a/src/calibre/utils/smtp.py +++ b/src/calibre/utils/smtp.py @@ -98,8 +98,7 @@ def get_mx(host, verbose=0): if verbose: print('Find mail exchanger for', host) answers = list(dns.resolver.query(host, 'MX')) - answers.sort(cmp=lambda x, y: cmp(int(getattr(x, 'preference', sys.maxint)), - int(getattr(y, 'preference', sys.maxint)))) + answers.sort(key=lambda x: int(getattr(x, 'preference', sys.maxint))) return [str(x.exchange) for x in answers if hasattr(x, 'exchange')] diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index f51f7e5a21..101e68b510 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -760,7 +760,7 @@ class BasicNewsRecipe(Recipe): in index are not in weights, they are assumed to have a weight of 0. ''' weights = defaultdict(lambda: 0, weights) - index.sort(cmp=lambda x, y: cmp(weights[x], weights[y])) + index.sort(key=lambda x: weights[x]) return index def parse_index(self):