I just wanted to understand Python's Pickle module

What's Pickle this image.jpeg

Wow delicious ~ Aside from the blur, the name comes from Pickles, a Python module that allows you to save the state of an object by serializing it.

I've been studying deep learning lately and often

train_list = load_image_list(args.train)
val_list = load_image_list(args.val)
mean_image = pickle.load(open(args.mean, 'rb'))

I often come across codes like. I thought, "What is pickle?", So I will summarize what I researched.

Since I have little knowledge of programming in general, please point out if there is a description that you are interested in.

What is pickling and non-pickling (*) of objects?

Most of it is documented.

http://docs.python.jp/2/library/pickle.html http://docs.python.jp/3/library/pickle.html

By its very nature, programs work in memory and are removed from memory when they have finished their role. The object will also be deleted by the GC when it finishes its role, or it will disappear when the program finishes running.

The Pickle module provides the ability to save the created object even after the program has finished running.

The way to save is to convert the object to a byte string and write it to a file.

Execution example

Write a simple class.

Singer.py


class Singer(object):
    def __init__(self, lylics):
        self.lylics = lylics
    def sing(self):
        print(self.lylics)

When this class is instantiated

main.py


from singer import Singer

singer = Singer('Shanranran')
singer.sing()
$ main.py
$ 'Shanranran'

And so on, it will sing the lyrics you passed to the constructor.

Of course, Singer forgets the lyrics when the run is finished.

So, instead of using the lyrics card, use Pickle to save the state of the object.

dump.py


import pickle
from singer import Singer

singer = Singer('Shanranran')

with open('singer.pickle', 'wb') as f:
    pickle.dump(singer, f)

By making the state of the Singer object a binary file called singer.pickle, you can save the state of the object even after dump.py is finished. This is Pickle.

I use the load method to make an object non-pickle,

load.py


import pickle

with open('singer.pickle', 'rb') as f:
    singer = pickle.load(f)

singer.sing()
$ python load.py
$ Shanranran

It was confirmed that the state of the Singer object when dump.py was executed was saved.

in conclusion

I briefly wrote about the Pickle module, but this is just the beginning, and there are still more things to be aware of when using it in earnest. I'm studying myself, so I'll write it when I look it up again.

reference

12.1. pickle — Serialization of Python objects http://docs.python.jp/3/library/pickle.html

11.1. pickle — Serialization of Python objects http://docs.python.jp/2/library/pickle.html

Python: Pickle an object http://blog.amedama.jp/entry/2015/12/05/132520

Recommended Posts

I just wanted to understand Python's Pickle module
Hash chain I wanted to avoid (2)
I want to understand systemd roughly
How to use Python's logging module
I wanted to evolve cGAN to ACGAN
Hash chain I wanted to avoid (1)
I wanted to be careful about the behavior of Python's default arguments
I wanted to solve ABC159 in Python
I wanted to solve ABC172 with Python
I really wanted to copy with selenium
Implemented DQN in TensorFlow (I wanted to ...)
I wanted to solve NOMURA Contest 2020 with Python
i-Town Page Scraping: I Wanted To Replace Wise-kun
I wanted to play with the Bezier curve
I wanted to install Python 3.4.3 with Homebrew + pyenv
I tried to touch Python's GUI library "PySimple GUI"
I just wanted to extract the data of the desired date and time with Django
I want to add my own structure to the structure created by Python's C extension module!
I want to understand (engineering) UMAP stronger than t-SNE
I also wanted to check type hints with numpy
I made a Python module to translate comment outs
I wanted to use the Python library from MATLAB
I want to fully understand the basics of Bokeh
[Failure] I wanted to generate sentences using Flair's TextRegressor
I wanted to modify Django's admin site a bit