Python Pickle Module Learning (super detail) __python

Source: Internet
Author: User
Tags object serialization serialization

Pickle provides a simple persistence function. objects can be stored on disk as files.

Pickle modules can only be used in Python, and almost all data types in Python (lists, dictionaries, collections, classes, and so on) are serialized using pickle,

Pickle serialization of data, poor readability, people generally unrecognized.

------------------------------------------

Pickle.dump (obj, file[, Protocol])
Serializes the object and writes the resulting data stream to the file object. Parameter protocol is a serialization mode with a default value of 0, which indicates serialization as text. The value of the protocol can also be 1 or 2, which indicates serialization in binary form.

------------------------------------------
Pickle.load (file)
Deserializes the object. Resolves the data in a file to a Python object.

It should be noted that at load (file), to allow Python to find the definition of the class, it will be an error:

For example, the following example [python] view plain copy import Pickle class Person:def __init__ (self,n,a): Self.nam E=n self.age=a def Show (self): print self.name+ "_" +str (self.age) AA = person ("Jgood", 2) AA. Show () F=open ("D:\\p.txt", "W") Pickle.dump (aa,f,0) f.close () #del person F=open (' d:\\p.txt ', ' R ') Bb=pickle.loa D (f) f.close () bb.show ()

If you do not comment out del person, then the error will be as follows:

This means that the current module cannot find the definition of the class.

--------------------------------------------------

Clear_memo ()
Empty Pickler's "memo". Using the Pickler instance, when serializing an object, it "remembers" the object reference that has been serialized, so that dump (obj) is called multiple times on the same object, and Pickler is not "goofy" to serialize multiple times.
Look at the following example:[Python]View Plain copy import Stringio import pickle class Person:def __init__ (self,n,a): self.name=n   Self.age=a def Show (self): print self.name+ "_" +str (self.age) AA = person ("Jgood", 2) aa.show () Fle = Stringio.stringio () pick = pickle. Pickler (fle) pick.dump (AA) Val1=fle.getvalue () Print len (val1) Pick.clear_memo () pick.dump (AA) Val2=fle.getval UE () Print len (val2) Fle.close ()

The above code runs as follows:

If you do not comment out, the run result is the second. If the comment is dropped, the result is the first one.

The main reason is that Python's pickle, if not clear_memo, will not serialize the object multiple times.

Original link: Click to open the link


Persistence means persisting objects, even between executing the same program multiple times. With this article, you will have a general understanding of the various persistence mechanisms of Python objects, from relational databases to Python pickle and other mechanisms. It also gives you a deeper understanding of Python's object serialization capabilities. What is persistence. The basic idea of permanence is simple. Suppose you have a Python program that may be a program to manage your day-to-day to-do items, and you want to save the Application object (to do) between this program multiple times. In other words, you want to store objects on disk for easy retrieval later. This is persistence. There are several ways to achieve this, each of which has its advantages and disadvantages. For example, you can store object data in a text file of a format, such as a CSV file. Or you can use relational databases, such as Gadfly, MySQL, PostgreSQL, or DB2. These file formats and databases are excellent and Python has a robust interface for all of these storage mechanisms. These storage mechanisms have one thing in common: stored data is independent of the objects and programs that manipulate the data. The advantage of this is that the data can be used as a shared resource for use by other applications. The disadvantage is that, in this way, other programs can be allowed to access the object's data, which violates the object-oriented encapsulation principle that the object's data can only be accessed through the object's own public interface. In addition, relational database methods may not be ideal for some applications. In particular, relational databases do not understand objects. In contrast, relational databases force their own type systems and relational data models (tables), each containing a set of tuples (rows), each containing a fixed number of static type fields (columns). If an application's object model cannot be easily converted to a relational model, it can be difficult to map objects to tuples and to map tuples back to objects. This difficulty is often referred to as a blocking mismatch (impedence-mismatch) problem. Object persistence requires some form of object serialization if you want to transparently store a Python object without losing information such as its identity and type: It is the process of converting arbitrarily complex objects into text or binary representations of objects. Similarly, you must be able to restore an object to its original object after it has been serialized. In Python, this serialization process, called Pickle, can pickle objects into strings, files on disk, or any object that resembles a file, or you can unpickle these strings, files, or any object that resembles a file to the original object. We'll discuss pickle in detail later in this article. Suppose you like to save everything as an object, and you want to avoidConvert an object to some kind of object-based storage overhead; then pickle files can provide these benefits, but sometimes you might need something more robust and scalable than this simple pickle file. For example, using pickle alone does not solve problems such as naming and locating pickle files, nor does it support concurrent access to persistent objects. If you need these aspects of functionality, turn to a database similar to the ZODB (Z object Database for Python). ZODB is a robust, multi-user, and object-oriented database system that stores and manages arbitrary and complex Python objects and supports transactional operations and concurrency control. (See Resources for a download of ZODB.) It is interesting enough that even ZODB relies on Python's native serialization capabilities, and that to effectively use ZODB, you must fully understand pickle. Another interesting way to solve persistent problems is Prevayler, which was originally implemented in Java (see Resources for the Developerworks article on Prevaylor). Recently, a group of Python programmers ported Prevayler to Python, another named Pypersyst, hosted by SourceForge (see Resources for a link to pypersyst project). Prevayler/pypersy

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.