Shelve is a very powerful Python module for object persistence. When you shelve an object, you must give 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.
To shelve an object, one first imports the module and then assigns the object value as follows:
import shelve
database = shelve.open(filename.suffix)
object = Object()
database['key'] = object
If one wanted to keep a database of stocks, one could adapt the following code:
import shelveAs "stockvalues.db" is already opened, one need not open it again. Rather, one can open multiple databases at a time, write to each at will, and leave Python to close them when the program terminates. One could, for example, keep a separate database of names for each symbol, appending the following to the preceding code:
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
## assuming shelve is already importedNote that any change in the name or suffix of the database file constitutes a different file and, therefore, a different database.
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
The result will be a second database file containing the given values. Unlike most files written in self-styled formats, shelved databases are saved in binary form.
Once the data is written to the file, it can be recalled at anytime. Naturally, if you want to restore the data in a later session, you will need to re-open the file. If it is the same session, simply re-call 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 our preceding example would read:
import shelve
stockname_file = shelve.open('stocknames.db')
stockname_ibm = stockname_file['ibm']
stockname_db = stockname_file['db']
It is very important to note that the database will remain open until you close it (or until the program terminates). Therefore, if you are writing a program of any size, you will want to close the database after working with it. Otherwise, the entire database (not just the value you want) will sit in memory (RAM) and consume 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 RAM at this point. So, after having read the stock names in the previous example, one would then close each database in turn as follows:
stockvalues_db.close()
stocknames_db.close()
stockname_file.close()
