[Python-checkins] r79755 - in python/trunk: Doc/library/functions.rst Doc/library/functools.rst Doc/library/stdtypes.rst Lib/functools.py Lib/pstats.py Lib/test/test_functools.py Lib/unittest/loader.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Sun Apr 4 23:45:01 CEST 2010


Author: raymond.hettinger
Date: Sun Apr  4 23:45:01 2010
New Revision: 79755

Log:
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.



Modified:
   python/trunk/Doc/library/functions.rst
   python/trunk/Doc/library/functools.rst
   python/trunk/Doc/library/stdtypes.rst
   python/trunk/Lib/functools.py
   python/trunk/Lib/pstats.py
   python/trunk/Lib/test/test_functools.py
   python/trunk/Lib/unittest/loader.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Doc/library/functions.rst
==============================================================================
--- python/trunk/Doc/library/functions.rst	(original)
+++ python/trunk/Doc/library/functions.rst	Sun Apr  4 23:45:01 2010
@@ -1154,9 +1154,8 @@
    In general, the *key* and *reverse* conversion processes are much faster
    than specifying an equivalent *cmp* function.  This is because *cmp* is
    called multiple times for each list element while *key* and *reverse* touch
-   each element only once.  To convert an old-style *cmp* function to a *key*
-   function, see the `CmpToKey recipe in the ASPN cookbook
-   <http://code.activestate.com/recipes/576653/>`_\.
+   each element only once.  Use :func:`functools.cmp_to_key` to convert an
+   old-style *cmp* function to a *key* function.
 
    For sorting examples and a brief sorting tutorial, see `Sorting HowTo
    <http://wiki.python.org/moin/HowTo/Sorting/>`_\.

Modified: python/trunk/Doc/library/functools.rst
==============================================================================
--- python/trunk/Doc/library/functools.rst	(original)
+++ python/trunk/Doc/library/functools.rst	Sun Apr  4 23:45:01 2010
@@ -17,7 +17,7 @@
 
 The :mod:`functools` module defines the following functions:
 
-..  function:: CmpToKey(func)
+..  function:: cmp_to_key(func)
 
     Transform an old-style comparison function to a key-function.  Used with
     tools that accept key functions (such as :func:`sorted`, :func:`min`,
@@ -35,7 +35,7 @@
 
     Example::
 
-        sorted(iterable, key=CmpToKey(locale.strcoll))  # locale-aware sort order
+        sorted(iterable, key=cmp_to_key(locale.strcoll))  # locale-aware sort order
 
    .. versionadded:: 2.7
 

Modified: python/trunk/Doc/library/stdtypes.rst
==============================================================================
--- python/trunk/Doc/library/stdtypes.rst	(original)
+++ python/trunk/Doc/library/stdtypes.rst	Sun Apr  4 23:45:01 2010
@@ -1618,7 +1618,8 @@
    In general, the *key* and *reverse* conversion processes are much faster than
    specifying an equivalent *cmp* function.  This is because *cmp* is called
    multiple times for each list element while *key* and *reverse* touch each
-   element only once.
+   element only once.  Use :func:`functools.cmp_to_key` to convert an
+   old-style *cmp* function to a *key* function.
 
    .. versionchanged:: 2.3
       Support for ``None`` as an equivalent to omitting *cmp* was added.

Modified: python/trunk/Lib/functools.py
==============================================================================
--- python/trunk/Lib/functools.py	(original)
+++ python/trunk/Lib/functools.py	Sun Apr  4 23:45:01 2010
@@ -76,7 +76,7 @@
             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):

Modified: python/trunk/Lib/pstats.py
==============================================================================
--- python/trunk/Lib/pstats.py	(original)
+++ python/trunk/Lib/pstats.py	Sun Apr  4 23:45:01 2010
@@ -37,6 +37,7 @@
 import time
 import marshal
 import re
+from functools import cmp_to_key
 
 __all__ = ["Stats"]
 
@@ -238,7 +239,7 @@
             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 @@
                 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)
 

Modified: python/trunk/Lib/test/test_functools.py
==============================================================================
--- python/trunk/Lib/test/test_functools.py	(original)
+++ python/trunk/Lib/test/test_functools.py	Sun Apr  4 23:45:01 2010
@@ -338,7 +338,12 @@
         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):

Modified: python/trunk/Lib/unittest/loader.py
==============================================================================
--- python/trunk/Lib/unittest/loader.py	(original)
+++ python/trunk/Lib/unittest/loader.py	Sun Apr  4 23:45:01 2010
@@ -6,23 +6,13 @@
 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'

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Apr  4 23:45:01 2010
@@ -60,7 +60,7 @@
 - the functools module now has a total_ordering() class decorator
   to simplify the specifying rich comparisons.
 
-- The functools module also adds CmpToKey() as a tool to transition
+- The functools module also adds cmp_to_key() as a tool to transition
   old-style comparison functions to new-style key-functions.
 
 - Issue #8294: The Fraction constructor now accepts Decimal and float


More information about the Python-checkins mailing list