The basic idea of persistence is simple. Suppose you have a Python program that might be a program to manage your daily backlog, and you want to save application objects (to-dos) between multiple executions of the program. In other words, you want to store objects on disk for later retrieval. That's the durability. There are several ways to achieve this, and each 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 a relational database, such as Gadfly, MySQL, PostgreSQL, or DB2. These file formats and databases are excellent, and for all of these storage mechanisms, Python has a robust interface.
These storage mechanisms have one thing in common: the stored data is independent of the objects and programs that manipulate the data. The benefit of this is that the data can be used as a shared resource for other applications. The disadvantage is that, in this way, other programs can be allowed to access the object's data, which violates the principle of object-oriented encapsulation-that is, the object's data can only be accessed through the public interface of the object itself.
also, for some applications, relational database methods may not be ideal. In particular, relational databases do not understand objects. Instead, relational databases forcibly use 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 easily be 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 an obstructive mismatch (impedence-mismatch) problem.
Serialization: Dumps (object) returns a string that contains an object in the pickle format.
Dump (object, file) writes the object to a file, which can be the actual physical file, but it can also be any file-like object that has the write () method, which can accept a single string parameter
Deserialization: Loads (string) returns the object contained in the pickle string
Load (file) returns the object contained in the pickle file
demo of Listing 1:dumps () and loads ()
- >>> import cpickle as pickle
- >>> T1 = (' This is a string ', [1, 2, 3], None)
- >>> T1
- (' This is a string ', [1, 2, 3], None)
- >>> P1 = pickle.dumps (t1)
- >>> P1
- "(S ' This is a string '/ni42/n (lp1/ni1/nai2/nai3/nantp2/n.")
- >>> print P1
- (S' This is a string '
- I42
- (LP1
- I1
- AI2
- AI3
- ANtp2
- .
- >>> t2 = pickle.loads (p1)
- >>> T2
- (' This is a string ', [1, 2, 3], None)
- >>> P2 = pickle.dumps (t1, True)
- >>> P2
- ' (U/x10this is a stringk*]q/x01 (k/x01k/x02k/x03entq/x02. ')
- >>> t3 = pickle.loads (p2)
- >>> T3
- (' This is a string ', [1, 2, 3], None)
Next, let's look at some examples that use dump () and load (), which work with objects of files and similar files. The operations of these functions are very similar to the dumps () and loads () that we have just seen, except that they have another capability-dump () function can dump several objects to the same file one after the other. The load () is then called to retrieve the objects in the same order. Listing 2 shows the practical application of this capability:
>>> a1 = ' Apple ' >>> B1 = {1: ' One ', 2: ' One ', 3: ' Three '} >>> c1 = [' Fee ', ' fie ', ' foe ', ' fum '] >>> f1 = file (' temp.pkl ', ' WB ') >>> pickle.dump (A1, F1, True) >>> Pickle.dump (B1, F1, True) >>> pickle.dump (c1, F1, True) >>> f1.close () >>> F2 = File (' temp.pkl ', ' RB ') >>> a2 = pickle.load (F2) >>> A2 ' Apple ' >>> b2 = Pickle.load (F2) >>> B2 {1: ' One ', 2: ' Both ', 3: ' Three '} >>> c2 = pickle.load (F2) >>> C2 [' Fee ', ' fie ', ' foe ', ' Fum ']
"Python" pickle module