Object persistence
01 persistence Overview
And SEQ ID NO deserialization
The data stored in the hard disk memory think that is persistent, for example the game midway state storage to save the next play
02 use formatted text
Flat file is a text file, save the text that is the original intention of the text.
Some objects or data structures have some type of running in memory, such as a list of tuples dictionary table, stored text into plain text, loaded again when memory needs to deserialize
Write text text
Read out the need to deserialize list data structure
Read out to a temporary variable lst
Eval pass the brackets expression string into the python
03 Pickle
If the structure is more complex, with a text in this way is inappropriate, the text itself is stored in the text, it is recommended to use different persistence technologies
Pickle is a module designed for the serialization and deserialization
Note: When the character string processing method pickle loads where the string s is non-complex
Examples: The person sequences into the dictionary string, then the string object dictionary deserialize
Example: The person dictionary serialized as text, then the text will deserialize dictionary
Note: The method of processing text using a pickle load, different from the character string processing loads
04 shelve
Pickle applicable to serialize an object, when a plurality of objects into a file, the pickle will process a plurality of objects as a target, it is necessary to apply shelve.
Shelve wherein: the plurality of objects may be stored in a file, similar to the key value to distinguish a plurality of objects
Example: a list a dict
Which shelve.open directly create a binary file store our information
Then into two objects
Then len View shelve the file structure has several objects, the result is two
So, how to read the information inside it
Delete an object
Custom type of serialization and deserialization
Define a custom class
Write custom object - with close attention to the last close the file
Read custom objects - with close attention to the last close the file