Skip to content

Commit 5833403

Browse files
committed
Use str.removeprefix()/removesuffix()
These functions are now available in all supported Python versions.
1 parent 121be0e commit 5833403

6 files changed

Lines changed: 10 additions & 19 deletions

File tree

nextstrain/cli/command/shell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..argparse import add_extended_help_flags
1111
from ..paths import SHELL_HISTORY
1212
from ..runner import docker, conda, singularity
13-
from ..util import colored, remove_prefix, runner_name
13+
from ..util import colored, runner_name
1414
from ..volume import NamedVolume
1515
from .build import assert_overlay_volumes_support, pathogen_volumes
1616

@@ -113,7 +113,7 @@ def fg(color: str) -> str: return r'\[\e[38;2;{};{};{}m\]'.format(*rgb(color))
113113
def bg(color: str) -> str: return r'\[\e[48;2;{};{};{}m\]'.format(*rgb(color))
114114

115115
def rgb(color: str) -> Tuple[int, int, int]:
116-
color = remove_prefix("#", color)
116+
color = color.removeprefix("#")
117117
r,g,b = (int(c, 16) for c in (color[0:2], color[2:4], color[4:6]))
118118
return r,g,b
119119

nextstrain/cli/command/view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
from ..argparse import add_extended_help_flags, SUPPRESS, SKIP_AUTO_DEFAULT_IN_HELP
6060
from ..browser import BROWSER, open_browser as __open_browser
6161
from ..runner import docker, ambient, conda, singularity
62-
from ..util import colored, remove_suffix, warn
62+
from ..util import colored, warn
6363
from ..volume import NamedVolume
6464

6565

@@ -324,7 +324,7 @@ def sidecar_file(path):
324324

325325
# v1: All *_tree.json files with corresponding *_meta.json files.
326326
def meta_exists(path):
327-
return path.with_name(remove_suffix("_tree.json", path.name) + "_meta.json").exists()
327+
return path.with_name(path.name.removesuffix("_tree.json") + "_meta.json").exists()
328328

329329
datasets_v1 = set(
330330
re.sub(r"_tree$", "", path.stem).replace("_", "/")

nextstrain/cli/remote/nextstrain_dot_org.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
from ..gzip import GzipCompressingReader
8787
from ..net import is_loopback
8888
from ..url import URL, Origin
89-
from ..util import remove_prefix, glob_matcher, glob_match
89+
from ..util import glob_matcher, glob_match
9090

9191

9292
# Default to embedding images, but allow it to be turned off as an escape
@@ -971,7 +971,7 @@ def authn_challenge(response: requests.Response) -> Optional[Dict[str, str]]:
971971
if not challenge or not challenge.startswith("Bearer "):
972972
return None
973973

974-
challenge_params = remove_prefix("Bearer ", challenge).lstrip(" ")
974+
challenge_params = challenge.removeprefix("Bearer ").lstrip(" ")
975975

976976
return requests.utils.parse_dict_header(challenge_params)
977977

nextstrain/cli/remote/s3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from ..aws.s3 import split_url
5252
from ..authn import User
5353
from ..gzip import GzipCompressingReader, ContentDecodingWriter
54-
from ..util import warn, remove_prefix
54+
from ..util import warn
5555
from ..errors import UserError
5656
from ..types import S3Object
5757
from ..url import URL, Origin
@@ -454,4 +454,4 @@ def remove_origin_path(origin: dict, prefix: str) -> str:
454454
"""
455455
Return the given prefix stripped of any implicit distribution origin path.
456456
"""
457-
return remove_prefix(origin["OriginPath"].lstrip("/"), prefix)
457+
return prefix.removeprefix(origin["OriginPath"].lstrip("/"))

nextstrain/cli/rst/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from docutils.parsers.rst.roles import register_local_role
2525
from docutils.utils import unescape
2626
from ..__version__ import __version__ as cli_version
27-
from ..util import remove_suffix
2827
from .sphinx import TextWriter
2928

3029

@@ -225,7 +224,7 @@ def doc_url(target: str) -> str:
225224

226225
if path.endswith("/index"):
227226
# a/b/index → a/b/ (e.g. implicitly a/b/index.html)
228-
path_url = remove_suffix("index", path)
227+
path_url = path.removesuffix("index")
229228
else:
230229
path_url = path + (suffix or "")
231230

nextstrain/cli/util.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ def colored(color, text):
6464
)
6565

6666

67-
# TODO: Use str.removeprefix/removesuffix once Python 3.9 is the minimum supported version.
68-
def remove_prefix(prefix, string):
69-
return re.sub('^' + re.escape(prefix), '', string)
70-
71-
def remove_suffix(suffix, string):
72-
return re.sub(re.escape(suffix) + '$', '', string)
73-
74-
7567
class NewVersionCheckResult(NamedTuple):
7668
newer_version: Optional[str]
7769
status_message: Optional[str]
@@ -724,7 +716,7 @@ def print_and_check_setup_tests(tests: SetupTestResults) -> bool:
724716
# Indent subsequent lines of any multi-line descriptions so it
725717
# lines up under the status marker.
726718
formatted_description = \
727-
remove_prefix(" ", indent(description, " "))
719+
indent(description, " ").removeprefix(" ")
728720

729721
results.append((description, result))
730722

0 commit comments

Comments
 (0)