@@ -83,7 +83,7 @@ All of the classes in this module may safely be accessed from multiple threads.
8383
8484 The *buffering * argument is ignored. Its use is deprecated since Python 3.0.
8585
86- If *mode * is ``'w' `` or ``'a' ``, *compresslevel * can be a number between
86+ If *mode * is ``'w' `` or ``'a' ``, *compresslevel * can be an integer between
8787 ``1 `` and ``9 `` specifying the level of compression: ``1 `` produces the
8888 least compression, and ``9 `` (default) produces the most compression.
8989
@@ -148,7 +148,7 @@ Incremental (de)compression
148148 incrementally. For one-shot compression, use the :func: `compress ` function
149149 instead.
150150
151- *compresslevel *, if given, must be a number between ``1 `` and ``9 ``. The
151+ *compresslevel *, if given, must be an integer between ``1 `` and ``9 ``. The
152152 default is ``9 ``.
153153
154154 .. method :: compress(data)
@@ -234,17 +234,17 @@ One-shot (de)compression
234234
235235.. function :: compress(data, compresslevel=9)
236236
237- Compress *data *.
237+ Compress *data *, a :term: ` bytes-like object <bytes-like object> ` .
238238
239- *compresslevel *, if given, must be a number between ``1 `` and ``9 ``. The
239+ *compresslevel *, if given, must be an integer between ``1 `` and ``9 ``. The
240240 default is ``9 ``.
241241
242242 For incremental compression, use a :class: `BZ2Compressor ` instead.
243243
244244
245245.. function :: decompress(data)
246246
247- Decompress *data *.
247+ Decompress *data *, a :term: ` bytes-like object <bytes-like object> ` .
248248
249249 If *data * is the concatenation of multiple compressed streams, decompress
250250 all of the streams.
@@ -254,3 +254,75 @@ One-shot (de)compression
254254 .. versionchanged :: 3.3
255255 Support for multi-stream inputs was added.
256256
257+ .. _bz2-usage-examples :
258+
259+ Examples of usage
260+ -----------------
261+
262+ Below are some examples of typical usage of the :mod: `bz2 ` module.
263+
264+ Using :func: `compress ` and :func: `decompress ` to demonstrate round-trip compression:
265+
266+ >>> import bz2
267+
268+ >>> data = b """ \
269+ ... Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
270+ ... nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
271+ ... sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
272+ ... pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
273+ ... Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
274+ ... felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
275+ ... dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum. """
276+
277+ >>> c = bz2.compress(data)
278+ >>> len (data) / len (c) # Data compression ratio
279+ 1.5212121212121212
280+
281+ >>> d = bz2.decompress(c)
282+ >>> data == d # Check equality to original object after round-trip
283+ True
284+
285+ Using :class: `BZ2Compressor ` for incremental compression:
286+
287+ >>> import bz2
288+ >>> import os
289+ >>>
290+ >>> def gen_data (chunks = 10 , chunksize = 1000 ):
291+ ... """ Yield incremental blocks of ``chunksize`` bytes."""
292+ ... for _ in range (chunks):
293+ ... yield os.urandom(chunksize)
294+ ...
295+ >>> comp = bz2.BZ2Compressor()
296+ >>> out = b " "
297+ >>> for chunk in gen_data():
298+ ... # Provide data to the compressor object
299+ ... out = out + comp.compress(chunk)
300+ ...
301+ >>> # Finish the compression process. Call this once you have
302+ >>> # finished providing data to the compressor.
303+ >>> out = out + comp.flush()
304+
305+ Writing and reading a bzip2-compressed file in binary mode:
306+
307+ >>> import bz2
308+
309+ >>> data = b """ \
310+ ... Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
311+ ... nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
312+ ... sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
313+ ... pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
314+ ... Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
315+ ... felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
316+ ... dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum. """
317+
318+ >>> with bz2.open(" myfile.bz2" , " wb" ) as f:
319+ ... # Write compressed data to file
320+ ... f.write(data)
321+ ... 502
322+
323+ >>> with bz2.open(" myfile.bz2" , " rb" ) as f:
324+ ... # Decompress data from file
325+ ... content = f.read()
326+
327+ >>> content == data # Check equality to original object after round-trip
328+ True
0 commit comments