Skip to content

Commit be6939f

Browse files
bsolomon1124gpshead
authored andcommitted
Docs: Add bz2 usage examples (GH-13258)
* Docs: Add bz2 usage examples - Adds an "Examples of usage" section inspired by the one found in the gzip docs - Corrects the descriptions for ``compresslevel`` and ``data``: - ``compresslevel`` must be an `int`, not any number. For instance, passing a float will raise ``TypeError`` - Notes that `data` must be bytes-like
1 parent 95da83d commit be6939f

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

Doc/library/bz2.rst

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,77 @@ 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.513595166163142
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+
289+
>>> def gen_data(chunks=10, chunksize=1000):
290+
... """Yield incremental blocks of chunksize bytes."""
291+
... for _ in range(chunks):
292+
... yield b"z" * chunksize
293+
...
294+
>>> comp = bz2.BZ2Compressor()
295+
>>> out = b""
296+
>>> for chunk in gen_data():
297+
... # Provide data to the compressor object
298+
... out = out + comp.compress(chunk)
299+
...
300+
>>> # Finish the compression process. Call this once you have
301+
>>> # finished providing data to the compressor.
302+
>>> out = out + comp.flush()
303+
304+
The example above uses a very "nonrandom" stream of data
305+
(a stream of `b"z"` chunks). Random data tends to compress poorly,
306+
while ordered, repetitive data usually yields a high compression ratio.
307+
308+
Writing and reading a bzip2-compressed file in binary mode:
309+
310+
>>> import bz2
311+
312+
>>> data = b"""\
313+
... Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
314+
... nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
315+
... sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
316+
... pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
317+
... Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
318+
... felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
319+
... dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum."""
320+
321+
>>> with bz2.open("myfile.bz2", "wb") as f:
322+
... # Write compressed data to file
323+
... unused = f.write(data)
324+
325+
>>> with bz2.open("myfile.bz2", "rb") as f:
326+
... # Decompress data from file
327+
... content = f.read()
328+
329+
>>> content == data # Check equality to original object after round-trip
330+
True

0 commit comments

Comments
 (0)