Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 1 | \documentclass{howto} |
| 2 | \usepackage{distutils} |
| 3 | % $Id$ |
| 4 | |
| 5 | \title{What's New in Python 2.4} |
| 6 | \release{0.0} |
| 7 | \author{A.M.\ Kuchling} |
| 8 | \authoraddress{\email{amk@amk.ca}} |
| 9 | |
| 10 | \begin{document} |
| 11 | \maketitle |
| 12 | \tableofcontents |
| 13 | |
| 14 | This article explains the new features in Python 2.4. No release date |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 15 | for Python 2.4 has been set; expect that this will happen mid-2004. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 16 | |
| 17 | While Python 2.3 was primarily a library development release, Python |
| 18 | 2.4 may extend the core language and interpreter in |
| 19 | as-yet-undetermined ways. |
| 20 | |
| 21 | This article doesn't attempt to provide a complete specification of |
| 22 | the new features, but instead provides a convenient overview. For |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 23 | full details, you should refer to the documentation for Python 2.4, |
| 24 | such as the \citetitle[../lib/lib.html]{Python Library Reference} and |
| 25 | the \citetitle[../ref/ref.html]{Python Reference Manual}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 26 | If you want to understand the complete implementation and design |
| 27 | rationale, refer to the PEP for a particular new feature. |
| 28 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 29 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 30 | %====================================================================== |
| 31 | \section{PEP 218: Built-In Set Objects} |
| 32 | |
| 33 | Two new built-in types, \function{set(iterable)} and |
| 34 | \function{frozenset(iterable)} provide high speed data types for |
| 35 | membership testing, for eliminating duplicates from sequences, and |
| 36 | for mathematical operations like unions, intersections, differences, |
| 37 | and symmetric differences. |
| 38 | |
| 39 | \begin{verbatim} |
| 40 | >>> a = set('abracadabra') # form a set from a string |
| 41 | >>> 'z' in a # fast membership testing |
| 42 | False |
| 43 | >>> a # unique letters in a |
| 44 | set(['a', 'r', 'b', 'c', 'd']) |
| 45 | >>> ''.join(a) # convert back into a string |
| 46 | 'arbcd' |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 47 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 48 | >>> b = set('alacazam') # form a second set |
| 49 | >>> a - b # letters in a but not in b |
| 50 | set(['r', 'd', 'b']) |
| 51 | >>> a | b # letters in either a or b |
| 52 | set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) |
| 53 | >>> a & b # letters in both a and b |
| 54 | set(['a', 'c']) |
| 55 | >>> a ^ b # letters in a or b but not both |
| 56 | set(['r', 'd', 'b', 'm', 'z', 'l']) |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 58 | >>> a.add('z') # add a new element |
| 59 | >>> a.update('wxy') # add multiple new elements |
| 60 | >>> a |
| 61 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) |
| 62 | >>> a.remove('x') # take one element out |
| 63 | >>> a |
| 64 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) |
| 65 | \end{verbatim} |
| 66 | |
| 67 | The type \function{frozenset()} is an immutable version of \function{set()}. |
| 68 | Since it is immutable and hashable, it may be used as a dictionary key or |
| 69 | as a member of another set. Accordingly, it does not have methods |
| 70 | like \method{add()} and \method{remove()} which could alter its contents. |
| 71 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 72 | % XXX what happens to the sets module? |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 73 | % The current thinking is that the sets module will be left alone. |
| 74 | % That way, existing code will continue to run without alteration. |
| 75 | % Also, the module provides an autoconversion feature not supported by set() |
| 76 | % and frozenset(). |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 77 | |
Raymond Hettinger | 7e0282f | 2003-11-24 07:14:54 +0000 | [diff] [blame] | 78 | \begin{seealso} |
| 79 | \seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by |
| 80 | Greg Wilson and ultimately implemented by Raymond Hettinger.} |
| 81 | \end{seealso} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 82 | |
| 83 | %====================================================================== |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 84 | \section{PEP 237: Unifying Long Integers and Integers} |
| 85 | |
| 86 | XXX write this. |
| 87 | |
| 88 | %====================================================================== |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 89 | \section{PEP 322: Reverse Iteration} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 90 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 91 | A new built-in function, \function{reversed(seq)}, takes a sequence |
| 92 | and returns an iterator that returns the elements of the sequence |
| 93 | in reverse order. |
| 94 | |
| 95 | \begin{verbatim} |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 96 | >>> for i in reversed(xrange(1,4)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 97 | ... print i |
| 98 | ... |
| 99 | 3 |
| 100 | 2 |
| 101 | 1 |
| 102 | \end{verbatim} |
| 103 | |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 104 | Compared to extended slicing, \code{range(1,4)[::-1]}, \function{reversed()} |
| 105 | is easier to read, runs faster, and uses substantially less memory. |
| 106 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 107 | Note that \function{reversed()} only accepts sequences, not arbitrary |
Raymond Hettinger | bc3cba2 | 2003-11-12 16:39:30 +0000 | [diff] [blame] | 108 | iterators. If you want to reverse an iterator, first convert it to |
| 109 | a list with \function{list()}. |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 110 | |
| 111 | \begin{verbatim} |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 112 | >>> data = open('/etc/passwd', 'r') |
| 113 | >>> for line in reversed(list(data)): |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 114 | ... print line |
| 115 | ... |
| 116 | root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| 117 | ... |
| 118 | \end{verbatim} |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 119 | |
Andrew M. Kuchling | f7a6b67 | 2003-11-08 16:05:37 +0000 | [diff] [blame] | 120 | \begin{seealso} |
| 121 | \seepep{322}{Reverse Iteration}{Written and implemented by Raymond Hettinger.} |
| 122 | |
| 123 | \end{seealso} |
| 124 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 125 | |
| 126 | %====================================================================== |
| 127 | \section{Other Language Changes} |
| 128 | |
| 129 | Here are all of the changes that Python 2.4 makes to the core Python |
| 130 | language. |
| 131 | |
| 132 | \begin{itemize} |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 133 | |
| 134 | \item The string methods, \method{ljust()}, \method{rjust()}, and |
Andrew M. Kuchling | 6708756 | 2003-11-26 18:03:48 +0000 | [diff] [blame] | 135 | \method{center()} now take an optional argument for specifying a |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 136 | fill character other than a space. |
| 137 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 138 | \item Strings also gained an \method{rsplit()} method that |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 139 | works like the \method{split()} method but splits from the end of |
| 140 | the string. Possible applications include splitting a filename |
| 141 | from a path or a domain name from URL. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 142 | |
| 143 | \begin{verbatim} |
| 144 | >>> 'a b c'.split(None, 1) |
| 145 | ['a', 'b c'] |
| 146 | >>> 'a b c'.rsplit(None, 1) |
| 147 | ['a b', 'c'] |
| 148 | \end{verbatim} |
| 149 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 150 | \item The \method{sort()} method of lists gained three keyword |
| 151 | arguments, \var{cmp}, \var{key}, and \var{reverse}. These arguments |
| 152 | make some common usages of \method{sort()} simpler. All are optional. |
| 153 | |
| 154 | \var{cmp} is the same as the previous single argument to |
| 155 | \method{sort()}; if provided, the value should be a comparison |
| 156 | function that takes two arguments and returns -1, 0, or +1 depending |
| 157 | on how the arguments compare. |
| 158 | |
| 159 | \var{key} should be a single-argument function that takes a list |
| 160 | element and returns a comparison key for the element. The list is |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 161 | then sorted using the comparison keys. The following example sorts a |
| 162 | list case-insensitively: |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 163 | |
| 164 | \begin{verbatim} |
| 165 | >>> L = ['A', 'b', 'c', 'D'] |
| 166 | >>> L.sort() # Case-sensitive sort |
| 167 | >>> L |
| 168 | ['A', 'D', 'b', 'c'] |
| 169 | >>> L.sort(key=lambda x: x.lower()) |
| 170 | >>> L |
| 171 | ['A', 'b', 'c', 'D'] |
| 172 | >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower())) |
| 173 | >>> L |
| 174 | ['A', 'b', 'c', 'D'] |
| 175 | \end{verbatim} |
| 176 | |
| 177 | The last example, which uses the \var{cmp} parameter, is the old way |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 178 | to perform a case-insensitive sort. It works but is slower than |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 179 | using a \var{key} parameter. Using \var{key} results in calling the |
| 180 | \method{lower()} method once for each element in the list while using |
| 181 | \var{cmp} will call the method twice for each comparison. |
| 182 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 183 | For simple key functions and comparison functions, it is often |
| 184 | possible to avoid a \keyword{lambda} expression by using an unbound |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 185 | method instead. For example, the above case-insensitive sort is best |
| 186 | coded as: |
| 187 | |
| 188 | \begin{verbatim} |
| 189 | >>> L.sort(key=str.lower) |
| 190 | >>> L |
| 191 | ['A', 'b', 'c', 'D'] |
| 192 | \end{verbatim} |
| 193 | |
Andrew M. Kuchling | 2fb4d51 | 2003-10-21 12:31:16 +0000 | [diff] [blame] | 194 | The \var{reverse} parameter should have a Boolean value. If the value is |
| 195 | \constant{True}, the list will be sorted into reverse order. Instead |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 196 | of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write: |
| 197 | \code{L.sort(key = lambda x: x.score, reverse=True)}. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 198 | |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 199 | The results of sorting are now guaranteed to be stable. This means |
| 200 | that two entries with equal keys will be returned in the same order as |
| 201 | they were input. For example, you can sort a list of people by name, |
| 202 | and then sort the list by age, resulting in a list sorted by age where |
| 203 | people with the same age are in name-sorted order. |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 204 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 205 | \item There is a new built-in function \function{sorted(iterable)} that works |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 206 | like the in-place \method{list.sort()} method but has been made suitable |
| 207 | for use in expressions. The differences are: |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 208 | \begin{itemize} |
Raymond Hettinger | 7d1dd04 | 2003-11-12 16:42:10 +0000 | [diff] [blame] | 209 | \item the input may be any iterable; |
| 210 | \item a newly formed copy is sorted, leaving the original intact; and |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 211 | \item the expression returns the new sorted copy |
| 212 | \end{itemize} |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 213 | |
| 214 | \begin{verbatim} |
| 215 | >>> L = [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 216 | >>> [10+i for i in sorted(L)] # usable in a list comprehension |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 217 | [11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 218 | >>> L = [9,7,8,3,2,4,1,6,5] # original is left unchanged |
| 219 | [9,7,8,3,2,4,1,6,5] |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 220 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 221 | >>> sorted('Monte Python') # any iterable may be an input |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 222 | [' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y'] |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 223 | |
| 224 | >>> # List the contents of a dict sorted by key values |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 225 | >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 226 | >>> for k, v in sorted(colormap.iteritems()): |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 227 | ... print k, v |
| 228 | ... |
| 229 | black 4 |
| 230 | blue 2 |
| 231 | green 3 |
| 232 | red 1 |
| 233 | yellow 5 |
| 234 | |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 235 | \end{verbatim} |
| 236 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 237 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
Andrew M. Kuchling | 6708756 | 2003-11-26 18:03:48 +0000 | [diff] [blame] | 238 | now return an empty list instead of raising a \exception{TypeError} |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 239 | exception if called with no arguments. This makes the function more |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 240 | suitable for use with variable length argument lists: |
| 241 | |
| 242 | \begin{verbatim} |
| 243 | >>> def transpose(array): |
| 244 | ... return zip(*array) |
| 245 | ... |
| 246 | >>> transpose([(1,2,3), (4,5,6)]) |
| 247 | [(1, 4), (2, 5), (3, 6)] |
| 248 | >>> transpose([]) |
| 249 | [] |
| 250 | \end{verbatim} |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 251 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 252 | \end{itemize} |
| 253 | |
| 254 | |
| 255 | %====================================================================== |
| 256 | \subsection{Optimizations} |
| 257 | |
| 258 | \begin{itemize} |
| 259 | |
| 260 | \item Optimizations should be described here. |
| 261 | |
| 262 | \end{itemize} |
| 263 | |
| 264 | The net result of the 2.4 optimizations is that Python 2.4 runs the |
| 265 | pystone benchmark around XX\% faster than Python 2.3 and YY\% faster |
| 266 | than Python 2.2. |
| 267 | |
| 268 | |
| 269 | %====================================================================== |
| 270 | \section{New, Improved, and Deprecated Modules} |
| 271 | |
| 272 | As usual, Python's standard library received a number of enhancements and |
| 273 | bug fixes. Here's a partial list of the most notable changes, sorted |
| 274 | alphabetically by module name. Consult the |
| 275 | \file{Misc/NEWS} file in the source tree for a more |
| 276 | complete list of changes, or look through the CVS logs for all the |
| 277 | details. |
| 278 | |
| 279 | \begin{itemize} |
| 280 | |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 281 | \item The \module{curses} modules now supports the ncurses extension |
| 282 | \function{use_default_colors()}. On platforms where the terminal |
| 283 | supports transparency, this makes it possible to use a transparent background. |
| 284 | (Contributed by J\"org Lehmann.) |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 285 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 286 | \item The \module{heapq} module has been converted to C. The resulting |
| 287 | ten-fold improvement in speed makes the module suitable for handling |
| 288 | high volumes of data. |
Andrew M. Kuchling | 1a42025 | 2003-11-08 15:58:49 +0000 | [diff] [blame] | 289 | |
Andrew M. Kuchling | dff9dbd | 2003-11-20 22:22:19 +0000 | [diff] [blame] | 290 | \item The \module{imaplib} module now supports IMAP's THREAD command. |
| 291 | (Contributed by Yves Dionne.) |
| 292 | |
Andrew M. Kuchling | ad80955 | 2003-12-06 23:19:23 +0000 | [diff] [blame] | 293 | \item The \module{itertools} module gained a |
| 294 | \function{groupby(\var{iterable}\optional{, \var{func}})} function, |
| 295 | inspired by the GROUP BY clause from SQL. |
| 296 | \var{iterable} returns a succession of elements, and the optional |
| 297 | \var{func} is a function that takes an element and returns a key |
| 298 | value; if omitted, the key is simply the element itself. |
| 299 | \function{groupby()} then groups the elements into subsequences |
| 300 | which have matching values of the key, and returns a series of 2-tuples |
| 301 | containing the key value and an iterator over the subsequence. |
| 302 | |
| 303 | Here's an example. The \var{key} function simply returns whether a |
| 304 | number is even or odd, so the result of \function{groupby()} is to |
| 305 | return consecutive runs of odd or even numbers. |
| 306 | |
| 307 | \begin{verbatim} |
| 308 | >>> import itertools |
| 309 | >>> L = [2,4,6, 7,8,9,11, 12, 14] |
| 310 | >>> for key_val, it in itertools.groupby(L, lambda x: x % 2): |
| 311 | ... print key_val, list(it) |
| 312 | ... |
| 313 | 0 [2, 4, 6] |
| 314 | 1 [7] |
| 315 | 0 [8] |
| 316 | 1 [9, 11] |
| 317 | 0 [12, 14] |
| 318 | >>> |
| 319 | \end{verbatim} |
| 320 | |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 321 | Like its SQL counterpart, \function{groupby()} is typically used with |
| 322 | sorted input. The logic for \function{groupby()} is similar to the |
| 323 | \UNIX{} \code{uniq} filter which makes it handy for eliminating, |
| 324 | counting, or identifying duplicate elements: |
| 325 | |
| 326 | \begin{verbatim} |
| 327 | >>> word = 'abracadabra' |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 328 | >>> letters = sorted(word) # Turn string into a sorted list of letters |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 329 | >>> letters |
Andrew M. Kuchling | 4612bc5 | 2003-12-16 20:59:37 +0000 | [diff] [blame] | 330 | ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 331 | >>> [k for k, g in groupby(letters)] # List unique letters |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 332 | ['a', 'b', 'c', 'd', 'r'] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 333 | >>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 334 | [('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 335 | >>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters |
Raymond Hettinger | feb78c9 | 2003-12-12 13:13:47 +0000 | [diff] [blame] | 336 | ['a', 'b', 'r'] |
| 337 | \end{verbatim} |
| 338 | |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 339 | \item \module{itertools} also gained a function named |
| 340 | \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent |
| 341 | iterators that replicate \var{iterator}. If \var{N} is omitted, the |
| 342 | default is 2. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 343 | |
| 344 | \begin{verbatim} |
| 345 | >>> L = [1,2,3] |
| 346 | >>> i1, i2 = itertools.tee(L) |
| 347 | >>> i1,i2 |
| 348 | (<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 349 | >>> list(i1) # Run the first iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 350 | [1, 2, 3] |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 351 | >>> list(i2) # Run the second iterator to exhaustion |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 352 | [1, 2, 3] |
| 353 | >\end{verbatim} |
| 354 | |
| 355 | Note that \function{tee()} has to keep copies of the values returned |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 356 | by the iterator; in the worst case, it may need to keep all of them. |
| 357 | This should therefore be used carefully if there the leading iterator |
| 358 | can run far ahead of the trailing iterator in a long stream of inputs. |
| 359 | If the separation is large, then it becomes preferrable to use |
| 360 | \function{list()} instead. When the iterators track closely with one |
| 361 | another, \function{tee()} is ideal. Possible applications include |
| 362 | bookmarking, windowing, or lookahead iterators. |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 363 | |
Andrew M. Kuchling | dff9dbd | 2003-11-20 22:22:19 +0000 | [diff] [blame] | 364 | \item A new \function{getsid()} function was added to the |
| 365 | \module{posix} module that underlies the \module{os} module. |
| 366 | (Contributed by J. Raynor.) |
| 367 | |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 368 | \item The \module{operator} module gained two new functions, |
| 369 | \function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}. |
| 370 | Both functions return callables that take a single argument and return |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 371 | the corresponding attribute or item; these callables make excellent |
| 372 | data extractors when used with \function{map()} or \function{sorted()}. |
| 373 | For example: |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 374 | |
| 375 | \begin{verbatim} |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 376 | >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 377 | >>> map(operator.itemgetter(0), L) |
| 378 | ['c', 'd', 'a', 'b'] |
| 379 | >>> map(operator.itemgetter(1), L) |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 380 | [2, 1, 4, 3] |
| 381 | >>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item |
| 382 | [('d', 1), ('c', 2), ('b', 3), ('a', 4)] |
Andrew M. Kuchling | 35f2b05 | 2003-12-18 13:28:13 +0000 | [diff] [blame] | 383 | \end{verbatim} |
| 384 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 385 | \item The \module{random} module has a new method called \method{getrandbits(N)} |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 386 | which returns an N-bit long integer. This method supports the existing |
| 387 | \method{randrange()} method, making it possible to efficiently generate |
| 388 | arbitrarily large random numbers (suitable for prime number generation in |
Raymond Hettinger | ed54d91 | 2003-12-31 01:59:18 +0000 | [diff] [blame] | 389 | RSA applications for example). |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 390 | |
| 391 | \item The regular expression language accepted by the \module{re} module |
| 392 | was extended with simple conditional expressions, written as |
| 393 | \code{(?(\var{group})\var{A}|\var{B})}. \var{group} is either a |
| 394 | numeric group ID or a group name defined with \code{(?P<group>...)} |
| 395 | earlier in the expression. If the specified group matched, the |
| 396 | regular expression pattern \var{A} will be tested against the string; if |
| 397 | the group didn't match, the pattern \var{B} will be used instead. |
Andrew M. Kuchling | 69f31eb | 2003-08-13 23:11:04 +0000 | [diff] [blame] | 398 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 399 | \end{itemize} |
| 400 | |
| 401 | |
| 402 | %====================================================================== |
| 403 | % whole new modules get described in \subsections here |
| 404 | |
| 405 | |
| 406 | % ====================================================================== |
| 407 | \section{Build and C API Changes} |
| 408 | |
| 409 | Changes to Python's build process and to the C API include: |
| 410 | |
| 411 | \begin{itemize} |
| 412 | |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 413 | \item Three new convenience macros were added for common return |
| 414 | values from extension functions: \csimplemacro{Py_RETURN_NONE}, |
| 415 | \csimplemacro{Py_RETURN_TRUE}, and \csimplemacro{Py_RETURN_FALSE}. |
| 416 | |
| 417 | \item A new function, \cfunction{PyTuple_Pack(N, obj1, obj2, ..., |
| 418 | objN)}, constructs tuples from a variable length argument list of |
| 419 | Python objects. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 420 | |
Andrew M. Kuchling | 2ce1d47 | 2003-11-26 18:05:26 +0000 | [diff] [blame] | 421 | \item A new function, \cfunction{PyDict_Contains(d, k)}, implements |
| 422 | fast dictionary lookups without masking exceptions raised during the |
| 423 | look-up process. |
Raymond Hettinger | d446230 | 2003-11-26 17:52:45 +0000 | [diff] [blame] | 424 | |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 425 | \end{itemize} |
| 426 | |
| 427 | |
| 428 | %====================================================================== |
| 429 | \subsection{Port-Specific Changes} |
| 430 | |
| 431 | Platform-specific changes go here. |
| 432 | |
| 433 | |
| 434 | %====================================================================== |
| 435 | \section{Other Changes and Fixes \label{section-other}} |
| 436 | |
| 437 | As usual, there were a bunch of other improvements and bugfixes |
| 438 | scattered throughout the source tree. A search through the CVS change |
| 439 | logs finds there were XXX patches applied and YYY bugs fixed between |
| 440 | Python 2.3 and 2.4. Both figures are likely to be underestimates. |
| 441 | |
| 442 | Some of the more notable changes are: |
| 443 | |
| 444 | \begin{itemize} |
| 445 | |
| 446 | \item Details go here. |
| 447 | |
| 448 | \end{itemize} |
| 449 | |
| 450 | |
| 451 | %====================================================================== |
| 452 | \section{Porting to Python 2.4} |
| 453 | |
| 454 | This section lists previously described changes that may require |
| 455 | changes to your code: |
| 456 | |
| 457 | \begin{itemize} |
| 458 | |
Raymond Hettinger | 607c00f | 2003-11-12 16:27:50 +0000 | [diff] [blame] | 459 | \item The \function{zip()} built-in function and \function{itertools.izip()} |
| 460 | now return an empty list instead of raising a \exception{TypeError} |
| 461 | exception if called with no arguments. |
Andrew M. Kuchling | 6aedcfc | 2003-10-21 12:48:23 +0000 | [diff] [blame] | 462 | |
| 463 | \item \function{dircache.listdir()} now passes exceptions to the caller |
| 464 | instead of returning empty lists. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 465 | |
| 466 | \end{itemize} |
| 467 | |
| 468 | |
| 469 | %====================================================================== |
| 470 | \section{Acknowledgements \label{acks}} |
| 471 | |
| 472 | The author would like to thank the following people for offering |
| 473 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | 981a918 | 2003-11-13 21:33:26 +0000 | [diff] [blame] | 474 | article: Raymond Hettinger. |
Fred Drake | ed0fa3d | 2003-07-30 19:14:09 +0000 | [diff] [blame] | 475 | |
| 476 | \end{document} |