Disappearing objects in shelve

R

Ray O

I have read a number of threads relating to problems with shelve, but
I couldn't find one directly related my experience, so I would
appreciate some advice. The closest one ended with a recommendation
to upgrade to Python 2.3, but that, unfortunately, is out of my
control. The system I use runs Python 2.2.2, and whichdb gives
dbhash.

The problem only occurs when serializing class instances; lists don't
seem to be affected. Every time I attempt to read the instance from
the shelve file, it seems to delete it from the file even though I am
not performing any write operations. Attempts to use flag='r' in the
shelve.open generated error messages, as did restricting write
permissions to the database.

This is some example code. 'Editor.py' defines the class whose
instance is stored in db["john"], etc. The file, "editors.db",
originally contained keys "nick and "john". By the end, the file has
been emptied.

import shelve
import Editor

def main():
db = shelve.open("editors.db")
value = db["john"]
db.close()

db=shelve.open("editors.db")
print db.keys()
db.close()

db = shelve.open("editors.db")
value = db["nick"]
db.close()

db=shelve.open("editors.db")
print db.keys()
db.close()

main()
[ 'john','nick']
['nick']

I got the same result when I attempted a deep copy instead of the
assignment to value. Anyone know what I'm missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top