Skip to content

Commit 41bade9

Browse files
committed
Remove duplicates of cmp_to_key (python#12542, reviewed by Raymond Hettinger)
1 parent 45dedaa commit 41bade9

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

Lib/test/list_tests.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44

55
import sys
66
import os
7+
from functools import cmp_to_key
78

89
from test import support, seq_tests
910

10-
def CmpToKey(mycmp):
11-
'Convert a cmp= function into a key= function'
12-
class K(object):
13-
def __init__(self, obj):
14-
self.obj = obj
15-
def __lt__(self, other):
16-
return mycmp(self.obj, other.obj) == -1
17-
return K
1811

1912
class CommonTest(seq_tests.CommonTest):
2013

@@ -443,7 +436,7 @@ def revcmp(a, b):
443436
return 1
444437
else: # a > b
445438
return -1
446-
u.sort(key=CmpToKey(revcmp))
439+
u.sort(key=cmp_to_key(revcmp))
447440
self.assertEqual(u, self.type2test([2,1,0,-1,-2]))
448441

449442
# The following dumps core in unpatched Python 1.5:
@@ -456,7 +449,7 @@ def myComparison(x,y):
456449
else: # xmod > ymod
457450
return 1
458451
z = self.type2test(range(12))
459-
z.sort(key=CmpToKey(myComparison))
452+
z.sort(key=cmp_to_key(myComparison))
460453

461454
self.assertRaises(TypeError, z.sort, 2)
462455

@@ -468,7 +461,8 @@ def selfmodifyingComparison(x,y):
468461
return -1
469462
else: # x > y
470463
return 1
471-
self.assertRaises(ValueError, z.sort, key=CmpToKey(selfmodifyingComparison))
464+
self.assertRaises(ValueError, z.sort,
465+
key=cmp_to_key(selfmodifyingComparison))
472466

473467
self.assertRaises(TypeError, z.sort, 42, 42, 42, 42)
474468

Lib/test/test_sort.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22
import random
33
import sys
44
import unittest
5+
from functools import cmp_to_key
56

67
verbose = support.verbose
78
nerrors = 0
89

9-
def CmpToKey(mycmp):
10-
'Convert a cmp= function into a key= function'
11-
class K(object):
12-
def __init__(self, obj):
13-
self.obj = obj
14-
def __lt__(self, other):
15-
return mycmp(self.obj, other.obj) == -1
16-
return K
1710

1811
def check(tag, expected, raw, compare=None):
1912
global nerrors
@@ -23,7 +16,7 @@ def check(tag, expected, raw, compare=None):
2316

2417
orig = raw[:] # save input in case of error
2518
if compare:
26-
raw.sort(key=CmpToKey(compare))
19+
raw.sort(key=cmp_to_key(compare))
2720
else:
2821
raw.sort()
2922

@@ -108,7 +101,7 @@ def __repr__(self):
108101
print(" Checking against an insane comparison function.")
109102
print(" If the implementation isn't careful, this may segfault.")
110103
s = x[:]
111-
s.sort(key=CmpToKey(lambda a, b: int(random.random() * 3) - 1))
104+
s.sort(key=cmp_to_key(lambda a, b: int(random.random() * 3) - 1))
112105
check("an insane function left some permutation", x, s)
113106

114107
if len(x) >= 2:
@@ -165,12 +158,12 @@ def mutating_cmp(x, y):
165158
L.pop()
166159
return (x > y) - (x < y)
167160
L = [1,2]
168-
self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp))
161+
self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp))
169162
def mutating_cmp(x, y):
170163
L.append(3)
171164
del L[:]
172165
return (x > y) - (x < y)
173-
self.assertRaises(ValueError, L.sort, key=CmpToKey(mutating_cmp))
166+
self.assertRaises(ValueError, L.sort, key=cmp_to_key(mutating_cmp))
174167
memorywaster = [memorywaster]
175168

176169
#==============================================================================
@@ -185,7 +178,7 @@ def test_decorated(self):
185178
def my_cmp(x, y):
186179
xlower, ylower = x.lower(), y.lower()
187180
return (xlower > ylower) - (xlower < ylower)
188-
copy.sort(key=CmpToKey(my_cmp))
181+
copy.sort(key=cmp_to_key(my_cmp))
189182

190183
def test_baddecorator(self):
191184
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
@@ -261,8 +254,8 @@ def my_cmp(x, y):
261254
def my_cmp_reversed(x, y):
262255
x0, y0 = x[0], y[0]
263256
return (y0 > x0) - (y0 < x0)
264-
data.sort(key=CmpToKey(my_cmp), reverse=True)
265-
copy1.sort(key=CmpToKey(my_cmp_reversed))
257+
data.sort(key=cmp_to_key(my_cmp), reverse=True)
258+
copy1.sort(key=cmp_to_key(my_cmp_reversed))
266259
self.assertEqual(data, copy1)
267260
copy2.sort(key=lambda x: x[0], reverse=True)
268261
self.assertEqual(data, copy2)

0 commit comments

Comments
 (0)