- Python Data Persistence - Discussion
- Python Data Persistence - Useful Resources
- Python Data Persistence - Quick Guide
- Data Persistence - Openpyxl Module
- Data Persistence - ZODB
- Python Data Persistence - Cassandra Driver
- Python Data Persistence - PyMongo module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - Plistlib Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - JSON Module
- Python Data Persistence - CSV Module
- Python Data Persistence - dbm Package
- Python Data Persistence - Shelve Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Pickle Module
- Python Data Persistence - Object Serialization
- File Handling with os Module
- Python Data Persistence - File API
- Python Data Persistence - Introduction
- Python Data Persistence - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Data Persistence - Shelve Module
The shelve module in Python’s standard pbrary provides simple yet effective object persistence mechanism. The shelf object defined in this module is dictionary-pke object which is persistently stored in a disk file. This creates a file similar to dbm database on UNIX pke systems.
The shelf dictionary has certain restrictions. Only string data type can be used as key in this special dictionary object, whereas any picklable Python object can be used as value.
The shelve module defines three classes as follows −
Sr.No | Shelve Module & Description |
---|---|
1 |
Shelf This is the base class for shelf implementations. It is initiapzed with dict-pke object. |
2 |
BsdDbShelf This is a subclass of Shelf class. The dict object passed to its constructor must support first(), next(), previous(), last() and set_location() methods. |
3 |
DbfilenameShelf This is also a subclass of Shelf but accepts a filename as parameter to its constructor rather than dict object. |
The open() function defined in shelve module which return a DbfilenameShelf object.
open(filename, flag= c , protocol=None, writeback=False)
The filename parameter is assigned to the database created. Default value for flag parameter is ‘c’ for read/write access. Other flags are ‘w’ (write only) ‘r’ (read only) and ‘n’ (new with read/write).
The seriapzation itself is governed by pickle protocol, default is none. Last parameter writeback parameter by default is false. If set to true, the accessed entries are cached. Every access calls sync() and close() operations, hence process may be slow.
Following code creates a database and stores dictionary entries in it.
import shelve s=shelve.open("test") s[ name ]="Ajay" s[ age ]=23 s[ marks ]=75 s.close()
This will create test.dir file in current directory and store key-value data in hashed form. The Shelf object has following methods available −
Sr.No. | Methods & Description |
---|---|
1 |
close() synchronise and close persistent dict object. |
2 |
sync() Write back all entries in the cache if shelf was opened with writeback set to True. |
3 |
get() returns value associated with key |
4 |
items() pst of tuples – each tuple is key value pair |
5 |
keys() pst of shelf keys |
6 |
pop() remove specified key and return the corresponding value. |
7 |
update() Update shelf from another dict/iterable |
8 |
values() pst of shelf values |
To access value of a particular key in shelf −
s=shelve.open( test ) print (s[ age ]) #this will print 23 s[ age ]=25 print (s.get( age )) #this will print 25 s.pop( marks ) #this will remove corresponding k-v pair
As in a built-in dictionary object, the items(), keys() and values() methods return view objects.
print (pst(s.items())) [( name , Ajay ), ( age , 25), ( marks , 75)] print (pst(s.keys())) [ name , age , marks ] print (pst(s.values())) [ Ajay , 25, 75]
To merge items of another dictionary with shelf use update() method.
d={ salary :10000, designation : manager } s.update(d) print (pst(s.items())) [( name , Ajay ), ( age , 25), ( salary , 10000), ( designation , manager )]Advertisements