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

Skip to content

Commit fdf2925

Browse files
committed
Issue #10225: Correct interactive Doc/howto/sorting.rst examples so doctest runs.
Based on part of A. Belopolsky's patch.
1 parent 524359f commit fdf2925

1 file changed

Lines changed: 39 additions & 40 deletions

File tree

Doc/howto/sorting.rst

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Sorting HOW TO
44
**************
55

66
:Author: Andrew Dalke and Raymond Hettinger
7-
:Release: 0.1
87

98

109
Python lists have a built-in :meth:`list.sort` method that modifies the list
@@ -18,7 +17,7 @@ Sorting Basics
1817
==============
1918

2019
A simple ascending sort is very easy: just call the :func:`sorted` function. It
21-
returns a new sorted list::
20+
returns a new sorted list:
2221

2322
>>> sorted([5, 2, 3, 1, 4])
2423
[1, 2, 3, 4, 5]
@@ -58,28 +57,28 @@ A common pattern is to sort complex objects using some of the object's indices
5857
as keys. For example:
5958

6059
>>> student_tuples = [
61-
('john', 'A', 15),
62-
('jane', 'B', 12),
63-
('dave', 'B', 10),
64-
]
60+
... ('john', 'A', 15),
61+
... ('jane', 'B', 12),
62+
... ('dave', 'B', 10),
63+
... ]
6564
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
6665
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
6766

6867
The same technique works for objects with named attributes. For example:
6968

7069
>>> class Student:
71-
def __init__(self, name, grade, age):
72-
self.name = name
73-
self.grade = grade
74-
self.age = age
75-
def __repr__(self):
76-
return repr((self.name, self.grade, self.age))
70+
... def __init__(self, name, grade, age):
71+
... self.name = name
72+
... self.grade = grade
73+
... self.age = age
74+
... def __repr__(self):
75+
... return repr((self.name, self.grade, self.age))
7776

7877
>>> student_objects = [
79-
Student('john', 'A', 15),
80-
Student('jane', 'B', 12),
81-
Student('dave', 'B', 10),
82-
]
78+
... Student('john', 'A', 15),
79+
... Student('jane', 'B', 12),
80+
... Student('dave', 'B', 10),
81+
... ]
8382
>>> sorted(student_objects, key=lambda student: student.age) # sort by age
8483
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
8584

@@ -208,39 +207,39 @@ return a negative value for less-than, return zero if they are equal, or return
208207
a positive value for greater-than. For example, we can do:
209208

210209
>>> def numeric_compare(x, y):
211-
return x - y
212-
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
210+
... return x - y
211+
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare) # doctest: +SKIP
213212
[1, 2, 3, 4, 5]
214213

215214
Or you can reverse the order of comparison with:
216215

217216
>>> def reverse_numeric(x, y):
218-
return y - x
219-
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
217+
... return y - x
218+
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric) # doctest: +SKIP
220219
[5, 4, 3, 2, 1]
221220

222221
When porting code from Python 2.x to 3.x, the situation can arise when you have
223222
the user supplying a comparison function and you need to convert that to a key
224-
function. The following wrapper makes that easy to do::
225-
226-
def cmp_to_key(mycmp):
227-
'Convert a cmp= function into a key= function'
228-
class K(object):
229-
def __init__(self, obj, *args):
230-
self.obj = obj
231-
def __lt__(self, other):
232-
return mycmp(self.obj, other.obj) < 0
233-
def __gt__(self, other):
234-
return mycmp(self.obj, other.obj) > 0
235-
def __eq__(self, other):
236-
return mycmp(self.obj, other.obj) == 0
237-
def __le__(self, other):
238-
return mycmp(self.obj, other.obj) <= 0
239-
def __ge__(self, other):
240-
return mycmp(self.obj, other.obj) >= 0
241-
def __ne__(self, other):
242-
return mycmp(self.obj, other.obj) != 0
243-
return K
223+
function. The following wrapper makes that easy to do:
224+
225+
>>> def cmp_to_key(mycmp):
226+
... 'Convert a cmp= function into a key= function'
227+
... class K(object):
228+
... def __init__(self, obj, *args):
229+
... self.obj = obj
230+
... def __lt__(self, other):
231+
... return mycmp(self.obj, other.obj) < 0
232+
... def __gt__(self, other):
233+
... return mycmp(self.obj, other.obj) > 0
234+
... def __eq__(self, other):
235+
... return mycmp(self.obj, other.obj) == 0
236+
... def __le__(self, other):
237+
... return mycmp(self.obj, other.obj) <= 0
238+
... def __ge__(self, other):
239+
... return mycmp(self.obj, other.obj) >= 0
240+
... def __ne__(self, other):
241+
... return mycmp(self.obj, other.obj) != 0
242+
... return K
244243

245244
To convert to a key function, just wrap the old comparison function:
246245

0 commit comments

Comments
 (0)