Python Friday #94: Store Your Objects With Pickle

Sometimes the collection of data takes a lot longer than processing it. Wouldn’t it be great if you could store whole Python objects? If something goes wrong, we can restart the analytics part and don’t need to recreate the data.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

What is pickle?

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. (Source: docs.python.org)

Pickle is a great helper to store (temporarily) your data. Be aware that a lot is going on when you unpickle data that could compromise your computer – therefore, only use it with trusted sources!

 

Save your objects

We can use the same example data from the post on PrettyPrinter:

For pickle we need a file that we open in the binary mode. The dump() method does all the work and persists our objects:

The pickle.bin file now contains our objects in a binary format. If we open the file in a text editor, we see a lot of special characters:

Our objects are stored in a binary format

 

Restore your objects

Pickle offers the load() method to restore our objects from a file (that we need to open in binary mode):

For our application, the restored objects look the same as if we would create them in code. We can run our script and print the values to the command line or do whatever we want with them:

name => Rebecca Stephenson
phone => (154) 221-8558
zipCode => 900185
country => South Korea
options => [‘a’, ‘b’, ‘c’]
total => $74.79
name => Amos Nieves
phone => 1-762-301-2264
zipCode => 25566
country => Russian Federation
options => {‘a’: ‘full’, ‘f’: ‘partial’, ‘c’: {‘k1’: 1, ‘k2’: 3}}
total => $21.78

 

Conclusion

The pickle module in Python offers a built-in way to persist whole object graphs without much effort. If you need to temporarily store your objects you should try pickle.

1 thought on “Python Friday #94: Store Your Objects With Pickle”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.