Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4cb56d

Browse files
committed
Convert some custom sort comparison functions to equivalent key functions.
1 parent fd66e51 commit d4cb56d

12 files changed

Lines changed: 50 additions & 20 deletions

File tree

Lib/bsddb/dbtables.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ def __init__(self, likestr, re_flags=re.IGNORECASE, encoding="utf-8"):
8888
def __call__(self, s):
8989
return self.re.match(s.decode(self.encoding))
9090

91+
def CmpToKey(mycmp):
92+
'Convert a cmp= function into a key= function'
93+
class K(object):
94+
def __init__(self, obj, *args):
95+
self.obj = obj
96+
def __lt__(self, other):
97+
return mycmp(self.obj, other.obj) == -1
98+
return K
99+
91100
#
92101
# keys used to store database metadata
93102
#
@@ -587,7 +596,7 @@ def cmp_conditions(atuple, btuple):
587596
return 0
588597

589598
conditionlist = list(conditions.items())
590-
conditionlist.sort(cmp_conditions)
599+
conditionlist.sort(key=CmpToKey(cmp_conditions))
591600

592601
# Apply conditions to column data to find what we want
593602
cur = self.db.cursor()

Lib/bsddb/test/test_compare.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,20 @@ def reverse (left, right, delegate=cmp):
3232
_expected_lowercase_test_data = [s.encode('ascii') for s in
3333
('', 'a', 'aaa', 'b', 'c', 'CC', 'cccce', 'ccccf', 'CCCP')]
3434

35+
36+
def CmpToKey(mycmp):
37+
'Convert a cmp= function into a key= function'
38+
class K(object):
39+
def __init__(self, obj, *args):
40+
self.obj = obj
41+
def __lt__(self, other):
42+
return mycmp(self.obj, other.obj) == -1
43+
return K
44+
3545
class ComparatorTests (unittest.TestCase):
3646
def comparator_test_helper (self, comparator, expected_data):
3747
data = expected_data[:]
38-
data.sort (comparator)
48+
data.sort (key=CmpToKey(comparator))
3949
self.failUnless (data == expected_data,
4050
"comparator `%s' is not right: %s vs. %s"
4151
% (comparator, expected_data, data))

Lib/ctypes/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def find_library(name):
123123
res = re.findall(expr, data)
124124
if not res:
125125
return _get_soname(_findLib_gcc(name))
126-
res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y)))
126+
res.sort(key=_num_version)
127127
return res[-1]
128128

129129
else:

Lib/idlelib/MultiCall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def nbits(n):
125125
statelist = []
126126
for state in states:
127127
substates = list(set(state & x for x in states))
128-
substates.sort(lambda a,b: nbits(b) - nbits(a))
128+
substates.sort(key=nbits, reverse=True)
129129
statelist.append(substates)
130130
return statelist
131131

Lib/idlelib/TreeWidget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def GetSubList(self):
398398
names = os.listdir(self.path)
399399
except os.error:
400400
return []
401-
names.sort(lambda a, b: cmp(os.path.normcase(a), os.path.normcase(b)))
401+
names.sort(key = os.path.normcase)
402402
sublist = []
403403
for name in names:
404404
item = FileTreeItem(os.path.join(self.path, name))

Lib/pyclbr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ def _main():
324324
path = []
325325
dict = readmodule_ex(mod, path)
326326
objs = dict.values()
327-
objs.sort(lambda a, b: cmp(getattr(a, 'lineno', 0),
328-
getattr(b, 'lineno', 0)))
327+
objs.sort(key=lambda a: getattr(a, 'lineno', 0))
329328
for obj in objs:
330329
if isinstance(obj, Class):
331330
print("class", obj.name, obj.super, obj.lineno)

Lib/pydoc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,10 +797,7 @@ def spilldata(msg, attrs, predicate):
797797
tag += ':<br>\n'
798798

799799
# Sort attrs by name.
800-
try:
801-
attrs.sort(key=lambda t: t[0])
802-
except TypeError:
803-
attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) # 2.3 compat
800+
attrs.sort(key=lambda t: t[0])
804801

805802
# Pump out the attrs, segregated by kind.
806803
attrs = spill('Methods %s' % tag, attrs,

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ def extractall(self, path=".", members=None):
20162016
self.extract(tarinfo, path)
20172017

20182018
# Reverse sort directories.
2019-
directories.sort(lambda a, b: cmp(a.name, b.name))
2019+
directories.sort(key=lambda a: a.name)
20202020
directories.reverse()
20212021

20222022
# Set correct owner, mtime and filemode on directories.

Lib/unittest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,15 @@ def shortDescription(self):
504504
# Locating and loading tests
505505
##############################################################################
506506

507+
def CmpToKey(mycmp):
508+
'Convert a cmp= function into a key= function'
509+
class K(object):
510+
def __init__(self, obj, *args):
511+
self.obj = obj
512+
def __lt__(self, other):
513+
return mycmp(self.obj, other.obj) == -1
514+
return K
515+
507516
class TestLoader:
508517
"""This class is responsible for loading tests according to various
509518
criteria and returning them wrapped in a TestSuite
@@ -598,7 +607,7 @@ def isTestMethod(attrname, testCaseClass=testCaseClass,
598607
and hasattr(getattr(testCaseClass, attrname), '__call__')
599608
testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
600609
if self.sortTestMethodsUsing:
601-
testFnNames.sort(self.sortTestMethodsUsing)
610+
testFnNames.sort(key=CmpToKey(self.sortTestMethodsUsing))
602611
return testFnNames
603612

604613

Tools/pynche/ColorDB.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ def unique_names(self):
122122
self.__allnames = []
123123
for name, aliases in self.__byrgb.values():
124124
self.__allnames.append(name)
125-
# sort irregardless of case
126-
def nocase_cmp(n1, n2):
127-
return cmp(n1.lower(), n2.lower())
128-
self.__allnames.sort(nocase_cmp)
125+
self.__allnames.sort(key=unicode.lower)
129126
return self.__allnames
130127

131128
def aliases_of(self, red, green, blue):

0 commit comments

Comments
 (0)