@@ -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.
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
@@ -144,7 +144,7 @@ Incremental (de)compression
144
144
incrementally. For one-shot compression, use the :func: `compress ` function
145
145
instead.
146
146
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
148
148
default is ``9 ``.
149
149
150
150
.. method :: compress(data)
@@ -230,17 +230,17 @@ One-shot (de)compression
230
230
231
231
.. function :: compress(data, compresslevel=9)
232
232
233
- Compress *data *.
233
+ Compress *data *, a :term: ` bytes-like object <bytes-like object> ` .
234
234
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
236
236
default is ``9 ``.
237
237
238
238
For incremental compression, use a :class: `BZ2Compressor ` instead.
239
239
240
240
241
241
.. function :: decompress(data)
242
242
243
- Decompress *data *.
243
+ Decompress *data *, a :term: ` bytes-like object <bytes-like object> ` .
244
244
245
245
If *data * is the concatenation of multiple compressed streams, decompress
246
246
all of the streams.
@@ -250,3 +250,77 @@ One-shot (de)compression
250
250
.. versionchanged :: 3.3
251
251
Support for multi-stream inputs was added.
252
252
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