From d68bb78f446966efcfa7ab9b8c9f970150d6c2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 14 Oct 2024 20:53:49 +0200 Subject: [PATCH] use 'True if COND else False' for bool conversions faster than 'bool(COND)', especially on older Pythons < 3.11 --- gallery_dl/cookies.py | 19 ++++++++++--------- gallery_dl/extractor/cohost.py | 2 +- gallery_dl/extractor/deviantart.py | 2 +- gallery_dl/extractor/nozomi.py | 3 ++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/gallery_dl/cookies.py b/gallery_dl/cookies.py index 3fa9fa39..6e97f443 100644 --- a/gallery_dl/cookies.py +++ b/gallery_dl/cookies.py @@ -74,8 +74,9 @@ def load_cookies_firefox(profile=None, container=None, domain=None): cookies = [ Cookie( 0, name, value, None, False, - domain, bool(domain), domain.startswith("."), - path, bool(path), secure, expires, False, None, None, {}, + domain, True if domain else False, domain.startswith("."), + path, True if path else False, secure, expires, + False, None, None, {}, ) for name, value, domain, path, secure, expires in db.execute( sql, parameters) @@ -157,9 +158,9 @@ def load_cookies_chromium(browser_name, profile=None, cookies.append(Cookie( 0, name, value, None, False, - domain, bool(domain), domain.startswith("."), - path, bool(path), secure, expires or None, False, - None, None, {}, + domain, True if domain else False, domain.startswith("."), + path, True if path else False, secure, expires or None, + False, None, None, {}, )) if failed_cookies > 0: @@ -284,7 +285,7 @@ def _safari_parse_cookies_record(data, cookies, host=None): record_size = p.read_uint() p.skip(4, "unknown record field 1") flags = p.read_uint() - is_secure = bool(flags & 0x0001) + is_secure = True if (flags & 0x0001) else False p.skip(4, "unknown record field 2") domain_offset = p.read_uint() name_offset = p.read_uint() @@ -322,9 +323,9 @@ def _safari_parse_cookies_record(data, cookies, host=None): cookies.append(Cookie( 0, name, value, None, False, - domain, bool(domain), domain.startswith("."), - path, bool(path), is_secure, expiration_date, False, - None, None, {}, + domain, True if domain else False, domain.startswith("."), + path, True if path else False, is_secure, expiration_date, + False, None, None, {}, )) return record_size diff --git a/gallery_dl/extractor/cohost.py b/gallery_dl/extractor/cohost.py index 4722a4f1..0524239b 100644 --- a/gallery_dl/extractor/cohost.py +++ b/gallery_dl/extractor/cohost.py @@ -109,7 +109,7 @@ class CohostUserExtractor(CohostExtractor): "projectHandle": self.groups[0], "page": 0, "options": { - "pinnedPostsAtTop" : bool(self.pinned), + "pinnedPostsAtTop" : True if self.pinned else False, "hideReplies" : not self.replies, "hideShares" : not self.shares, "hideAsks" : not self.asks, diff --git a/gallery_dl/extractor/deviantart.py b/gallery_dl/extractor/deviantart.py index 836fae78..3b10a8b1 100644 --- a/gallery_dl/extractor/deviantart.py +++ b/gallery_dl/extractor/deviantart.py @@ -1301,7 +1301,7 @@ class DeviantartOAuthAPI(): metadata = extractor.config("metadata", False) if not metadata: - metadata = bool(extractor.extra) + metadata = True if extractor.extra else False if metadata: self.metadata = True diff --git a/gallery_dl/extractor/nozomi.py b/gallery_dl/extractor/nozomi.py index 8c7ffe52..851f663a 100644 --- a/gallery_dl/extractor/nozomi.py +++ b/gallery_dl/extractor/nozomi.py @@ -63,7 +63,8 @@ class NozomiExtractor(Extractor): yield Message.Directory, post for post["num"], image in enumerate(images, 1): post["filename"] = post["dataid"] = did = image["dataid"] - post["is_video"] = video = bool(image.get("is_video")) + post["is_video"] = video = \ + True if image.get("is_video") else False ext = image["type"] if video: