Linux

Python Shelve

In previous page you have seen storing data using Python pickle. In Python shelve you access the keys randomly. Syntax for open the file using Python shelve
shelve.open(filename, flag='c' , writeback=True) 
shelve.open()
function works a lot like the file open() function The filename specified is the base filename for the underlying database. The optional flag argument must be one of these values: The filename specified is the base filename for the underlying database. The optional flag argument must be one of these values:
ValueMeaning
rOpen existing database for reading only (default)
wOpen existing database for reading and writing
cOpen database for reading and writing, creating it if it doesn’t exist
nAlways create a new, empty database, open for reading and writing

If flag is not mentioned By default, the underlying database file is opened for reading and writing which is c If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); means if you want to update or add any entry in database file use writeback =true. This can make it handier to mutate mutable entries in the persistent dictionary. Shelf.sync() Write back all entries in the cache if the shelf was opened with writeback set to True. Also empty the cache and synchronize the persistent dictionary on disk, if feasible. This is called automatically when the shelf is closed with close().
Shelf.close()
Synchronize and close the persistent dict object.

Storing Python shelve data

Let us discuss next example
import shelve
s = shelve.open("mydata.dat")
s["name"]= ["Mohit", "Ravi","Bhaskar"]
s["skill"]= ["Python", "Java","java"]
s["age"]= [27,25,25]
s.close()
Just run the above program, see one file mydata.dat will be created. from line 2 The function s = shelve.open("mydata.dat") open file mydata.dat in reading, writing and creating mode and s work like a dictionary. So the key name is paired with the value ['Mohit', 'Ravi', 'Bhaskar']. The key skill is paired with the value ['Python', 'Java', 'java'] and key age is paired with the value [27,25,25]
To make sure the file reflects all the changes to a shelf, you can invoke a shelf's sync() method. A shelf is also updated when close it with its close() method. So I did not use sync() function.

Retrieve Python shelve data

To access the data again, open the shelf and use it like a dictionary. Let us discuss next example.
import shelve
r = shelve.open("mydata.dat")
print r["skill"]
print r["age"]
print r["name"]
r.close()
Out-put
Python retrieve shelve data
retrieve Python shelve data
The above example is very easy to understand just open the database file as we have done in previous example. Print the keys. The interesting thing, you can access any key values randomly. If you don't know the key you can take advantage of for loop as shown in example below.
import shelve
r = shelve.open("mydata.dat")
for key in r:
   print r[key]
r.close()
Out-put
printing python shelve all key using for loop
printing all key using for loop

Updating the Python shelve

You can update the Shelve keys , let us discuss with example.
import shelve
s = shelve.open("mydata.dat",writeback=True)
key1 = raw_input("Enter the key\n")
val1 = int(raw_input("Enter the number of values "))
for x in range(val1):
   val = raw_input("\n Enter the value\t")
   (s[key1]).append(val)
print s[key1]
s.sync()
r.close()
Out-put
Python shelve updating the value
Updating the values
Interesting thing here is just to understand key1 key of list in file and s[key1] is the list key outside the file. Values are updated using append() method. So append() method is used with s[key1] .

Selective access of key

import shelve
r = shelve.open("mydata11.dat",writeback=True)
key1 = raw_input("Enter the key\n") print r[key1]
s.sync()
r.close()
Out-put
Selective access of key from Python shelve
Selective retrieve of the values

Similarly you can delete, extend etc. using list methods. You can do your experiments using dictionaries.





admin