[docs] [issue10225] Fix doctest runable examples in python manual

Alexander Belopolsky report at bugs.python.org
Wed Jan 12 05:32:05 CET 2011


Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:

On Tue, Jan 11, 2011 at 10:03 PM, Terry J. Reedy <report at bugs.python.org> wrote:
..
> What is the alternative?

I am attaching an alternative patch for sorting howto. You can run the
doctests in it as follows:

$ python2.7 tools/sphinx-build.py -b doctest -d build/doctrees .
build/doctest howto/sorting.rst
...

Doctest summary
===============
   34 tests
    0 failures in tests
    0 failures in setup code
build succeeded.

Unfortunately, Sphinx does not support Ramond's preferred style, but
my patch comes close.

----------
Added file: http://bugs.python.org/file20359/sorting-howto.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10225>
_______________________________________
-------------- next part --------------
Index: howto/sorting.rst
===================================================================
--- howto/sorting.rst	(revision 87942)
+++ howto/sorting.rst	(working copy)
@@ -58,30 +58,44 @@
 A common pattern is to sort complex objects using some of the object's indices
 as keys. For example:
 
-    >>> student_tuples = [
+.. testcode::
+
+    student_tuples = [
         ('john', 'A', 15),
         ('jane', 'B', 12),
         ('dave', 'B', 10),
     ]
-    >>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
+
+Sort by age:
+
+.. doctest::
+
+    >>> sorted(student_tuples, key=lambda student: student[2])
     [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
 
 The same technique works for objects with named attributes. For example:
 
-    >>> class Student:
-            def __init__(self, name, grade, age):
-                self.name = name
-                self.grade = grade
-                self.age = age
-            def __repr__(self):
-                return repr((self.name, self.grade, self.age))
+.. testcode::
 
-    >>> student_objects = [
+    class Student:
+        def __init__(self, name, grade, age):
+            self.name = name
+            self.grade = grade
+            self.age = age
+        def __repr__(self):
+            return repr((self.name, self.grade, self.age))
+
+    student_objects = [
         Student('john', 'A', 15),
         Student('jane', 'B', 12),
         Student('dave', 'B', 10),
     ]
-    >>> sorted(student_objects, key=lambda student: student.age)   # sort by age
+
+Sort by age:
+
+.. doctest::
+
+    >>> sorted(student_objects, key=lambda student: student.age)
     [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
 
 Operator Module Functions
@@ -210,22 +224,34 @@
 return a negative value for less-than, return zero if they are equal, or return
 a positive value for greater-than. For example, we can do:
 
-    >>> def numeric_compare(x, y):
-            return x - y
+.. testcode::
+
+    def numeric_compare(x, y):
+        return x - y
+
+.. doctest::
+
     >>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
     [1, 2, 3, 4, 5]
 
 Or you can reverse the order of comparison with:
 
-    >>> def reverse_numeric(x, y):
-            return y - x
+.. testcode::
+
+    def reverse_numeric(x, y):
+        return y - x
+
+.. doctest::
+
     >>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
     [5, 4, 3, 2, 1]
 
 When porting code from Python 2.x to 3.x, the situation can arise when you have
 the user supplying a comparison function and you need to convert that to a key
-function. The following wrapper makes that easy to do::
+function. The following wrapper makes that easy to do:
 
+.. testcode::
+
     def cmp_to_key(mycmp):
         'Convert a cmp= function into a key= function'
         class K(object):


More information about the docs mailing list