diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-04 21:45:01 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-04 21:45:01 (GMT) |
commit | bb006cf26cc41aefcddc8f06722c524826aacefa (patch) | |
tree | 5d0e82402052f188ea868cf94497eba52a7b38fd /Lib | |
parent | 4f185228b084ee45ef822198762154457dc343db (diff) | |
download | cpython-bb006cf26cc41aefcddc8f06722c524826aacefa.zip cpython-bb006cf26cc41aefcddc8f06722c524826aacefa.tar.gz cpython-bb006cf26cc41aefcddc8f06722c524826aacefa.tar.bz2 |
Add tests for cmp_to_key.
Adopt PEP 8 compliant function name.
Factor-out existing uses cmp_to_key.
Update documentation to use internal pointers instead of external resource.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/functools.py | 2 | ||||
-rw-r--r-- | Lib/pstats.py | 13 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 7 | ||||
-rw-r--r-- | Lib/unittest/loader.py | 12 |
4 files changed, 10 insertions, 24 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index d31b090..ad1cccc 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -76,7 +76,7 @@ def total_ordering(cls): setattr(cls, opname, opfunc) return cls -def CmpToKey(mycmp): +def cmp_to_key(mycmp): 'Convert a cmp= function into a key= function' class K(object): def __init__(self, obj, *args): diff --git a/Lib/pstats.py b/Lib/pstats.py index 0effa1c..8b60810 100644 --- a/Lib/pstats.py +++ b/Lib/pstats.py @@ -37,6 +37,7 @@ import os import time import marshal import re +from functools import cmp_to_key __all__ = ["Stats"] @@ -238,7 +239,7 @@ class Stats: stats_list.append((cc, nc, tt, ct) + func + (func_std_string(func), func)) - stats_list.sort(key=CmpToKey(TupleComp(sort_tuple).compare)) + stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare)) self.fcn_list = fcn_list = [] for tuple in stats_list: @@ -471,16 +472,6 @@ class TupleComp: return direction return 0 -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 - - #************************************************************************** # func_name is a triple (file:string, line:int, name:string) diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 2549e05..44992b8 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -338,7 +338,12 @@ class TestReduce(unittest.TestCase): self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item self.assertRaises(TypeError, reduce, 42, (42, 42)) - +class TestCmpToKey(unittest.TestCase): + def test_cmp_to_key(self): + def mycmp(x, y): + return y - x + self.assertEqual(sorted(range(5), key=functools.cmp_to_key(mycmp)), + [4, 3, 2, 1, 0]) def test_main(verbose=None): diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py index e0b8585..f0cc157 100644 --- a/Lib/unittest/loader.py +++ b/Lib/unittest/loader.py @@ -6,23 +6,13 @@ import sys import traceback import types +from functools import cmp_to_key as _CmpToKey from fnmatch import fnmatch from . import case, suite __unittest = True - -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 - - # what about .pyc or .pyo (etc) # we would need to avoid loading the same tests multiple times # from '.py', '.pyc' *and* '.pyo' |