Advanced Python Development Course
Chapter
>
Level
Serialization-modules
Compression Modules
Objective
Take messages and orders and compress them for archiving and transmission.
There are several construction instructions, shipments and manifests that have been submitted and need to be transferred or archived. While these can be sent out by manually sending everything out a good way to do this is by compressing and decompressing files then storing or sending them out. To do this, you can use compression modules to decrease file sizes and make information easier to manage. Unlike other problems we solved where we learned and used a single module, there are several compression modules we can use with a small selection of functions. They are as follows:
For fast compression speed and low compression, use the zlib module, by adding import zlib . For our purposes we will be using the following function: zlib.compress() , uses one argument, that being the information you would like to compress. Argument needs to be entered as bites instead of a string.
For medium compression speed and medium compression, use the bz2 module, by adding import bz2. For our purposes we will be using the following functions:
bz2.compress(), uses one argument, that being the information you would like to compress.bz2.decompress(), uses one argument, that being the information you would like to compress. It needs to be entered as bites instead of a string.
Arguments needs to be entered as bites instead of a string.
For high compression but slow compression speed , use the lzma module, by adding import lzma . For our purposes we will be using the following classes and functions:
lzma.LZMACompressor(): Class used to create compression objects allowing you to activate a compressor.compress(): uses one argument, that being the information you would like to compress. It needs to be entered as bites instead of a string. Argument needs to be entered as bites instead of a string.
In addition to these modules, we’ll be using the bytes() function, allowing you to convert strings into bytes which will be useful for compressing. It takes two arguments, the string you wish to convert and the format, which for our purposes is 'utf-8' .
There are three constants containing data that needs to be compressed, these being named red_message , blue_message and green_message. To start off walk to the gold X mark over the red carpet, create a variable named red_bytes . Take the red_message constant and convert the message into bytes so the data may be compressed, by using the bytes() function formatted to 'utf-8' , like this: red_bytes = bytes(red_message, 'utf-8') .
Create a variable named red_compression and store the value of zlib.compress() . Use red_bytes as an argument which will compress the data, like this: red_compression = zlib.compress(red_bytes) . Once the data has been compressed, use the pre-written display() function on the code editor and add the red_message and red_compression variables as arguments in order to read the message and view the compression data.
Walk to the light X mark over the red carpet and face the desk, create two variables named: message_length and compression_length . In these variables we will be storing the character length of the data both before and after compression using the len() function. For example, for message_length get the length of red_message , like this: message_length = len(red_message) . Do the same for compression_length by storing the len() of red_compression .
Once both variables have been populated, use the pre-written write() function and insert message_length and compression_length as arguments in order to verify the compression sizes. This demonstrates how much the file is compressed relative to when it’s left uncompressed.
Walk to the dark X mark over the blue carpet, face the computer. Create a variable named blue_bytes and convert blue_message into bytes using the bytes() function in the same manner as you did with red_message .Take the blue_message constant and compress it by storing the value of bz2.compress() and inserting blue_bytes as an argument to compress the data, like this: blue_message = bz2.compress(blue_bytes) .
Create a variable named blue_decompress and store the value of bz2.decompress() and add blue_message as an argument in order to view the data after decompression. Add the blue_message and blue _decompress variables to the pre-written display() function in order to view compressed and compressed variants of the message data.
Make your way to the dark X mark over the green carpet and create an object named compressor and populate it with lzma.LZMACompressor() , this will create a compressor object that can be used with lzma functions. Create a variable named green_bytes and transform green_message into bytes format using bytes() function in the same manner as you did with red_message and blue message .
Create a variable named green_compression and store compressor.compress() as a value, adding green_bytes as a variable. This will allow you to compress the message data using the compressor object. On the pre-written display() function add the green_message and green_compression variables as arguments in order to view the message and compressed data.
Walk to the light X mark over the green carpet and face the desk, modify the two variables named: message_length and compression_length . In these variables we will be storing the character length of the data both before and after compression using the len() function. For message_length get the length of green_message with len(). Do the same for compression_length by storing the len() of green_compression . Once both variables have been populated, use the pre-written write() function and insert message_length and compression_length as arguments in order to verify the compression sizes and finish the level.