This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Retrospective review
There are currently three modules in Python that support serialization, which we studied earlier
- Sequence and deserialize data using binary protocols – Pickle module
- Convert data to JSON format and deserialize operations – JSON module
What are the features of serialization?
-
The feature of serialization is data persistence
-
Persistence: the retention of objects after multiple execution of a program
-
Persistent performance characteristics: Everyday office work, we need to use the program and files many times, these are the performance of persistence
In this installment, we’ll look at the shelve module, the Python module that provides persistence of data keys, which internally uses the pickle protocol for data serialization
Rainy day โ๏ธ, put on earphone ๐ง, play music ๐ผ, start today’s learning journey
1. Overview of shelve module
Shelve is a simple data storage scheme. The shelf object is a persistent data store object. It is similar to a dictionary and exists in the form of key-value to facilitate the storage of Objects of Python data types.
The shelve module is implemented internally using the pickle module. Let’s take a look at their similarities and differences
The shelve module is compared to the pickle module
-
The same
(1) Used in the system
Both the pickle and shelve modules use their own serialization protocols, and the serialized data can only be recognized by Python and can only be used internally
(2) The Python version must be specified
Because the pickle module supports different functions depending on the Python version.
It’s also because the shelve module uses the pickle protocol internally.
Therefore, python2 and PYTHon3 use different sequence protocols by default, and you need to specify the version of the protocol to be compatible.
(3) Easy to serialize and deserialize custom data types
Compared to JSON, the pickle and shelve modules can serialize and deserialize custom data types directly without writing additional conversion functions or classes.
ย
-
The difference between
(1) Provide a simple and easy method
The shelve module can be seen as an upgrade to the pickle module because it uses the same serialization protocol as the pickle module, but shelve provides the open function to create and open a stored file, which is easier and more convenient than the pickle operation.
(2) Easy to read and write data
When persisting Python data to local disks, the shelve module, compared to the other two modules, can manipulate serialized data like a dict (key-value form) without having to save or read all the data at once.
Applicable scenario
(1) Use json module when interacting with external systems.
(2) Consider the pickle module when you need to persist small amounts of simple Python data to local disk files.
(3) Shelve module can be used when a large amount of Python data needs to be persistent to local disk files or some simple database-like add, delete, change and check functions are needed.
2. Common methods of the shelve module
The shelve module, while simpler than the pickle module, is still provided
methods | role |
---|---|
shelve.open(filename) | Creates an open file object to which key-value data can be written |
shelve.sync() | If Writeback is True when shelf is on, all entries in the cache are written back |
shelve.close() | Synchronize and close persistent dict objects |
๐ข Important notes:
-
Shelve. open(filename) : this is the primary method of shelve and returns a shelf object
(1) Shelf is a persistent dictionary-like object
(2) Shelf objects can be used with the with context manager
(3) Write value to shelf using key
(4) The value can be any Python object
(5) The key value must be a string
(6) The file must be of.bat type and is opened in read-write mode by default
(6) The shelve module is internally implemented by the pickle module
-
Open Flag There are four modes for opening files
model describe “r” Open an existing datastore file in read-only mode “w” Open an existing datastore file in read/write mode “c” Open a datastore file in read/write mode, or create one if it does not exist “n” Always create a new, empty datastore file and open it in read/write mode
3. Experiment
Let’s use the shelve module for serialization and deserialization:
Example of serializing data:
Import shelve with shelve.open("juejin.bat") as f: f['name']= {"Anne"," juejin "} f['day'] = [9,8,7]Copy the code
Example of deserializing data:
import shelve
f = shelve.open("juejin.bat")
existing = f['name']
f.close()
print(existing)
Copy the code
conclusion
In this installment, we looked at The shelve module, Python’s simplest serialization and deserialization of data
The shelve module provides the open method to quickly and easily create file objects for storing data and to write data in key-value format. This method is used when you need to write data locally to disks
The above is the content of this issue, welcome big guys to like comment แฆ(ยด ยท แด ยท ‘) heart, see next time ~๐๐๐