豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 6abe8fd

Browse files
committed
style: improve readability
1 parent 92d7487 commit 6abe8fd

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/polyfills/stdlib/functions.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,22 @@ def stable_key(x):
148148
return (_ReverseCompare(x[1]), x[0])
149149
return (x[1], x[0])
150150

151+
# For very old Python versions without key support
152+
def stable_cmp(a, b):
153+
result = (a[1] > b[1]) - (a[1] < b[1])
154+
155+
if reverse:
156+
result = -result
157+
158+
if result == 0:
159+
# If keys are equal, compare indices to maintain stability (never reverse index order)
160+
result = (a[0] > b[0]) - (a[0] < b[0])
161+
162+
return result
163+
151164
try:
152165
elements_with_index.sort(key=stable_key)
153166
except TypeError:
154-
# For very old Python versions without key support
155-
def stable_cmp(a, b):
156-
result = (a[1] > b[1]) - (a[1] < b[1])
157-
if reverse:
158-
result = -result
159-
if result == 0:
160-
# If keys are equal, compare indices to maintain stability (never reverse index order)
161-
result = (a[0] > b[0]) - (a[0] < b[0])
162-
return result
163167
elements_with_index.sort(stable_cmp)
164168
else:
165169
# The `key` argument was introduced starting from Python 2.4
@@ -169,21 +173,24 @@ def stable_key_wrapper(x):
169173
return (_ReverseCompare(key(x[1])), x[0])
170174
return (key(x[1]), x[0])
171175

176+
# Convert the key function to a `cmp` function for older Python versions (<3.0)
177+
# Info: https://docs.python.org/3/howto/sorting.html#the-old-way-using-the-cmp-parameter
178+
#
179+
# Include index comparison for stability
180+
def stable_key_to_cmp(a, b):
181+
ka, kb = key(a[1]), key(b[1])
182+
result = (ka > kb) - (ka < kb)
183+
if reverse:
184+
result = -result
185+
if result == 0:
186+
# If keys are equal, compare indices to maintain stability (never reverse index order)
187+
result = (a[0] > b[0]) - (a[0] < b[0])
188+
return result
189+
172190
try:
173191
elements_with_index.sort(key=stable_key_wrapper)
174192
except TypeError:
175-
# Convert the key function to a `cmp` function for older Python versions
176-
# Include index comparison for stability
177-
def stable_cmp(a, b):
178-
ka, kb = key(a[1]), key(b[1])
179-
result = (ka > kb) - (ka < kb)
180-
if reverse:
181-
result = -result
182-
if result == 0:
183-
# If keys are equal, compare indices to maintain stability (never reverse index order)
184-
result = (a[0] > b[0]) - (a[0] < b[0])
185-
return result
186-
elements_with_index.sort(stable_cmp)
193+
elements_with_index.sort(stable_key_to_cmp)
187194

188195
# Extract just the elements (without indices)
189196
elements = [elem for _, elem in elements_with_index]
@@ -407,8 +414,7 @@ def test_stability(self):
407414
for test_case in test_cases
408415
for test_method in dir(test_case)
409416
if test_method.startswith("test_")
410-
# and "showall" in test_method
411-
and test_case.__name__.lower().find("sorted") != -1
417+
# and test_case.__name__.lower().find("sorted") != -1
412418
]
413419

414420
suite = _unittest.TestSuite(tests)

0 commit comments

Comments
 (0)