diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-30 02:55:10 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-30 02:55:10 (GMT) |
commit | d4cb56d4e88c7e001bbaba2c80953db47632f199 (patch) | |
tree | 73c95e0223ed8a98fac797fc99ab1bffae9c5457 /Lib | |
parent | fd66e51c4c1ff9293b0f332d6ebc8093b2ef12bb (diff) | |
download | cpython-d4cb56d4e88c7e001bbaba2c80953db47632f199.zip cpython-d4cb56d4e88c7e001bbaba2c80953db47632f199.tar.gz cpython-d4cb56d4e88c7e001bbaba2c80953db47632f199.tar.bz2 |
Convert some custom sort comparison functions to equivalent key functions.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/bsddb/dbtables.py | 11 | ||||
-rw-r--r-- | Lib/bsddb/test/test_compare.py | 12 | ||||
-rw-r--r-- | Lib/ctypes/util.py | 2 | ||||
-rw-r--r-- | Lib/idlelib/MultiCall.py | 2 | ||||
-rw-r--r-- | Lib/idlelib/TreeWidget.py | 2 | ||||
-rw-r--r-- | Lib/pyclbr.py | 3 | ||||
-rwxr-xr-x | Lib/pydoc.py | 5 | ||||
-rw-r--r-- | Lib/tarfile.py | 2 | ||||
-rw-r--r-- | Lib/unittest.py | 11 |
9 files changed, 37 insertions, 13 deletions
diff --git a/Lib/bsddb/dbtables.py b/Lib/bsddb/dbtables.py index 56b4e3a..563390b 100644 --- a/Lib/bsddb/dbtables.py +++ b/Lib/bsddb/dbtables.py @@ -88,6 +88,15 @@ class LikeCond(Cond): def __call__(self, s): return self.re.match(s.decode(self.encoding)) +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 + # # keys used to store database metadata # @@ -587,7 +596,7 @@ class bsdTableDB : return 0 conditionlist = list(conditions.items()) - conditionlist.sort(cmp_conditions) + conditionlist.sort(key=CmpToKey(cmp_conditions)) # Apply conditions to column data to find what we want cur = self.db.cursor() diff --git a/Lib/bsddb/test/test_compare.py b/Lib/bsddb/test/test_compare.py index a36faae..49aa7ca 100644 --- a/Lib/bsddb/test/test_compare.py +++ b/Lib/bsddb/test/test_compare.py @@ -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)) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 733a99c..3e6ae01 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -123,7 +123,7 @@ elif os.name == "posix": res = re.findall(expr, data) if not res: return _get_soname(_findLib_gcc(name)) - res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y))) + res.sort(key=_num_version) return res[-1] else: diff --git a/Lib/idlelib/MultiCall.py b/Lib/idlelib/MultiCall.py index 9e9e40c..c59ccfe 100644 --- a/Lib/idlelib/MultiCall.py +++ b/Lib/idlelib/MultiCall.py @@ -125,7 +125,7 @@ def expand_substates(states): statelist = [] for state in states: substates = list(set(state & x for x in states)) - substates.sort(lambda a,b: nbits(b) - nbits(a)) + substates.sort(key=nbits, reverse=True) statelist.append(substates) return statelist diff --git a/Lib/idlelib/TreeWidget.py b/Lib/idlelib/TreeWidget.py index c508410..0009de8 100644 --- a/Lib/idlelib/TreeWidget.py +++ b/Lib/idlelib/TreeWidget.py @@ -398,7 +398,7 @@ class FileTreeItem(TreeItem): names = os.listdir(self.path) except os.error: return [] - names.sort(lambda a, b: cmp(os.path.normcase(a), os.path.normcase(b))) + names.sort(key = os.path.normcase) sublist = [] for name in names: item = FileTreeItem(os.path.join(self.path, name)) diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py index c402a09..cf4951c 100644 --- a/Lib/pyclbr.py +++ b/Lib/pyclbr.py @@ -324,8 +324,7 @@ def _main(): path = [] dict = readmodule_ex(mod, path) objs = dict.values() - objs.sort(lambda a, b: cmp(getattr(a, 'lineno', 0), - getattr(b, 'lineno', 0))) + objs.sort(key=lambda a: getattr(a, 'lineno', 0)) for obj in objs: if isinstance(obj, Class): print("class", obj.name, obj.super, obj.lineno) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 0fdbb90..de8d193 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -797,10 +797,7 @@ class HTMLDoc(Doc): tag += ':<br>\n' # Sort attrs by name. - try: - attrs.sort(key=lambda t: t[0]) - except TypeError: - attrs.sort(lambda t1, t2: cmp(t1[0], t2[0])) # 2.3 compat + attrs.sort(key=lambda t: t[0]) # Pump out the attrs, segregated by kind. attrs = spill('Methods %s' % tag, attrs, diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 9ea92d0..b184ed8 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2016,7 +2016,7 @@ class TarFile(object): self.extract(tarinfo, path) # Reverse sort directories. - directories.sort(lambda a, b: cmp(a.name, b.name)) + directories.sort(key=lambda a: a.name) directories.reverse() # Set correct owner, mtime and filemode on directories. diff --git a/Lib/unittest.py b/Lib/unittest.py index adbbe8a..742871b 100644 --- a/Lib/unittest.py +++ b/Lib/unittest.py @@ -504,6 +504,15 @@ class FunctionTestCase(TestCase): # Locating and loading tests ############################################################################## +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 TestLoader: """This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite @@ -598,7 +607,7 @@ class TestLoader: and hasattr(getattr(testCaseClass, attrname), '__call__') testFnNames = list(filter(isTestMethod, dir(testCaseClass))) if self.sortTestMethodsUsing: - testFnNames.sort(self.sortTestMethodsUsing) + testFnNames.sort(key=CmpToKey(self.sortTestMethodsUsing)) return testFnNames |