1 \section{\module{xreadlines
} ---
2 Efficient iteration over a file
}
4 \declaremodule{extension
}{xreadlines
}
5 \modulesynopsis{Efficient iteration over the lines of a file.
}
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.
19 import xreadlines, sys
21 for line in xreadlines.xreadlines(sys.stdin):
25 has approximately the same speed and memory consumption as
29 lines = sys.stdin.readlines(
8*
1024)
35 except the clarity of the
\keyword{for
} statement is retained in the
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.
44 An xreadlines object
\var{s
} supports the following sequence
47 \begin{tableii
}{c|l
}{code
}{Operation
}{Result
}
48 \lineii{\var{s
}[\var{i
}]}{\var{i
}'th line of
\var{s
}}
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
}.