Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Serialization-modules
Marshal and Pickle Modules

Objective

Go to the service station and make sure data is properly stored by using JSON module to format files.

Continuing across the path, we find a few stations monitoring the water distribution and soil quality for the land that’s being developed for a future harvest. In order process the data it’s efficient to serialize the data. While using Json can be effective as generalized solution, there are also a few python specific serialization methods, those being the Marshal and Pickle modules. The Marshal module is designed to for fast serialization/deserialization by converting the data into binary, ideal for simpler data transfers. The Pickle module is slower but it’s designed to process more complex data structures and is a lot more flexible, ideal for larger or priority data structures.

To use the Marshal and Pickle modules, import their functions by writing: import marshal and import pickle. For our purposes, we’ll be using the following functions:

  • marshal.dumps(): Serializes data into a binary format, designed for speed within the python. Takes one argument, that being the data you wish to serialize, is limited to basic data types.
  • marshal.loads(): Deserializes binary data into python readable data types, limited in it’s capabilities but fast execution. Takes one argument, that being the data to deserialize.
  • pickle.DEFAULT_PROTOCOL: Checks what protocol version for Pickle serialization is default in your Python version. The protocols affect how the data is serialized.
  • pickle.HIGHEST_PROTOCOL: Checks what protocol version for Pickle serialization is the highest available in your Python version. The protocols affect how the data is serialized.
  • pickle.dumps(): Serializes complex python data structures in binary format according to the protocol used. Takes two arguments, the data you wish to serialize and the protocol version you wish to use when serializing.
  • pickle.loads(): Deserializes binary data into python readable data types, powerful in scope for python objects. Takes one argument, that being the data you wish to deserialize.

In addition to these modules and functions we will also be using a formatting module named pprint , which we will import using from pprint import pprint . This allows us to use the function pprint() which can be used to format data structures like dictionaries allowing us to display the information clearly.

To start off walk to the light X mark inside the small monitoring station and face the computer terminal. The station monitors the water pressure and and soil conditions of the nearby terrain that’s being developed for planting crops. There is a constant named reading , this contains python data collected by the station.

Create a variable named data and store the value of marshal.dumps() function and setting reading as the argument. Like this: data = marshal.dumps(reading) . Use data variable with the pre-written display() function to view the serialized variable data.

Walk out to the station and head to the gold X mark, use the open() function to open the door and gain access to the office. Walk to the light X mark in front of the terminal, and deserialize the data you just serialized. Create a variable named output and store the value of marshal.loads() with the previous data variable as an argument. Use the write() function and add output as an argument to access the serialized data you collected from the monitoring station.

Walk to the light X mark over the blue carpet in the office, on the terminal we’ll be verifying the protocols to determine what type of serialization the pickle module will be using when processing data. In the pre-written write() function, set the arguments as pickle.DEFAULT_PROTOCOL and pickle.HIGHEST_PROTOCOL . This will verify what the current serialization protocol is as well as the latest serialization protocol available at this time.

Move on to the dark X mark over the blue carpet, now that we’ve confirmed the protocols, we can now verify stored data. Data has been collected and stored throughout the months and stored in a constant named samples . Create three variables named: sereal_0 , sereal_2 and sereal_5 we will use these to verify three of the protocols available for serialization. Protocol 0 is readable ASCII, not very efficient, Protocol 2 is a more efficient binary format and Protocol 5 is the latest with the most complex features added.

We will store the value of pickle.dumps() and set as arguments the sample constant and the protocol corresponding to the number on the variable name. For example, for sereal_0 we will set the protocol as 0 , like this: sereal_0 = pickle.dumps(samples, protocol=0) Use the sereal_0 , sereal_2 and sereal_5 with the pre-written display() function in order to verify how different the serialization for each protocol.

Walk to the light X mark over the red carpet and face the terminal, overwrite the output variable and store the value of pickle.loads() and set the argument to sereal_5 in order to deserialize the sample data that has been gathered so far. Use the display() function and add the pprint() function with the output variable in order to view the data. Like this: await player.display(pprint(output)) . Make note of the ratings for each month displayed in the terminal so you may chart them down later.

Walk to the dark X mark over the red carpet and face the desk, on the pre-written write() function, add the ratings you made note of for each month, that were previously displayed in the terminal. Add them as strings with "" marks in the correct order as they are outlined in the function in order to complete the level.

Code book