Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify multiple calls to str.startswith / str.endswith #1760

Merged
merged 2 commits into from
Jan 2, 2025

Conversation

QuLogic
Copy link
Contributor

@QuLogic QuLogic commented Aug 26, 2024

These methods have supported tuples to mean "any of the prefixes / suffixes" since Python 2.5, which can greatly simplify a bunch of or'd conditions.

@@ -361,9 +356,9 @@ def load_addon_file(path, callback=None):
return False
fptr.close()
# file_obj is either Zipfile or TarFile
if path.endswith(".zip") or path.endswith(".ZIP"):
if path.endswith((".zip", ".ZIP")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if path.endswith((".zip", ".ZIP")):
if path.lower().endswith((".zip")):

would this be ever clearer and handle mixed case (.Zip etc.) correctly as well?

or path.startswith("ftp://")
or path.startswith("file://")
):
if path.startswith(("http://", "https://", "ftp://", "file://")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if path.startswith(("http://", "https://", "ftp://", "file://")):
if path.lower().startswith(("http://", "https://", "ftp://", "file://")):

The protocol in a URI is case-insensitive. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1

file_obj = Zipfile(buffer)
elif path.endswith(".tar.gz") or path.endswith(".tgz"):
elif path.endswith((".tar.gz", ".tgz")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif path.endswith((".tar.gz", ".tgz")):
elif path.lower().endswith((".tar.gz", ".tgz")):

or url.endswith(".tar.gz")
or url.endswith(".tgz")
):
if url.endswith((".zip", ".ZIP", ".tar.gz", ".tgz")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if url.endswith((".zip", ".ZIP", ".tar.gz", ".tgz")):
if url.lower().endswith((".zip", ".ZIP", ".tar.gz", ".tgz")):

if not (
uri.startswith("http://") or uri.startswith("https://")
):
if not uri.startswith(("http://", "https://")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not uri.startswith(("http://", "https://")):
if not uri.lower().startswith(("http://", "https://")):

url = self.secure_mode
uri = url + "%(website)s" % {"website": uri}

# FTP server address
elif _type == UrlType.WEB_FTP:
if not (uri.startswith("ftp://") or uri.startswith("ftps://")):
if not uri.startswith(("ftp://", "ftps://")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not uri.startswith(("ftp://", "ftps://")):
if not uri.lower().startswith(("ftp://", "ftps://")):

@@ -280,8 +280,8 @@ def month_navigation(self, nr_up, year, currentsection):
url_fname = url_fname.lower()
url = url_fname
add_subdirs = False
if not (url.startswith("http:") or url.startswith("/")):
add_subdirs = not any(url.endswith(ext) for ext in _WEB_EXT)
if not url.startswith(("http:", "/")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not url.startswith(("http:", "/")):
if not url.lower().startswith(("http:", "/")):

@@ -708,7 +708,7 @@ def _has_webpage_extension(url):

@param: url -- filename to be checked
"""
return any(url.endswith(ext) for ext in _WEB_EXT)
return url.endswith(_WEB_EXT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return url.endswith(_WEB_EXT)
return url.lower().endswith(_WEB_EXT)

@@ -709,8 +709,8 @@ def month_navigation(self, nr_up, year, currentsection):
url_fname = url_fname.lower()
url = url_fname
add_subdirs = False
if not (url.startswith("http:") or url.startswith("/")):
add_subdirs = not any(url.endswith(ext) for ext in _WEB_EXT)
if not url.startswith(("http:", "/")):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not url.startswith(("http:", "/")):
if not url.lower().startswith(("http:", "/")):

@@ -2238,7 +2238,7 @@ def _has_webpage_extension(url):

url = filename to be checked
"""
return any(url.endswith(ext) for ext in _WEB_EXT)
return url.endswith(_WEB_EXT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return url.endswith(_WEB_EXT)
return url.lower().endswith(_WEB_EXT)

@stevenyoungs
Copy link
Contributor

@QuLogic My suggestions to improve handling of case go beyond your original fix. Let me know if you would prefer me to submit a separate PR with these changes.

These methods have supported tuples to mean "any of the prefixes /
suffixes" since Python 2.5, which can greatly simplify a bunch of `or`'d
conditions.
@Nick-Hall Nick-Hall force-pushed the startswith-endswith branch from fd8fc0e to c3c30f5 Compare January 2, 2025 18:00
@Nick-Hall Nick-Hall merged commit df6df78 into gramps-project:master Jan 2, 2025
2 checks passed
@QuLogic QuLogic deleted the startswith-endswith branch January 2, 2025 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants