Skip to content

Compress

The act of compressing some data simply means modifying the data to reduce its size. When it comes to features like these, the compress.py module is useful for that.

The compress module provides a way to compress and decompress data using python compression libraries. If you have a large amount of data that you need to store or transfer, you can use this module to compress the data and save space.

from everysk.core.compress import compress

data = 'example of a large dataset, this can be of any datatype.'
compress(data)

b'x\x9ck`\x99j\xc3\x00\x01=\x16\xa9\x15\x89\xb9\x059\xa9\n\xf9i\n\x89\n9\x89E\xe9\xa9\n)\x89%\x89\xc5\xa9%:\n%\x19\x99\xc5\n\xc9\x89y\nI\x10\xf9\xbcJ\xb0\\IeA\xaa\xde\x14=\x00\xeb\xc4\x16\xb3'


The module supports the following compression algorithms:

  • gzip: This is a widely used compression algorithm that is supported by most operating systems and programming languages.
  • zlib: This is another popular compression algorithm that is supported by most operating systems and programming languages.

If needed, you could set the serialization format to pickle or json to compress the data.

Compressing Data

To compress data, you can use the compress function from the compress module. The function takes the data to be compressed and the compression algorithm as arguments. Here is an example of how to compress data using the gzip algorithm:

from everysk.core.compress import compress

compress('string', protocol='gzip')
b'\x1f\x8b\x08\x00...\x02\xff...\x00\x00\x00'  # bytes vary due to gzip timestamp header

Decompressing Data

To decompress data, you can use the decompress function from the compress module. The function takes the compressed data and the compression algorithm as arguments. Here is an example of how to decompress data using the gzip algorithm:

from everysk.core.compress import compress, decompress

compressed = compress('string', protocol='gzip')
decompress(compressed, protocol='gzip')
'string'

Compress/Decompress with Serialization

When compressing and decompressing data, you can also serialize and deserialize the data using the serialize parameter. An advantage of this is that you can compress and decompress complex data structures like dictionaries and lists and retrieve the result as the same object.

from everysk.core.datetime import Date
from everysk.core.compress import compress, decompress

compressed = compress(Date(2024, 1, 1), protocol='gzip', serialize='json')
decompress(compressed, protocol='gzip', serialize='json')
Date(2024, 1, 1)