[Tutor] How does Shelve work?

Ibraheem Umaru-Mohammed umarumohammed@btinternet.com
Fri, 6 Sep 2002 12:18:06 +0100


>>>> S == "S A <buc40@bemail.org>" writes:
S>> Can someone please explain how the Shelve module works?
S>> 
S>> I tried to use a shelve.open (filename) command on a blank flat 
S>> file and got an error saying that it could not determine the database 
S>> type?
S>> 
S>> Traceback (most recent call last):
S>>   File "<stdin>", line 1, in ?
S>>   File "/sw/lib/python2.2/shelve.py", line 158, in open
S>>     return DbfilenameShelf(filename, flag)
S>>   File "/sw/lib/python2.2/shelve.py", line 148, in __init__
S>>     Shelf.__init__(self, anydbm.open(filename, flag))
S>>   File "/sw/lib/python2.2/anydbm.py", line 83, in open
S>>     raise error, "db type could not be determined"
S>> anydbm.error: db type could not be determined
S>> >>> d = shelve.open('test2')
S>> Traceback (most recent call last):
S>>   File "<stdin>", line 1, in ?
S>>   File "/sw/lib/python2.2/shelve.py", line 158, in open
S>>     return DbfilenameShelf(filename, flag)
S>>   File "/sw/lib/python2.2/shelve.py", line 148, in __init__
S>>     Shelf.__init__(self, anydbm.open(filename, flag))
S>>   File "/sw/lib/python2.2/anydbm.py", line 83, in open
S>>     raise error, "db type could not be determined"
S>> anydbm.error: db type could not be determined
S>> 
S>> Is this saying that it can not find a database to run on my computer or 
S>> that the file is not of a database type? Please excuse the ignorance, I'm 
S>> just now dabbling in databases.
S>> 
S>> Thanks.
S>> SA
S>> 

Does the following help at all?

,---- [ shelve module example - foo.py ]
| #!/usr/bin/env python
| import shelve
|
| handle = shelve.open("mydb")
| # save objects
| handle[1]="one"
| handle[2]="two"
| handle[3]="three"
| handle[4]="four"
| handle[5]="five"
|
| # retrieve objects
| print "Stored the following to database: "
| for item in handle.keys():
|   print item, "=>", handle[item]
| handle.close()
`----

You may also want to look at the following, although it wasn't all that helpful:

,---- [ shelve module URL ]
| http://www.python.org/doc/current/lib/module-shelve.html
`----


Kindest regards,

				--ibz.

-- 
				Ibraheem Umaru-Mohammed 
					"ibz"
			umarumohammed (at) btinternet (dot) com