Python class library 32 [serialization and deserialization of pickle]

Source: Internet
Author: User

One pickle

The pickle module is used to serialize and deserialize python objects. Generally, pickle serializes a python object into a binary stream or file.

 

Serialization and deserialization between python objects and files: pickle. dump () pickle. load () If you want to implement serialization and deserialization between python objects and strings, use: pickle. dumps () pickle. loads ()

 

The following types can be serialized: * None, True, and False; * integer, floating point, and plural; * string, byte stream, and byte array; * contains the tuples, lists, and pickle objects, 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; * owns _ dict __() or the custom type of _ setstate;

 

Note: serialization of functions or classes is identified by names, so you need to import the corresponding module.

 

The operating process of binary pickle

In most cases, we do not need additional code for the object picklable. By default, pickle intelligently checks the attributes of classes and instances. When a class instance is deserialized, its _ init _ () method is usually not called. Instead, create an uninitialized instance and then reply to the stored attributes.

 

However, you can modify the default behavior by implementing the following methods: object. _ getstate _ (): The _ dict __of the object is serialized by default. However, if you implement _ getstate _ (), _ getstate __() the value returned by the function will be serialized. Object. _ setstate _ (state): If the type implements this method, this method is used to restore the attributes of the object during deserialization. Object. _ getnewargs _ (): this function is required if a parameter is required during instance construction (_ new. NOTE: If _ getstate _ () returns False, _ setstate _ () is not called during deserialization.

 

Sometimes, for efficiency, or the above three functions cannot meet the requirements, the _ reduce _ () function must be implemented.

 

Three-instance import pickle

# An arbitrary collection of objects supported by pickle.
Data = {
'A': [1, 2.0, 3, 4 +],
'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 ))

4. 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 previusly opened file's state. To do so, we need
# 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

Complete!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.