Skip to content

Compression Configuration

cshelve supports compression to reduce the size of stored data. This is particularly useful when working with large datasets or to reduce network time. The compression algorithm can be configured using the options provided in the configuration.

[default]
provider = in-memory
algorithm = zlib
[compression]
algorithm = zlib
level = 1

In this example, the algorithm is set to zlib, and the compression level is set to 1.

Supported compression algorithms include:

  • zlib: Uses the zlib library for compression.

cshelve supports compression transparently; the compression settings don’t require changes to the application code:

import cshelve
with cshelve.open('config.ini') as db:
db['data'] = 'This is some data that will be compressed.'
with cshelve.open('config.ini') as db:
data = db['data']

If the specified compression algorithm isn’t supported, cshelve raises an UnknownCompressionAlgorithmError. Ensure the chosen algorithm is correctly specified in the configuration.