Introduction to the Python pickle class library (object serialization and deserialization)

  • 2020-04-02 14:24:49
  • OfStack

A, pickle

The pickle module is used to serialize and deserialize python objects. Usually pickle serializes python objects into binary streams or files.
 
Serialization and deserialization between python objects and files:


pickle.dump()
pickle.load()

To achieve serialization and deserialization between python objects and strings, use:

pickle.dumps()
pickle.loads()

 
The types that can be serialized are:
* None, True and False;
* integer, floating point, complex;
* string, byte stream, byte array;
* contain pickled objects tuples, lists, sets and dictionaries;
* functions defined at the top of the module:
* built-in functions defined at the top of the module;
* classes defined at the top of the module;
* with custom types of s/s s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s;
 

Note: serialization of a function or class is identified by name, so import the corresponding module.

Ii. Operation process of pickle

In most cases, if the object is picklable, we don't need extra code. By default pickling intelligently checks the properties of the class and instance, and its s/s methods are usually not called when an instance of the class is deserialized. Instead, you first create an uninitialized instance and then reply to the stored properties.
 

But you can change the default behavior by implementing the following methods:


object.__getstate__() : Serializes objects by default __dict__, But if you do __getstate__(), the __getstate__() The value returned by the function is serialized.
object.__setstate__(state) : If the type implements this method, it is used to restore the properties of the object during deserialization.
object.__getnewargs__() : If the instance is constructed ( __new__() ) if you need parameters, you need to implement this function.

Note: if s/he returns False, s/he is not called during deserialization.

Sometimes you need to implement the s/s () function for efficiency or if the above 3 functions cannot meet the requirement.

Three, the instance,


import pickle # An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': set([None, True, False])
} with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)    
with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)
    print(str(data))

Modify the default behavior of the picklable type    


class TextReader:
    """Print and number lines in a text file."""     def __init__(self, filename):
        self.filename = filename
        self.file = open(filename)
        self.lineno = 0     def readline(self):
        self.lineno += 1
        line = self.file.readline()
        if not line:
            return None
        if line.endswith('n'):
            line = line[:-1]
        return "%i: %s" % (self.lineno, line)     def __getstate__(self):
        # Copy the object's state from self.__dict__ which contains
        # all our instance attributes. Always use the dict.copy()
        # method to avoid modifying the original state.
        state = self.__dict__.copy()
        # Remove the unpicklable entries.
        del state['file']
        return state     def __setstate__(self, state):
        # Restore instance attributes (i.e., filename and lineno).
        self.__dict__.update(state)
        # Restore the previously opened file's state. To do so, we need to
        # reopen it and read from it until the line count is restored.
        file = open(self.filename)
        for _ in range(self.lineno):
            file.readline()
        # Finally, save the file.
        self.file = file
       
reader = TextReader("hello.txt")
print(reader.readline())
print(reader.readline())
s = pickle.dumps(reader)
#print(s)
new_reader = pickle.loads(s)
print(new_reader.readline()) # the output is
# 1: hello
# 2: how are you
# 3: goodbye


Related articles: