Skip to content

Commit ee9b5ce

Browse files
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 (cherry picked from commit be6939f) Co-authored-by: Brad <[email protected]>
1 parent 19464bc commit ee9b5ce

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.
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

@@ -144,7 +144,7 @@ Incremental (de)compression
144144
incrementally. For one-shot compression, use the :func:`compress` function
145145
instead.
146146

147-
*compresslevel*, if given, must be a number between ``1`` and ``9``. The
147+
*compresslevel*, if given, must be an integer between ``1`` and ``9``. The
148148
default is ``9``.
149149

150150
.. method:: compress(data)
@@ -230,17 +230,17 @@ One-shot (de)compression
230230

231231
.. function:: compress(data, compresslevel=9)
232232

233-
Compress *data*.
233+
Compress *data*, a :term:`bytes-like object <bytes-like object>`.
234234

235-
*compresslevel*, if given, must be a number between ``1`` and ``9``. The
235+
*compresslevel*, if given, must be an integer between ``1`` and ``9``. The
236236
default is ``9``.
237237

238238
For incremental compression, use a :class:`BZ2Compressor` instead.
239239

240240

241241
.. function:: decompress(data)
242242

243-
Decompress *data*.
243+
Decompress *data*, a :term:`bytes-like object <bytes-like object>`.
244244

245245
If *data* is the concatenation of multiple compressed streams, decompress
246246
all of the streams.
@@ -250,3 +250,77 @@ One-shot (de)compression
250250
.. versionchanged:: 3.3
251251
Support for multi-stream inputs was added.
252252

253+
.. _bz2-usage-examples:
254+
255+
Examples of usage
256+
-----------------
257+
258+
Below are some examples of typical usage of the :mod:`bz2` module.
259+
260+
Using :func:`compress` and :func:`decompress` to demonstrate round-trip compression:
261+
262+
>>> import bz2
263+
264+
>>> data = b"""\
265+
... Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
266+
... nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
267+
... sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
268+
... pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
269+
... Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
270+
... felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
271+
... dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum."""
272+
273+
>>> c = bz2.compress(data)
274+
>>> len(data) / len(c) # Data compression ratio
275+
1.513595166163142
276+
277+
>>> d = bz2.decompress(c)
278+
>>> data == d # Check equality to original object after round-trip
279+
True
280+
281+
Using :class:`BZ2Compressor` for incremental compression:
282+
283+
>>> import bz2
284+
285+
>>> def gen_data(chunks=10, chunksize=1000):
286+
... """Yield incremental blocks of chunksize bytes."""
287+
... for _ in range(chunks):
288+
... yield b"z" * chunksize
289+
...
290+
>>> comp = bz2.BZ2Compressor()
291+
>>> out = b""
292+
>>> for chunk in gen_data():
293+
... # Provide data to the compressor object
294+
... out = out + comp.compress(chunk)
295+
...
296+
>>> # Finish the compression process. Call this once you have
297+
>>> # finished providing data to the compressor.
298+
>>> out = out + comp.flush()
299+
300+
The example above uses a very "nonrandom" stream of data
301+
(a stream of `b"z"` chunks). Random data tends to compress poorly,
302+
while ordered, repetitive data usually yields a high compression ratio.
303+
304+
Writing and reading a bzip2-compressed file in binary mode:
305+
306+
>>> import bz2
307+
308+
>>> data = b"""\
309+
... Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue
310+
... nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,
311+
... sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus
312+
... pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.
313+
... Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo
314+
... felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum
315+
... dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum."""
316+
317+
>>> with bz2.open("myfile.bz2", "wb") as f:
318+
... # Write compressed data to file
319+
... unused = f.write(data)
320+
321+
>>> with bz2.open("myfile.bz2", "rb") as f:
322+
... # Decompress data from file
323+
... content = f.read()
324+
325+
>>> content == data # Check equality to original object after round-trip
326+
True

0 commit comments

Comments
 (0)