Serializing Python Objects with Pickle and Shelve

1 hour
  • 3 Learning Objectives

About this Hands-on Lab

Object serialization and persistence can be incredibly useful if you need to share data between multiple applications or save data for use later. Python provides utilities for serializing and deserializing Python objects to and from bytes, to make it possible to pass Python data structures between Python applications or to read/write to a file without needing to convert to an intermediate data format like JSON. In this hands-on lab, you’ll utilize these features in the standard library to create a simple flashcard command line application. By the time you’ve finished this lab, you should have a good understanding of how to use the `pickle` and/or `shelve` modules from the Python standard library.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a FlashCard Class with a Front and a Back

The FlashCard class needs to hold onto some string information. This class is a data class.

Implement new, delete, and list CLI Subcommands

The new_flashcard, delete_flashcard, and list_flashcards functions all need to be implemented so that they can read and write from a local file that persists the flashcard information.

Implement the practice Command

When practicing, you want to be able to specify the number of questions.

Additional Resources

You're planning on taking a difficult certification and know that you need to study. You've decided that the best way for you to study is to use flashcards. To facilitate this, you've decided to build a flashcards tool for yourself that will allow you to create and delete flashcards, and also randomly present you with a variable number of questions to practice. You want to be able to keep your work between study sessions, so you want to persist your flashcards. You have decided to use either pickle or shelve from the Python standard library to do this. Here's how you've planned on having the CLI work:

  • Adding a new flashcard
$ ./flashcards.py new "Front of card" "Back of card"
New card added to flashcard deck.
  • Listing flashcards
$ ./flashcards.py list
1. Front of first card
2. Front of second card
  • Deleting a flashcard
$ ./flashcards.py delete 1
Card Deleted: Front of first card
  • Practicing
$ ./flashcards.py practice 5 # optional defaults to 10
Asking 5 questions.

Question 1. (press Enter to reveal back of cards):

Front of first card

Back of first card

Press Enter to see the next question.

Question 2. (press Enter to reveal back of cards):

Front of another card

Back of another card

The file flashcards.py is already executable and includes the boilerplate for building a CLI with subcommands. To finish this tool, you'll need to implement the logic of the subcommands within the various functions.

What are Hands-on Labs

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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!