mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Convert some custom sort comparison functions to equivalent key functions.
This commit is contained in:
parent
fd66e51c4c
commit
d4cb56d4e8
12 changed files with 50 additions and 20 deletions
|
@ -32,10 +32,20 @@ _expected_lexical_test_data = [s.encode('ascii') for s in
|
|||
_expected_lowercase_test_data = [s.encode('ascii') for s in
|
||||
('', 'a', 'aaa', 'b', 'c', 'CC', 'cccce', 'ccccf', 'CCCP')]
|
||||
|
||||
|
||||
def CmpToKey(mycmp):
|
||||
'Convert a cmp= function into a key= function'
|
||||
class K(object):
|
||||
def __init__(self, obj, *args):
|
||||
self.obj = obj
|
||||
def __lt__(self, other):
|
||||
return mycmp(self.obj, other.obj) == -1
|
||||
return K
|
||||
|
||||
class ComparatorTests (unittest.TestCase):
|
||||
def comparator_test_helper (self, comparator, expected_data):
|
||||
data = expected_data[:]
|
||||
data.sort (comparator)
|
||||
data.sort (key=CmpToKey(comparator))
|
||||
self.failUnless (data == expected_data,
|
||||
"comparator `%s' is not right: %s vs. %s"
|
||||
% (comparator, expected_data, data))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue