Using Shelve to Save Objects in Python

The Shelve module implements persistent storage

Business owner working at laptop in clothing shop
Hero Images/Hero Images/Getty Images

Shelve is a powerful Python module for object persistence. When you shelve an object, you must assign a key by which the object value is known. In this way, the shelve file becomes a database of stored values, any of which can be accessed at any time.

Sample Code for Shelve in Python

To shelve an object, first import the module and then assign the object value as follows:

 import shelve 
database = shelve.open(filename.suffix)
object = Object()
database['key'] = object

If you want to keep a database of stocks, for example, you could adapt the following code:

 import shelve 

stockvalues_db = shelve.open('stockvalues.db')
object_ibm = Values.ibm()
stockvalues_db['ibm'] = object_ibm

object_vmw = Values.vmw()
stockvalues_db['vmw'] = object_vmw

object_db = Values.db()
stockvalues_db['db'] = object_db

A "stock values.db" is already opened, you don't need to open it again. Rather, you can open multiple databases at a time, write to each at will, and leave Python to close them when the program terminates. You could, for example, keep a separate database of names for each symbol, appending the following to the preceding code:

 ## assuming shelve is already imported 

stocknames_db = shelve.open('stocknames.db')

objectname_ibm = Names.ibm()
stocknames_db['ibm'] = objectname_ibm

objectname_vmw = Names.vmw()
stocknames_db['vmw'] = objectname_vmw

objectname_db = Names.db()
stocknames_db['db'] = objectname_db

Note that any change in the name or suffix of the database file constitutes a different file and, therefore, a different database.

The result is a second database file containing the given values. Unlike most files written in self-styled formats, shelved databases are saved in binary form.

After the data is written to the file, it can be recalled at any time. If you want to restore the data in a later session, you re-open the file. If it is the same session, simply recall the value; shelve database files are opened in read-write mode. The following is the basic syntax for achieving this:

 import shelve 
database = shelve.open(filename.suffix)
object = database['key']

So a sample from the preceding example would read:

 import shelve 
stockname_file = shelve.open('stocknames.db')
stockname_ibm = stockname_file['ibm']
stockname_db = stockname_file['db']

Considerations With Shelve

It is important to note that the database remains open until you close it (or until the program terminates). Therefore, if you are writing a program of any size, you want to close the database after working with it. Otherwise, the entire database (not just the value you want) sits in memory and consumes computing resources.

To close a shelve file, use the following syntax:

 database.close() 

If all of the code examples above were incorporated into one program, we would have two database files open and consuming memory at this point. So, after having read the stock names in the previous example, you could then close each database in turn as follows:

 stockvalues_db.close() 
stocknames_db.close()
stockname_file.close()
Format
mla apa chicago
Your Citation
Lukaszewski, Al. "Using Shelve to Save Objects in Python." ThoughtCo, Aug. 26, 2020, thoughtco.com/using-shelve-to-save-objects-2813668. Lukaszewski, Al. (2020, August 26). Using Shelve to Save Objects in Python. Retrieved from https://www.thoughtco.com/using-shelve-to-save-objects-2813668 Lukaszewski, Al. "Using Shelve to Save Objects in Python." ThoughtCo. https://www.thoughtco.com/using-shelve-to-save-objects-2813668 (accessed April 18, 2024).