@@ -83,7 +83,7 @@ All of the classes in this module may safely be accessed from multiple threads.
83
83
84
84
The *buffering * argument is ignored. Its use is deprecated since Python 3.0.
85
85
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
87
87
``1 `` and ``9 `` specifying the level of compression: ``1 `` produces the
88
88
least compression, and ``9 `` (default) produces the most compression.
89
89
@@ -148,7 +148,7 @@ Incremental (de)compression
148
148
incrementally. For one-shot compression, use the :func: `compress ` function
149
149
instead.
150
150
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
152
152
default is ``9 ``.
153
153
154
154
.. method :: compress(data)
@@ -234,17 +234,17 @@ One-shot (de)compression
234
234
235
235
.. function :: compress(data, compresslevel=9)
236
236
237
- Compress *data *.
237
+ Compress *data *, a :term: ` bytes-like object <bytes-like object> ` .
238
238
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
240
240
default is ``9 ``.
241
241
242
242
For incremental compression, use a :class: `BZ2Compressor ` instead.
243
243
244
244
245
245
.. function :: decompress(data)
246
246
247
- Decompress *data *.
247
+ Decompress *data *, a :term: ` bytes-like object <bytes-like object> ` .
248
248
249
249
If *data * is the concatenation of multiple compressed streams, decompress
250
250
all of the streams.
@@ -254,3 +254,77 @@ One-shot (de)compression
254
254
.. versionchanged :: 3.3
255
255
Support for multi-stream inputs was added.
256
256
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