From a1d21e36322f0c0615d9440b6ccfb68dfde7f5bc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 9 Jan 2023 19:13:32 +0530 Subject: [PATCH] Manual fixes for misreports from ruff --- =template.py | 5 ----- src/calibre/db/search.py | 6 ++++-- src/calibre/db/tests/legacy.py | 10 ++++++---- src/calibre/db/tests/reading.py | 5 +++-- src/calibre/ebooks/lrf/html/convert_from.py | 2 +- src/calibre/gui2/metadata/single_download.py | 5 +++-- src/calibre/library/database2.py | 3 ++- 7 files changed, 19 insertions(+), 17 deletions(-) delete mode 100644 =template.py diff --git a/=template.py b/=template.py deleted file mode 100644 index 1d4fcfab6c..0000000000 --- a/=template.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python -# vim:fileencoding=utf-8 -# License: GPL v3 Copyright: %YEAR%, %USER% <%MAIL%> - -%HERE% diff --git a/src/calibre/db/search.py b/src/calibre/db/search.py index 8e7b4b106d..60d48cee3f 100644 --- a/src/calibre/db/search.py +++ b/src/calibre/db/search.py @@ -237,11 +237,12 @@ class NumericSearch: # {{{ dt = datatype if is_many and query in {'true', 'false'}: - def valcheck(x): - return True if datatype == 'rating': def valcheck(x): return (x is not None and x > 0) + else: + def valcheck(x): + return True found = set() for val, book_ids in field_iter(): if valcheck(val): @@ -273,6 +274,7 @@ class NumericSearch: # {{{ if dt == 'rating': def cast(x): return (0 if x is None else int(x)) + def adjust(x): return (x // 2) else: diff --git a/src/calibre/db/tests/legacy.py b/src/calibre/db/tests/legacy.py index 194f682b23..2ba66b3ae8 100644 --- a/src/calibre/db/tests/legacy.py +++ b/src/calibre/db/tests/legacy.py @@ -57,8 +57,6 @@ def run_funcs(self, db, ndb, funcs): if callable(meth): meth(*args) else: - def fmt(x): - return x if meth[0] in {'!', '@', '#', '+', '$', '-', '%'}: if meth[0] != '+': fmt = {'!':dict, '@':lambda x:frozenset(x or ()), '#':lambda x:set((x or '').split(',')), @@ -68,6 +66,9 @@ def run_funcs(self, db, ndb, funcs): fmt = args[-1] args = args[:-1] meth = meth[1:] + else: + def fmt(x): + return x res1, res2 = fmt(getattr(db, meth)(*args)), fmt(getattr(ndb, meth)(*args)) self.assertEqual(res1, res2, f'The method: {meth}() returned different results for argument {args}') # }}} @@ -257,14 +258,15 @@ class LegacyTest(BaseTest): 'books_in_series_of':[(0,), (1,), (2,)], 'books_with_same_title':[(Metadata(db.title(0)),), (Metadata(db.title(1)),), (Metadata('1234'),)], }): - def fmt(x): - return x if meth[0] in {'!', '@'}: fmt = {'!':dict, '@':frozenset}[meth[0]] meth = meth[1:] elif meth == 'get_authors_with_ids': def fmt(val): return {x[0]: tuple(x[1:]) for x in val} + else: + def fmt(x): + return x for a in args: self.assertEqual(fmt(getattr(db, meth)(*a)), fmt(getattr(ndb, meth)(*a)), f'The method: {meth}() returned different results for argument {a}') diff --git a/src/calibre/db/tests/reading.py b/src/calibre/db/tests/reading.py index 907844d0c8..a6daf73d18 100644 --- a/src/calibre/db/tests/reading.py +++ b/src/calibre/db/tests/reading.py @@ -612,11 +612,12 @@ class ReadingTest(BaseTest): self.assertSetEqual(set(mi.custom_field_keys()), set(pmi.custom_field_keys())) for field in STANDARD_METADATA_FIELDS | {'#series_index'}: - def f(x): - return x if field == 'formats': def f(x): return (x if x is None else tuple(x)) + else: + def f(x): + return x self.assertEqual(f(getattr(mi, field)), f(getattr(pmi, field)), f'Standard field: {field} not the same for book {book_id}') self.assertEqual(mi.format_field(field), pmi.format_field(field), diff --git a/src/calibre/ebooks/lrf/html/convert_from.py b/src/calibre/ebooks/lrf/html/convert_from.py index 5f67b0ebf1..589ba395ec 100644 --- a/src/calibre/ebooks/lrf/html/convert_from.py +++ b/src/calibre/ebooks/lrf/html/convert_from.py @@ -817,7 +817,7 @@ class HTMLConverter: valign = css['vertical-align'] if valign in ('sup', 'super', 'sub'): fp['fontsize'] = int(fp['fontsize']) * 5 // 3 - valigner = Sub if valign == 'sub' else Sup + valigner = Sub if valign == 'sub' else Sup # noqa normal_font_size = int(fp['fontsize']) if variant == 'small-caps': diff --git a/src/calibre/gui2/metadata/single_download.py b/src/calibre/gui2/metadata/single_download.py index 4cfad54656..e31f42e62b 100644 --- a/src/calibre/gui2/metadata/single_download.py +++ b/src/calibre/gui2/metadata/single_download.py @@ -181,8 +181,6 @@ class ResultsModel(QAbstractTableModel): # {{{ return None def sort(self, col, order=Qt.SortOrder.AscendingOrder): - def key(x): - return x if col == 0: key = attrgetter('gui_rank') elif col == 1: @@ -199,6 +197,9 @@ class ResultsModel(QAbstractTableModel): # {{{ elif key == 4: def key(x): return bool(x.comments) + else: + def key(x): + return x self.beginResetModel() self.results.sort(key=key, reverse=order==Qt.SortOrder.AscendingOrder) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 200f95e584..1632921287 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1973,6 +1973,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): icon_map[category] = icon datatype = cat['datatype'] + def avgr(x): return (0.0 if x.rc == 0 else x.rt / x.rc) # Duplicate the build of items below to avoid using a lambda func @@ -1980,7 +1981,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if datatype == 'rating': def formatter(x): return ('★' * int(x // 2)) - def avgr(x): + def avgr(x): # noqa return x.n # eliminate the zero ratings line as well as count == 0 items = [v for v in tcategories[category].values() if v.c > 0 and v.n != 0]