diff --git a/pyproject.toml b/pyproject.toml index 7345d0f78f..295c3be9cb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,7 +114,7 @@ select = [ 'E', 'F', 'I', 'W', 'INT', 'Q', 'UP', 'YTT', 'TID', 'C4', 'COM818', 'PIE', 'RET501', 'ISC', 'PLE', 'PLW', 'PLC', - 'PLR0402', 'PLR1711', 'PLR1716', 'PLR1722', 'PLR1733', 'PLR1736', 'PLR1730', 'PLR5501', + 'PLR0402', 'PLR1711', 'PLR1716', 'PLR1722', 'PLR1733', 'PLR1736', 'PLR1730', 'PLR5501', 'PLR1714', 'RUF', # note: RUF can flag many unsolicited errors # preview rules 'RUF051', 'RUF056', # useless dict operation diff --git a/recipes/calgary_herald.recipe b/recipes/calgary_herald.recipe index 99cb4f5bd1..0e35443ba5 100644 --- a/recipes/calgary_herald.recipe +++ b/recipes/calgary_herald.recipe @@ -248,7 +248,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/edmonton_journal.recipe b/recipes/edmonton_journal.recipe index 90490cea83..d861c6cd9e 100644 --- a/recipes/edmonton_journal.recipe +++ b/recipes/edmonton_journal.recipe @@ -248,7 +248,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/faz_net.recipe b/recipes/faz_net.recipe index e1ac34f1b4..536e62cdc4 100644 --- a/recipes/faz_net.recipe +++ b/recipes/faz_net.recipe @@ -9,7 +9,7 @@ def format_tickaroo_liveblog(soup): sources = img['srcset'].split() i=0 for x in sources: - if x == '960w,' or x == '960w': + if x in {'960w,', '960w'}: img['src'] = sources[i-1] break i = i + 1 diff --git a/recipes/montreal_gazette.recipe b/recipes/montreal_gazette.recipe index 7dd5aef2fe..b10a2a791a 100644 --- a/recipes/montreal_gazette.recipe +++ b/recipes/montreal_gazette.recipe @@ -248,7 +248,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/nrc_next.recipe b/recipes/nrc_next.recipe index 237feaac3c..05fdd3648a 100644 --- a/recipes/nrc_next.recipe +++ b/recipes/nrc_next.recipe @@ -65,7 +65,7 @@ class NRCNext(BasicNewsRecipe): subst = '\\1' for name in namelist: _, ext = os.path.splitext(name) - if (ext == '.html') or (ext == '.ncx'): + if ext in {'.html', '.ncx'}: fname = os.path.join(self.output_dir, name) with open(fname) as f: s = f.read() diff --git a/recipes/ottawa_citizen.recipe b/recipes/ottawa_citizen.recipe index efe993007b..9233fb4f1c 100644 --- a/recipes/ottawa_citizen.recipe +++ b/recipes/ottawa_citizen.recipe @@ -248,7 +248,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/vancouver_province.recipe b/recipes/vancouver_province.recipe index deb46aa06f..fdc3e4c2a1 100644 --- a/recipes/vancouver_province.recipe +++ b/recipes/vancouver_province.recipe @@ -261,7 +261,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/vancouver_sun.recipe b/recipes/vancouver_sun.recipe index 9b78380d83..692609660d 100644 --- a/recipes/vancouver_sun.recipe +++ b/recipes/vancouver_sun.recipe @@ -249,7 +249,7 @@ class CanWestPaper(BasicNewsRecipe): ans = [] def handle_article(adiv, key): - if adiv.name == 'h1' or adiv.name == 'h3': + if adiv.name in {'h1', 'h3'}: h1tag = adiv else: h1tag = adiv.h1 diff --git a/recipes/vic_times.recipe b/recipes/vic_times.recipe index c5d2e76ca8..02b26b3001 100644 --- a/recipes/vic_times.recipe +++ b/recipes/vic_times.recipe @@ -194,7 +194,7 @@ class TimesColonist(BasicNewsRecipe): ptext = self.tag_to_string( ptag, use_alt=False, normalize_whitespace=True) ptext = re.sub(r'\s+', '', ptext) - if (ptext == '') or (ptext == ' '): + if ptext in {'', ' '}: ptag.extract() return self.strip_anchors(soup) diff --git a/setup/build.py b/setup/build.py index d7f90d4539..8282960ab2 100644 --- a/setup/build.py +++ b/setup/build.py @@ -441,7 +441,7 @@ class Build(Command): os.makedirs(x, exist_ok=True) pyqt_extensions, extensions = [], [] for ext in all_extensions: - if opts.only != 'all' and opts.only != ext.name: + if opts.only not in ('all', ext.name): continue if not is_ext_allowed(self.compiling_for, ext): continue diff --git a/src/calibre/db/write.py b/src/calibre/db/write.py index b6d727494b..779dea8c48 100644 --- a/src/calibre/db/write.py +++ b/src/calibre/db/write.py @@ -104,7 +104,7 @@ def adapt_bool(x): x = True elif x == 'false': x = False - elif x == 'none' or x == '': + elif x in {'none', ''}: x = None else: x = bool(int(x)) diff --git a/src/calibre/devices/kobo/driver.py b/src/calibre/devices/kobo/driver.py index c10a582dca..2ae1976872 100644 --- a/src/calibre/devices/kobo/driver.py +++ b/src/calibre/devices/kobo/driver.py @@ -252,7 +252,7 @@ class KOBO(USBMS): elif oncard == 'cardb' and not self._card_b_prefix: self.report_progress(1.0, _('Getting list of books on device...')) return dummy_bl - elif oncard and oncard != 'carda' and oncard != 'cardb': + elif oncard and oncard not in {'carda', 'cardb'}: self.report_progress(1.0, _('Getting list of books on device...')) return dummy_bl @@ -262,7 +262,7 @@ class KOBO(USBMS): self.fwversion = self.get_firmware_version() - if not (self.fwversion == (1,0) or self.fwversion == (1,4)): + if self.fwversion not in ((1, 0), (1, 4)): self.has_kepubs = True debug_print('Version of driver: ', self.version, 'Has kepubs:', self.has_kepubs) debug_print('Version of firmware: ', self.fwversion, 'Has kepubs:', self.has_kepubs) @@ -418,7 +418,7 @@ class KOBO(USBMS): path = self.path_from_contentid(row['ContentID'], row['ContentType'], row['MimeType'], oncard) mime = mime_type_ext(path_to_ext(path)) if path.find('kepub') == -1 else 'application/epub+zip' # debug_print("mime:", mime) - if oncard != 'carda' and oncard != 'cardb' and not row['ContentID'].startswith('file:///mnt/sd/'): + if oncard not in {'carda', 'cardb'} and not row['ContentID'].startswith('file:///mnt/sd/'): prefix = self._main_prefix elif oncard == 'carda' and row['ContentID'].startswith('file:///mnt/sd/'): prefix = self._card_a_prefix @@ -679,12 +679,12 @@ class KOBO(USBMS): # Kobo books do not have book files. They do have some images though # print('kobo book') ContentType = 6 - elif extension == '.pdf' or extension == '.epub': + elif extension in {'.pdf', '.epub'}: # print('ePub or pdf') ContentType = 16 - elif extension == '.rtf' or extension == '.txt' or extension == '.htm' or extension == '.html': + elif extension in {'.rtf', '.txt', '.htm', '.html'}: # print('txt') - if self.fwversion == (1,0) or self.fwversion == (1,4) or self.fwversion == (1,7,4): + if self.fwversion in ((1, 0), (1, 4), (1, 7, 4)): ContentType = 999 else: ContentType = 901 @@ -709,7 +709,7 @@ class KOBO(USBMS): # for calibre's reference path = self._main_prefix + path + '.kobo' # print('Path: ' + path) - elif (ContentType == '6' or ContentType == '10') and ( + elif (ContentType in {'6', '10'}) and ( MimeType == 'application/x-kobo-epub+zip' or ( MimeType == 'application/epub+zip' and self.isTolinoDevice()) ): @@ -823,7 +823,7 @@ class KOBO(USBMS): # Reset Im_Reading list in the database if oncard == 'carda': query= "update content set ReadStatus=0, FirstTimeReading = 'true' where BookID is Null and ContentID like 'file:///mnt/sd/%'" - elif oncard != 'carda' and oncard != 'cardb': + elif oncard not in {'carda', 'cardb'}: query= "update content set ReadStatus=0, FirstTimeReading = 'true' where BookID is Null and ContentID not like 'file:///mnt/sd/%'" try: @@ -868,7 +868,7 @@ class KOBO(USBMS): # Reset FavouritesIndex list in the database if oncard == 'carda': query= "update content set FavouritesIndex=-1 where BookID is Null and ContentID like 'file:///mnt/sd/%'" - elif oncard != 'carda' and oncard != 'cardb': + elif oncard not in {'carda', 'cardb'}: query= "update content set FavouritesIndex=-1 where BookID is Null and ContentID not like 'file:///mnt/sd/%'" cursor = connection.cursor() @@ -1701,7 +1701,7 @@ class KOBOTOUCH(KOBO): self.report_progress(1.0, _('Getting list of books on device...')) debug_print("KoboTouch:books - Asked to process 'cardb', but do not have one!") return dummy_bl - elif oncard and oncard != 'carda' and oncard != 'cardb': + elif oncard and oncard not in {'carda', 'cardb'}: self.report_progress(1.0, _('Getting list of books on device...')) debug_print('KoboTouch:books - unknown card') return dummy_bl @@ -1797,7 +1797,7 @@ class KOBOTOUCH(KOBO): allow_shelves = False if show_debug: debug_print('KoboTouch:update_booklist - have a deleted book') - elif self.supports_kobo_archive() and (accessibility == 1 or accessibility == 2): + elif self.supports_kobo_archive() and (accessibility in {1, 2}): playlist_map[lpath].append('Archived') allow_shelves = True @@ -2192,7 +2192,7 @@ class KOBOTOUCH(KOBO): if oncard == 'cardb': print('path from_contentid cardb') - elif (ContentType == '6' or ContentType == '10'): + elif (ContentType in {'6', '10'}): if (MimeType == 'application/octet-stream'): # Audiobooks purchased from Kobo are in a different location. path = self._main_prefix + KOBO_ROOT_DIR_NAME + '/audiobook/' + path elif (MimeType == 'audio/mpeg' and self.isTolinoDevice()): @@ -3305,7 +3305,7 @@ class KOBOTOUCH(KOBO): values.append(ContentID) elif oncard == 'carda': query += " WHERE ContentID like 'file:///mnt/sd/%'" - elif oncard != 'carda' and oncard != 'cardb': + elif oncard not in {'carda', 'cardb'}: query += " WHERE ContentID not like 'file:///mnt/sd/%'" if bookshelves: diff --git a/src/calibre/devices/mtp/driver.py b/src/calibre/devices/mtp/driver.py index d377abbee4..171d4077d0 100644 --- a/src/calibre/devices/mtp/driver.py +++ b/src/calibre/devices/mtp/driver.py @@ -362,7 +362,7 @@ class MTP_DEVICE(BASE): metadata = get_metadata(stream, stream_type=ext, force_read_metadata=True, pattern=self.build_template_regexp()) - if metadata.title != 'metadata' and metadata.title != 'Unknown': + if metadata.title not in {'metadata', 'Unknown'}: return metadata stream = self.get_mtp_file(mtp_file) with quick_metadata: diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index cdbd120903..87ffd89887 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -206,7 +206,7 @@ class USBMS(CLI, Device): elif oncard == 'cardb' and not self._card_b_prefix: self.report_progress(1.0, _('Getting list of books on device...')) return dummy_bl - elif oncard and oncard != 'carda' and oncard != 'cardb': + elif oncard and oncard not in {'carda', 'cardb'}: self.report_progress(1.0, _('Getting list of books on device...')) return dummy_bl diff --git a/src/calibre/ebooks/conversion/preprocess.py b/src/calibre/ebooks/conversion/preprocess.py index 5901216e24..4a1e25ac84 100644 --- a/src/calibre/ebooks/conversion/preprocess.py +++ b/src/calibre/ebooks/conversion/preprocess.py @@ -224,7 +224,7 @@ class Dehyphenator: searchresult = self.html.find(lookupword.lower()) except Exception: return hyphenated - if self.format == 'html_cleanup' or self.format == 'txt_cleanup': + if self.format in {'html_cleanup', 'txt_cleanup'}: if self.html.find(lookupword) != -1 or searchresult != -1: if self.verbose > 2: self.log(' Cleanup:returned dehyphenated word: ' + dehyphenated) diff --git a/src/calibre/ebooks/lrf/pylrs/pylrfopt.py b/src/calibre/ebooks/lrf/pylrs/pylrfopt.py index 0461122e6b..d5b9d0ea68 100644 --- a/src/calibre/ebooks/lrf/pylrs/pylrfopt.py +++ b/src/calibre/ebooks/lrf/pylrs/pylrfopt.py @@ -2,7 +2,7 @@ def _optimize(tagList, tagName, conversion): # copy the tag of interest plus any text newTagList = [] for tag in tagList: - if tag.name == tagName or tag.name == 'rawtext': + if tag.name in (tagName, 'rawtext'): newTagList.append(tag) # now, eliminate any duplicates (leaving the last one) diff --git a/src/calibre/ebooks/metadata/kfx.py b/src/calibre/ebooks/metadata/kfx.py index cd1c4ba092..0af76c0ff4 100644 --- a/src/calibre/ebooks/metadata/kfx.py +++ b/src/calibre/ebooks/metadata/kfx.py @@ -194,7 +194,7 @@ class PackedIon(PackedData): if data_type == DT_STRING: return self.extract(data_len).decode('utf8') - if data_type == DT_STRUCT or data_type == DT_LIST: + if data_type in (DT_STRUCT, DT_LIST): ion = PackedIon(self.extract(data_len)) result = [] diff --git a/src/calibre/ebooks/mobi/reader/mobi6.py b/src/calibre/ebooks/mobi/reader/mobi6.py index 7ae07fd70a..9394cfea62 100644 --- a/src/calibre/ebooks/mobi/reader/mobi6.py +++ b/src/calibre/ebooks/mobi/reader/mobi6.py @@ -862,7 +862,7 @@ class MobiReader: l = self.mobi_html.find(b'<', end) r = self.mobi_html.find(b'>', end) anchor = b'' - if r > -1 and (r < l or l == end or l == -1): + if r > -1 and (r < l or l in (end, -1)): p = self.mobi_html.rfind(b'<', 0, end + 1) if (pos < end and p > -1 and not end_tag_re.match(self.mobi_html[p:r]) and not self.mobi_html[p:r + 1].endswith(b'/>')): diff --git a/src/calibre/ebooks/pdb/palmdoc/reader.py b/src/calibre/ebooks/pdb/palmdoc/reader.py index 797356138d..e25b0cb742 100644 --- a/src/calibre/ebooks/pdb/palmdoc/reader.py +++ b/src/calibre/ebooks/pdb/palmdoc/reader.py @@ -44,7 +44,7 @@ class Reader(FormatReader): def decompress_text(self, number): if self.header_record.compression == 1: return self.section_data(number) - if self.header_record.compression == 2 or self.header_record.compression == 258: + if self.header_record.compression in {2, 258}: from calibre.ebooks.compression.palmdoc import decompress_doc return decompress_doc(self.section_data(number)) return b'' diff --git a/src/calibre/ebooks/pdf/html_writer.py b/src/calibre/ebooks/pdf/html_writer.py index f86cd91db0..2a262d09a5 100644 --- a/src/calibre/ebooks/pdf/html_writer.py +++ b/src/calibre/ebooks/pdf/html_writer.py @@ -124,7 +124,7 @@ def fix_fullscreen_images(container): svg = None for elem in body.iterdescendants('*'): name = local_name(elem.tag) - if name != 'style' and name != 'script': + if name not in {'style', 'script'}: names.append(name) if name == 'svg': svg = elem diff --git a/src/calibre/ebooks/pdf/reflow.py b/src/calibre/ebooks/pdf/reflow.py index c61648d568..b41781c29b 100644 --- a/src/calibre/ebooks/pdf/reflow.py +++ b/src/calibre/ebooks/pdf/reflow.py @@ -278,8 +278,7 @@ class Text(Element): if m_self and m_other: self.raw = m_self.group(1) other.raw = m_other.group(1) - elif self.font_size_em != other.font_size_em \ - and self.font_size_em != 1.00: + elif self.font_size_em not in (other.font_size_em, 1.0): if not self.raw.startswith(r'{self.raw}' # Try to allow for a very large initial character @@ -949,13 +948,7 @@ class Page: t.tag = 'h2' # It won't get set later # Centered if left and right margins are within FACTOR% # Because indents can waver a bit, use between indent_min and indent_max as == indent - if (lmargin < indent_min or lmargin > indent_max) \ - and lmargin > left_max \ - and lmargin != xmargin \ - and lmargin != ymargin \ - and lmargin >= rmargin - rmargin*CENTER_FACTOR \ - and lmargin <= rmargin + rmargin*CENTER_FACTOR \ - and '"float:right"' not in t.raw: + if (lmargin < indent_min or lmargin > indent_max) and lmargin > left_max and lmargin not in (xmargin, ymargin) and lmargin >= rmargin - rmargin * CENTER_FACTOR and lmargin <= rmargin + rmargin * CENTER_FACTOR and '"float:right"' not in t.raw: # and t.left + t.width + t.left >= self.width + l_offset - t.average_character_width \ # and t.left + t.width + t.left <= self.width + l_offset + t.average_character_width: t.align = 'C' @@ -1311,7 +1304,7 @@ class Page: # Font sizes start as pixels/points, but em is more useful for text in self.texts: text.font_size_em = self.font_map[text.font.id].size_em - if text.font_size_em != 0.00 and text.font_size_em != 1.00: + if text.font_size_em not in {0.0, 1.0}: text.raw = f'{text.raw}' def second_pass(self, stats, opts): diff --git a/src/calibre/ebooks/pdf/render/serialize.py b/src/calibre/ebooks/pdf/render/serialize.py index 8d060d1c05..b4ba43ff48 100644 --- a/src/calibre/ebooks/pdf/render/serialize.py +++ b/src/calibre/ebooks/pdf/render/serialize.py @@ -426,7 +426,7 @@ class PDFStream: if fmt == QImage.Format.Format_MonoLSB: image = image.convertToFormat(QImage.Format.Format_Mono) fmt = QImage.Format.Format_Mono - elif (fmt != QImage.Format.Format_RGB32 and fmt != QImage.Format.Format_ARGB32): + elif (fmt not in (QImage.Format.Format_RGB32, QImage.Format.Format_ARGB32)): image = image.convertToFormat(QImage.Format.Format_ARGB32) fmt = QImage.Format.Format_ARGB32 diff --git a/src/calibre/ebooks/readability/readability.py b/src/calibre/ebooks/readability/readability.py index a9400901e6..a445b71691 100644 --- a/src/calibre/ebooks/readability/readability.py +++ b/src/calibre/ebooks/readability/readability.py @@ -395,7 +395,7 @@ class Document: if counts['p'] and counts['img'] > counts['p']: reason = 'too many images ({})'.format(counts['img']) to_remove = True - elif counts['li'] > counts['p'] and tag != 'ul' and tag != 'ol': + elif counts['li'] > counts['p'] and tag not in {'ul', 'ol'}: reason = 'more
s'
to_remove = True
elif counts['input'] > (counts['p'] / 3):
diff --git a/src/calibre/ebooks/rtf2xml/configure_txt.py b/src/calibre/ebooks/rtf2xml/configure_txt.py
index 5582214397..0130a38d48 100644
--- a/src/calibre/ebooks/rtf2xml/configure_txt.py
+++ b/src/calibre/ebooks/rtf2xml/configure_txt.py
@@ -115,7 +115,7 @@ class Configure:
smart_output = return_dict.get('smart-output')
if not smart_output:
return_dict['smart-output'] = 0
- elif smart_output != 'true' and smart_output != 'false':
+ elif smart_output not in {'true', 'false'}:
sys.stderr.write('"smart-output" must be true or false.\n')
return 1
elif smart_output == 'false':
@@ -143,7 +143,7 @@ class Configure:
value = return_dict.get(font)
if not value:
return_dict[font] = 0
- elif value != 'true' and value != 'false':
+ elif value not in {'true', 'false'}:
sys.stderr.write(
f'"{font}" must be true or false.\n')
elif value == 'false':
diff --git a/src/calibre/ebooks/rtf2xml/fields_small.py b/src/calibre/ebooks/rtf2xml/fields_small.py
index 2d0b109a21..7dca1f424e 100644
--- a/src/calibre/ebooks/rtf2xml/fields_small.py
+++ b/src/calibre/ebooks/rtf2xml/fields_small.py
@@ -353,7 +353,7 @@ file.
else:
toc_string += f'{line}\n'
else:
- if token_info == 'cw