diff options
author | Éric Araujo <merwok@netwok.org> | 2011-07-26 13:13:47 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-07-26 13:13:47 (GMT) |
commit | 41bade96a4d1dcd35bbba79c2eabe052d02756a4 (patch) | |
tree | 3cd8fbe018d2737b5bdff5d8b98fb6141867b427 /Lib/test/list_tests.py | |
parent | 45dedaafc2d8f83c3021df28251a4a7dae067774 (diff) | |
download | cpython-41bade96a4d1dcd35bbba79c2eabe052d02756a4.zip cpython-41bade96a4d1dcd35bbba79c2eabe052d02756a4.tar.gz cpython-41bade96a4d1dcd35bbba79c2eabe052d02756a4.tar.bz2 |
Remove duplicates of cmp_to_key (#12542, reviewed by Raymond Hettinger)
Diffstat (limited to 'Lib/test/list_tests.py')
-rw-r--r-- | Lib/test/list_tests.py | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index e3a7845..be054ea 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -4,17 +4,10 @@ Tests common to list and UserList.UserList import sys import os +from functools import cmp_to_key from test import support, seq_tests -def CmpToKey(mycmp): - 'Convert a cmp= function into a key= function' - class K(object): - def __init__(self, obj): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) == -1 - return K class CommonTest(seq_tests.CommonTest): @@ -443,7 +436,7 @@ class CommonTest(seq_tests.CommonTest): return 1 else: # a > b return -1 - u.sort(key=CmpToKey(revcmp)) + u.sort(key=cmp_to_key(revcmp)) self.assertEqual(u, self.type2test([2,1,0,-1,-2])) # The following dumps core in unpatched Python 1.5: @@ -456,7 +449,7 @@ class CommonTest(seq_tests.CommonTest): else: # xmod > ymod return 1 z = self.type2test(range(12)) - z.sort(key=CmpToKey(myComparison)) + z.sort(key=cmp_to_key(myComparison)) self.assertRaises(TypeError, z.sort, 2) @@ -468,7 +461,8 @@ class CommonTest(seq_tests.CommonTest): return -1 else: # x > y return 1 - self.assertRaises(ValueError, z.sort, key=CmpToKey(selfmodifyingComparison)) + self.assertRaises(ValueError, z.sort, + key=cmp_to_key(selfmodifyingComparison)) self.assertRaises(TypeError, z.sort, 42, 42, 42, 42) |