|
| 1 | +\section{\module{bz2} --- |
| 2 | + Compression compatible with \program{bzip2}} |
| 3 | + |
| 4 | +\declaremodule{builtin}{bz2} |
| 5 | +\modulesynopsis{Interface to compression and decompression |
| 6 | + routines compatible with \program{bzip2}.} |
| 7 | +\moduleauthor{Gustavo Niemeyer}{ [email protected]} |
| 8 | +\sectionauthor{Gustavo Niemeyer}{ [email protected]} |
| 9 | + |
| 10 | +\versionadded{2.3} |
| 11 | + |
| 12 | +This module provides a comprehensive interface for the bz2 compression library. |
| 13 | +It implements a complete file interface, one-shot (de)compression functions, |
| 14 | +and types for sequential (de)compression. |
| 15 | + |
| 16 | +Here is a resume of the features offered by the bz2 module: |
| 17 | + |
| 18 | +\begin{itemize} |
| 19 | +\item \class{BZ2File} class implements a complete file interface, including |
| 20 | + \method{readline()}, \method{readlines()}, \method{xreadlines()}, |
| 21 | + \method{writelines()}, \method{seek()}, etc; |
| 22 | +\item \class{BZ2File} class implements emulated \method{seek()} support; |
| 23 | +\item \class{BZ2File} class implements universal newline support; |
| 24 | +\item \class{BZ2File} class offers an optimized line iteration using |
| 25 | + the readahead algorithm borrowed from file objects; |
| 26 | +\item \class{BZ2File} class developed inheriting builtin file type |
| 27 | + (\code{isinstance(BZ2File(), file) == 1}); |
| 28 | +\item Sequential (de)compression supported by \class{BZ2Compressor} and |
| 29 | + \class{BZ2Decompressor} classes; |
| 30 | +\item One-shot (de)compression supported by \function{compress()} and |
| 31 | + \function{decompress()} functions; |
| 32 | +\item Thread safety uses individual locking mechanism; |
| 33 | +\item Complete inline documentation; |
| 34 | +\end{itemize} |
| 35 | + |
| 36 | + |
| 37 | +\subsection{(De)compression of files} |
| 38 | + |
| 39 | +Handling of compressed files is offered by the \class{BZ2File} class. |
| 40 | + |
| 41 | +\begin{classdesc}{BZ2File}{filename \optional{, mode='r'\optional{, |
| 42 | + buffering=0\optional{, compresslevel=9}}}} |
| 43 | +Open a bz2 file. Mode can be either \code{'r'} or \code{'w'}, for reading |
| 44 | +(default) or writing. When opened for writing, the file will be created if |
| 45 | +it doesn't exist, and truncated otherwise. If the buffering argument is given, |
| 46 | +\code{0} means unbuffered, and larger numbers specify the buffer size. If |
| 47 | +compresslevel is given, must be a number between \code{1} and \code{9}. |
| 48 | +Add a \code{'U'} to mode to open the file for input with universal newline |
| 49 | +support. Any line ending in the input file will be seen as a |
| 50 | +\code{'\textbackslash n'} |
| 51 | +in Python. Also, a file so opened gains the attribute \member{newlines}; |
| 52 | +the value for this attribute is one of \code{None} (no newline read yet), |
| 53 | +\code{'\textbackslash r'}, \code{'\textbackslash n'}, |
| 54 | +\code{'\textbackslash r\textbackslash n'} or a tuple containing all the |
| 55 | +newline types seen. Universal newlines are available only when reading. |
| 56 | +\end{classdesc} |
| 57 | + |
| 58 | +\begin{methoddesc}[BZ2File]{close}{} |
| 59 | +Close the file. Sets data attribute \member{closed} to true. A closed file |
| 60 | +cannot be used for further I/O operations. \method{close()} may be called |
| 61 | +more than once without error. |
| 62 | +\end{methoddesc} |
| 63 | + |
| 64 | +\begin{methoddesc}[BZ2File]{read}{\optional{size}} |
| 65 | +Read at most \var{size} uncompressed bytes, returned as a string. If the |
| 66 | +\var{size} argument is negative or omitted, read until EOF is reached. |
| 67 | +\end{methoddesc} |
| 68 | + |
| 69 | +\begin{methoddesc}[BZ2File]{readline}{\optional{size}} |
| 70 | +Return the next line from the file, as a string, retaining newline. |
| 71 | +A non-negative \var{size} argument limits the maximum number of bytes to |
| 72 | +return (an incomplete line may be returned then). Return an empty |
| 73 | +string at EOF. |
| 74 | +\end{methoddesc} |
| 75 | + |
| 76 | +\begin{methoddesc}[BZ2File]{readlines}{\optional{size}} |
| 77 | +Return a list of lines read. The optional \var{size} argument, if given, |
| 78 | +is an approximate bound on the total number of bytes in the lines returned. |
| 79 | +\end{methoddesc} |
| 80 | + |
| 81 | +\begin{methoddesc}[BZ2File]{xreadlines}{} |
| 82 | +For backward compatibility. \class{BZ2File} objects now include the |
| 83 | +performance optimizations previously implemented in the \module{xreadlines} |
| 84 | +module. |
| 85 | +\end{methoddesc} |
| 86 | + |
| 87 | +\begin{methoddesc}[BZ2File]{\_\_iter\_\_}{} |
| 88 | +Iterate trough the file lines. Iteration optimization is implemented |
| 89 | +using the same readahead algorithm available in \class{file} objects. |
| 90 | +\end{methoddesc} |
| 91 | + |
| 92 | +\begin{methoddesc}[BZ2File]{seek}{offset \optional{, whence}} |
| 93 | +Move to new file position. Argument \var{offset} is a byte count. Optional |
| 94 | +argument \var{whence} defaults to \code{0} (offset from start of file, |
| 95 | +offset should be \code{>= 0}); other values are \code{1} (move relative to |
| 96 | +current position, positive or negative), and \code{2} (move relative to end |
| 97 | +of file, usually negative, although many platforms allow seeking beyond |
| 98 | +the end of a file). |
| 99 | + |
| 100 | +Note that seeking of bz2 files is emulated, and depending on the parameters |
| 101 | +the operation may be extremely slow. |
| 102 | +\end{methoddesc} |
| 103 | + |
| 104 | +\begin{methoddesc}[BZ2File]{tell}{} |
| 105 | +Return the current file position, an integer (may be a long integer). |
| 106 | +\end{methoddesc} |
| 107 | + |
| 108 | +\begin{methoddesc}[BZ2File]{write}{data} |
| 109 | +Write string \var{data} to file. Note that due to buffering, \method{close()} |
| 110 | +may be needed before the file on disk reflects the data written. |
| 111 | +\end{methoddesc} |
| 112 | + |
| 113 | +\begin{methoddesc}[BZ2File]{writelines}{sequence_of_strings} |
| 114 | +Write the sequence of strings to the file. Note that newlines are not added. |
| 115 | +The sequence can be any iterable object producing strings. This is equivalent |
| 116 | +to calling write() for each string. |
| 117 | +\end{methoddesc} |
| 118 | + |
| 119 | + |
| 120 | +\subsection{Sequential (de)compression} |
| 121 | + |
| 122 | +Sequential compression and decompression is done using the classes |
| 123 | +\class{BZ2Compressor} and \class{BZ2Decompressor}. |
| 124 | + |
| 125 | +\begin{classdesc}{BZ2Compressor}{\optional{compresslevel=9}} |
| 126 | +Create a new compressor object. This object may be used to compress |
| 127 | +data sequentially. If you want to compress data in one shot, use the |
| 128 | +\function{compress()} function instead. The \var{compresslevel} parameter, |
| 129 | +if given, must be a number between \code{1} and \code{9}. |
| 130 | +\end{classdesc} |
| 131 | + |
| 132 | +\begin{methoddesc}[BZ2Compressor]{compress}{data} |
| 133 | +Provide more data to the compressor object. It will return chunks of compressed |
| 134 | +data whenever possible. When you've finished providing data to compress, call |
| 135 | +the \method{flush()} method to finish the compression process, and return what |
| 136 | +is left in internal buffers. |
| 137 | +\end{methoddesc} |
| 138 | + |
| 139 | +\begin{methoddesc}[BZ2Compressor]{flush}{} |
| 140 | +Finish the compression process and return what is left in internal buffers. You |
| 141 | +must not use the compressor object after calling this method. |
| 142 | +\end{methoddesc} |
| 143 | + |
| 144 | +\begin{classdesc}{BZ2Decompressor}{} |
| 145 | +Create a new decompressor object. This object may be used to decompress |
| 146 | +data sequentially. If you want to decompress data in one shot, use the |
| 147 | +\function{decompress()} function instead. |
| 148 | +\end{classdesc} |
| 149 | + |
| 150 | +\begin{methoddesc}[BZ2Decompressor]{decompress}{data} |
| 151 | +Provide more data to the decompressor object. It will return chunks of |
| 152 | +decompressed data whenever possible. If you try to decompress data after the |
| 153 | +end of stream is found, \exception{EOFError} will be raised. If any data was |
| 154 | +found after the end of stream, it'll be ignored and saved in |
| 155 | +\member{unused\_data} attribute. |
| 156 | +\end{methoddesc} |
| 157 | + |
| 158 | + |
| 159 | +\subsection{One-shot (de)compression} |
| 160 | + |
| 161 | +One-shot compression and decompression is provided trough the |
| 162 | +\function{compress()} and \function{decompress()} functions. |
| 163 | + |
| 164 | +\begin{funcdesc}{compress}{data\optional{, compresslevel=9}} |
| 165 | +Compress \var{data} in one shot. If you want to compress data sequentially, |
| 166 | +use an instance of \class{BZ2Compressor} instead. The \var{compresslevel} |
| 167 | +parameter, if given, must be a number between \code{1} and \code{9}. |
| 168 | +\end{funcdesc} |
| 169 | + |
| 170 | +\begin{funcdesc}{decompress}{} |
| 171 | +Decompress \var{data} in one shot. If you want to decompress data |
| 172 | +sequentially, use an instance of \class{BZ2Decompressor} instead. |
| 173 | +\end{funcdesc} |
| 174 | + |
0 commit comments