- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Python Standard Library
In this Guided Code Lab, you will enhance a recipe book application to persist data between sessions using the pickle module in Python. This process, known as data serialization, is crucial for applications that need to save and load data. You'll learn about serialization, explore the pickle module, handle errors during pickling, and use the Pickler and Unpickler classes for better control over the serialization process.
Lab Info
Table of Contents
-
Challenge
Introduction
Introduction
Welcome to the Guided: Python Standard Library - Data Serialization lab 🚀.
In this Guided Code Lab, you will enhance a recipe book application to persist data between sessions using the pickle module in Python. This process, known as data serialization, is crucial for applications that need to save and load data. You'll learn about serialization, explore the pickle module, handle errors during pickling, and use the Pickler and Unpickler classes for better control over the serialization process.
Pre-requisites
- Basic understanding of Python programming.
Learning Objectives
- Understand data serialization and its importance.
- Explore the Pickle module in Python for data serialization.
- Implement and handle errors during pickling and unpickling.
- Enhance a recipe book application for data persistence between sessions.
Running The App
- You can run the Cookbook app in the terminal with the command
python3 cookbook.py
-
Challenge
Data Serialization Concepts
Introduction to Data Serialization
Understanding Data Serialization is fundamental as it facilitates the conversion of in-memory objects into a format that can be saved to disk or sent over a network, and later reconstructed. This is crucial for developing applications that require data persistence or inter-process communication.
Hands On - JSON vs Pickle
Let's explore two common ways to serialize data with Python, JSON and Pickle
In the terminal to your right, execute the following lines of code.
import json import pickle # Serialization using JSON data = {"name": "John", "age": 30} json_serialized = json.dumps(data) print(json_serialized) # Outputs: {"name": "John", "age": 30} # Deserialization using JSON json_deserialized = json.loads(json_serialized) print(json_deserialized) # Outputs: {'name': 'John', 'age': 30} # Serialization using Pickle pickle_serialized = pickle.dumps(data) print(pickle_serialized) # Outputs: b'\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04name\x94\x8c\x04John\x94\x8c\x03age\x94K\x1eu.' # Deserialization using Pickle pickle_deserialized = pickle.loads(pickle_serialized) print(pickle_deserialized) # Outputs: {'name': 'John', 'age': 30}Summary
JSON: Human-readable and textual format. Ideal for configuration files, data exchange between client-server, and different language environments. However, it doesn't support binary data and may not preserve data types for complex objects.
Pickle: Binary format, capable of serializing complex Python objects, and supports binary data. Ideal for serializing Python objects, but it's Python-specific and not human-readable.
Data Type Preservation Example
Let's dig a little deeper and take a look at an example showing data type preservation in Pickle vs JSON.
complex_data = (1 + 2j) # Complex number pickle_serialized = pickle.dumps(complex_data) # This will raise a TypeError as JSON can't serialize complex numbers json_serialized = json.dumps(complex_data) pickle_deserialized = pickle.loads(pickle_serialized) # This will preserve the complex number data typeSecurity Warning
The
picklemodule is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.The choice between JSON and Pickle, or other serialization formats, depends on the use case, with considerations for human-readability, data complexity, language interoperability, and security. In this lab we are going to focus on using Pickle. Let's get started!
-
Challenge
Setup and Import
Implementing Load and Save Functions
Having the ability to load and save data is crucial for providing a persistent state in applications. This section explores creating dedicated functions to handle loading and saving of data in your recipe book application.
All code will be written in
cookbook.pyPart 1: Importing Necessary Modules
Task
- Import the
picklemodule which will provide the necessary classes and functions for this lab.
import pickle - Import the
-
Challenge
Loading Recipes with Unpickler
Part 2: Loading Recipes with Unpickler
The Unpickler class in the
picklemodule is used to deserialize objects from a binary stream. In this part, you'll create a function to load recipes from a file using the Unpickler class.Tasks
- Define a function named
load_recipeswith a parameterfilename. - Within the function, use a
tryblock to attempt to open the file specified byfilenamein binary read mode ('rb'). - Create an instance of
pickle.Unpickler, passing the file object to it. - Use the
loadmethod of the Unpickler instance to deserialize the data from the file. - Return the deserialized data.
- Use an
exceptblock to catch aFileNotFoundErrorexception, returning an empty list in case the file doesn't exist.
đź’ˇ Hint: Peek at the solution code in solution/cookbook.py if you are stuck.
- Define a function named
-
Challenge
Saving Recipes with Pickler
Part 3: Saving Recipes with Pickler
The Pickler class in the
picklemodule is used to serialize objects into a binary stream. In this part, you'll create a function to save recipes to a file using the Pickler class.Tasks
- Define a function named
save_recipeswith parametersrecipesandfilename. - Within the function, use a
tryblock to attempt to open the file specified byfilenamein binary write mode ('wb'). - Create an instance of
pickle.Pickler, passing the file object to it. - Use the
dumpmethod of the Pickler instance to serialize therecipesdata into the file. - Use an
exceptblock to catch apickle.PicklingErrorexception, printing an error message in case the serialization fails.
đź’ˇ Hint: Peek at the solution code in
solution/cookbook.pyif you are stuck. - Define a function named
-
Challenge
Update Main Function
Part 4: Update the Main Function
Tasks
- Call
load_recipesat the start of your main function andsave_recipesbefore exiting the application.
This will ensure that your recipe data persists between application sessions, providing a much better user experience.
Pickling without a file can be useful in situations where you need to send serialized data over a network or save it to a database. Python's
picklemodule providesdumps()andloads()methods for this purpose.In the terminal to the right, run the following commands.
Hands On
# Pickling to a bytes object pickled_data = pickle.dumps(data) # Unpickling from a bytes object unpickled_data = pickle.loads(pickled_data)Potential Pitfalls
It's important to ensure that the serialized data does not get corrupted or altered as it could lead to errors during deserialization.
# Example of a PicklingError try: bad_data = pickle.loads(b"not a pickled object") except pickle.PicklingError as e: print(f"PicklingError: {e}")In summary, pickling without a file is a flexible way to work with serialized data in different contexts beyond file operations.
- Call
-
Challenge
Bytes Serialization Implementation
Your Turn: Implementing Bytes Serialization
In this part, you'll add the ability to pickle and unpickle data to and from bytes objects in your application.
Part 5: Serializing Data to Bytes Objects
The
pickle.dumps()function is used to serialize an object to a bytes object, instead of writing it to a file.Task
- Define a function named
pickle_to_byteswith a parameterrecipes. - Within the function, use
pickle.dumps()to serialize therecipesdata to a bytes object. - Return the serialized bytes object.
Part 6: Deserializing Data from Bytes Objects
The
pickle.loads()function is used to deserialize a bytes object back to its original object form.Task
- Define a function named
unpickle_from_byteswith a parameterpickled_data. - Within the function, use
pickle.loads()to deserialize thepickled_databytes object back to its original form. - Return the deserialized object.
Best Practices
Be cautious when using the
globalkeyword, as it can lead to code that is difficult to debug or maintain. It's often better to pass variables as arguments to functions where possible.# It's often better to pass variables as arguments def save_data(data, filename): with open(filename, 'wb') as file: pickle.dump(data, file) # Calling the function with arguments save_data(recipes, filename) - Define a function named
-
Challenge
Summary and Next Steps
Summary
Congratulations on completing the lab! 🎉 In this lab, you've learned about:
- Data Serialization: The process of converting in-memory objects into a format that can be saved to disk or transmitted over a network, and later reconstructed.
- Pickle Module: A Python module that provides functionalities for serializing and deserializing Python objects.
- Implementing Load and Save Functions: Created functions to handle the loading and saving of data to ensure the persistence of application state.
- Pickling Without a File: How to serialize and deserialize data to and from bytes objects, which is useful in scenarios such as network transmission or database storage.
- Modifying the Main Function: Incorporated loading and saving recipes in the main function to ensure data persistence across different runs of the application.
Throughout the lab, you not only explored the theoretical concepts but also applied them practically in a Recipe Book application. This hands-on approach helped in solidifying the understanding of data serialization using Python's Pickle module, and how it's instrumental in building applications with data persistence.
Next Steps
To further your understanding and skills, you might want to:
- Explore other serialization libraries in Python such as
jsonandmarshal. - Look into security best practices when working with serialization, especially when handling data from untrusted sources.
- Experiment with more complex data structures and see how they are handled by the
picklemodule. - Explore how to work with files in Python, including reading from and writing to files, which is a crucial part of data serialization.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.