Python object persistence (shelve)

PythonProgrammingServer Side Programming

The shelve module in Python’s standard library is a simple yet effective tool for persistent data storage when using a relational database solution is not required. The shelf object defined in this module is dictionary-like object which is persistently stored in a disk file. This creates afile similar to dbm database on UNIX like systems. Only string data type can be used as key in this special dictionary object, whereas any picklable object can serve as value.

The shelve module defines three classes as follows −

Sr.No.Module & Description
This is the base class for shelf implementations. It is initialized with dict-like object.
This is a subclass of Shelf class. The dict object passed to its constructor must support first(), next(), previous(), last() and set_location() methods.
This is also a subclass of Shelf but accepts a filename as parameter to its constructor rather than dict object.

Easiest way to form a Shelf object is to use 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)

Protocol parameter denotes pickle protocol 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 ="test")
s['name'] = "Ajay"
s['age'] = 23
s['marks'] = 75

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.Method & Description
synchronise and close persistent dict object.
Write back all entries in the cache if shelf was opened with writeback set to True.
returns value associated with key
list of tuples – each tuple is key value pair
list of shelf keys
remove specified key and return the corresponding value.
Update shelf from another dict/iterable
list of shelf values

To access value of a particular key in shelf.

>>> s['age']
>>> s['age']=25
>>> s.get('age')

The items(), keys() and values() methods return view objects.

>>> list(s.items())
[('name', 'Ajay'), ('age', 25), ('marks', 75)]
>>> list(s.keys())
['name', 'age', 'marks']
>>> list(s.values())
['Ajay', 25, 75]

To remove a key-value pair from shelf

>>> s.pop('marks')
>>> list(s.items())
[('name', 'Ajay'), ('age', 25)]

Notice that key-value pair of marks-75 has been removed.

To merge items of another dictionary with shelf use update() method

>>> d={'salary':10000, 'designation':'manager'}
>>> s.update(d)
>>> list(s.items())
[('name', 'Ajay'), ('age', 25), ('salary', 10000), ('designation', 'manager')]

In this article we learned about shelve module which provides convenient mechanism for storing persistent dictionary object.

Published on 07-Dec-2018 06:21:32