Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Usestr.translate to improve performance ofnormalize#529

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hugovk wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromhugovk:speedup-normalize
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletionimportlib_metadata/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -945,7 +945,12 @@ def normalize(name):
"""
PEP 503 normalization plus dashes as underscores.
"""
return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
# Much faster than re.sub, and even faster than str.translate
value = name.lower().replace("-", "_").replace(".", "_")
# Condense repeats (faster than regex)
while "__" in value:
value = value.replace("__", "_")
return value

@staticmethod
def legacy_normalize(name):
Expand Down
34 changes: 34 additions & 0 deletionstests/test_api.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@
from importlib_metadata import (
Distribution,
PackageNotFoundError,
Prepared,
distribution,
entry_points,
files,
Expand DownExpand Up@@ -317,3 +318,36 @@ class InvalidateCache(unittest.TestCase):
def test_invalidate_cache(self):
# No externally observable behavior, but ensures test coverage...
importlib.invalidate_caches()


class PreparedTests(unittest.TestCase):
def test_normalize(self):
tests = [
# Simple
("sample", "sample"),
# Mixed case
("Sample", "sample"),
("SAMPLE", "sample"),
("SaMpLe", "sample"),
# Separator conversions
("sample-pkg", "sample_pkg"),
("sample.pkg", "sample_pkg"),
("sample_pkg", "sample_pkg"),
# Multiple separators
("sample---pkg", "sample_pkg"),
("sample___pkg", "sample_pkg"),
("sample...pkg", "sample_pkg"),
# Mixed separators
("sample-._pkg", "sample_pkg"),
("sample_.-pkg", "sample_pkg"),
# Complex
("Sample__Pkg-name.foo", "sample_pkg_name_foo"),
("Sample__Pkg.name__foo", "sample_pkg_name_foo"),
# Uppercase with separators
("SAMPLE-PKG", "sample_pkg"),
("Sample.Pkg", "sample_pkg"),
("SAMPLE_PKG", "sample_pkg"),
]
for name, expected in tests:
with self.subTest(name=name):
self.assertEqual(Prepared.normalize(name), expected)

[8]ページ先頭

©2009-2026 Movatter.jp