Merged release21-maint changes.
[python/dscho.git] / Doc / lib / libxreadlines.tex
blobd824fc3e8c6310d724de295ab148889ca92f480e
1 \section{\module{xreadlines} ---
2 Efficient iteration over a file}
4 \declaremodule{extension}{xreadlines}
5 \modulesynopsis{Efficient iteration over the lines of a file.}
7 \versionadded{2.1}
10 This module defines a new object type which can efficiently iterate
11 over the lines of a file. An xreadlines object is a sequence type
12 which implements simple in-order indexing beginning at \code{0}, as
13 required by \keyword{for} statement or the
14 \function{filter()} function.
16 Thus, the code
18 \begin{verbatim}
19 import xreadlines, sys
21 for line in xreadlines.xreadlines(sys.stdin):
22 pass
23 \end{verbatim}
25 has approximately the same speed and memory consumption as
27 \begin{verbatim}
28 while 1:
29 lines = sys.stdin.readlines(8*1024)
30 if not lines: break
31 for line in lines:
32 pass
33 \end{verbatim}
35 except the clarity of the \keyword{for} statement is retained in the
36 former case.
38 \begin{funcdesc}{xreadlines}{fileobj}
39 Return a new xreadlines object which will iterate over the contents
40 of \var{fileobj}. \var{fileobj} must have a \method{readlines()}
41 method that supports the \var{sizehint} parameter.
42 \end{funcdesc}
44 An xreadlines object \var{s} supports the following sequence
45 operation:
47 \begin{tableii}{c|l}{code}{Operation}{Result}
48 \lineii{\var{s}[\var{i}]}{\var{i}'th line of \var{s}}
49 \end{tableii}
51 If successive values of \var{i} are not sequential starting from
52 \code{0}, this code will raise \exception{RuntimeError}.
54 After the last line of the file is read, this code will raise an
55 \exception{IndexError}.