Python object persistence (shelve)



Python Object Persistence 

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 a dictionary-like object persistently stored in a disk file. This creates a file similar to dbm database on UNIX-like systems. The only string data type can be used as a key in this special dictionary object, whereas any pickable object can serve as value.

The shelve module defines three classes as follows ?

Sr.No. Module & Description
1 Shelf
This is the base class for shelf implementations. It is initialized with dict-like object.
2 BsdDbShelf 
This is a subclass of the 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 a parameter to its constructor rather than dict object.

The easiest way to form a Shelf object is to use the open() function defined in the shelve module which returns a DbfilenameShelf object.

open(filename, flag = 'c', protocol=None, writeback = False)

Here,

  • The filename parameter is assigned to the database created. 
  • The default value for the flag parameter is ?c' for read/write access. Other flags are ?w' (write only) ?r' (read-only) and ?n' (new with read/write). 
  • The protocol parameter denotes that the 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, the process may be slow.

The 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 a test.dir file in the current directory and store key-value data in hashed form. The Shelf object has the following methods available ?

Sr.No. Method & Description
1 close()
synchronize and close persistent dict objects.
2 sync()
Write back all entries in the cache if the shelf was opened with writeback set to True.
3 get()
returns value associated with a key
4 items()
list of tuples - each tuple is key-value pair
5 keys()
list of shelf keys
6 pop()
remove the specified key and return the corresponding value.
7 update()
Update shelf from another dict/iterable
8 values()
list of shelf values

To access the value of a particular key on the shelf.

>>> s=shelve.open('test')
>>> s['age']
23
>>> s['age']=25
>>> s.get('age')
25

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 the shelf.

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

Notice that the key-value pair of marks-75 has been removed. To merge items of another dictionary with a shelf use the 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 the shelve module which provides a convenient mechanism for storing persistent dictionary objects.

Updated on: 2025-01-22T13:49:50+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements