Serialization and storage of Python objects in Linux programming (I)

Source: Internet
Author: User
Serialization of Linux programming stores Python objects (I)-Linux general technology-Linux programming and kernel information. For details, refer to the following section. What is persistence?

The basic idea of persistence is simple. Suppose there is a Python program, which may be a program for managing daily to-do items. You want to save the application objects (To-Do items) between multiple executions of this program ). In other words, you want to store the object on the disk for later retrieval. This is durability. There are several methods to achieve this. Each method has its own advantages and disadvantages.

For example, object data can be stored in text files of a certain format, such as CSV files. You can also use relational databases, such as Gadfly, MySQL, PostgreSQL, or DB2. These file formats and databases are excellent. For all these storage mechanisms, Python has robust interfaces.

These storage mechanisms have one thing in common: the stored data is independent of the objects and programs that operate on the data. The advantage of this is that data can be used as shared resources for other applications. The disadvantage is that, in this way, other programs can access the object data, which violates the object-oriented encapsulation principle-that is, the object data can only be accessed through the public) interface.

In addition, the relational database method may not be ideal for some applications. In particular, relational databases do not understand objects. Instead, relational databases forcibly use their own type systems and relational data models (tables). Each table contains a group of tuples (rows ), each row contains a fixed number of static fields (columns ). If the object model of the application cannot be easily converted to the relational model, it is difficult to map objects to tuples and to map them back to objects. This difficulty is often referred to as the impedence-mismatch problem.

Object Persistence

If you want to transparently store Python objects without losing their identity and type information, some form of Object serialization is required: it is a process of converting any complex object into the text or binary representation of the object. Similarly, the object must be able to be restored to the original object after being serialized. In Python, this serialization process is called pickle, which can make the object pickle into a string, a file on the disk, or any object similar to a file, you can also unpickle these strings, files, or any objects similar to files into the original objects. We will discuss pickle in detail later in this article.

Assuming that you like to save everything as an object and want to avoid converting the object into a non-Object Storage-based overhead, pickle files can provide these benefits, but sometimes it may be more robust and scalable than this simple pickle file. For example, using pickle alone cannot solve the problem of naming and searching for pickle files. In addition, it cannot support concurrent access to persistent objects. If you need these features, you need to seek help from databases such as ZODB (for the Z object database of Python. ZODB is a robust, multi-user and object-oriented database system that can store and manage any complicated Python objects and support transaction operations and concurrency control. (See references to download ZODB .) It is interesting that even ZODB relies on Python's native serialization capability, and to effectively use ZODB, you must fully understand pickle.

Another interesting way to solve persistence problems is Prevayler, which was originally implemented in Java (for more information about Prevaylor's developerWorks article, see references ). Recently, a group of Python programmers transplanted Prevayler to Python and named PyPerSyst hosted by SourceForge (for links to the PyPerSyst project, see references ). The Prevayler/PyPerSyst concept is also based on the native serialization capability of Java and Python. PyPerSyst stores the entire Object System in the memory, pickle The System Snapshot to the disk from time to time, and maintain a command log (this log can be used to re-apply the latest snapshot) to provide disaster recovery. Therefore, although applications using PyPerSyst are limited by available memory, the advantage is that the local object system can be fully loaded into the memory, which is extremely fast, in addition, it is easy to implement a database such as ZODB. ZODB allows more objects than can be maintained in the memory at the same time.

Now that we have briefly discussed various methods for storing persistent objects, we should discuss the pickle process in detail. Although we are mainly interested in exploring various ways to save Python objects without converting them into some other formats, there are still some things to be concerned about, such: how to effectively pickle and unpickle simple objects and complex objects, including custom class instances, how to maintain object references, including loop references and recursive references, and how to handle changes to class definitions, therefore, there will be no problems when using pickle instances. We will discuss all these issues in subsequent discussions about Python's pickle capabilities.

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.