using shelve correctly?

B

Blair Hall

I am having trouble understanding the correct use of 'shelve'.

I have a number of small programs that read and write to a single
shelve file (not multithreading though).

It seems that by writing to an existing entry I
sometimes corrupt the database. For instance, I get the problem
when a Python script has opened the file and read and written a few
objects it then tries to overwrite an existing entry:

cf = {}
FILE['correction factor'] = cf
FILE.sync()

The program then closes the file and terminates. Now if I go
back and look at the keys to the data base I find that
'correction factor' is no more and instead there is apparently
'correction factorr' (repetetion of last letter), however is
I try to use that key to access the data I get a KeyError.

I get the same pattern of corrupted key with other entries too (last
letter repeats).

It appears that by deleteing the entry before hand

del FILE['correction factor']
FILE['correction factor'] = cf

the problem does not occur.
But I didn't think that this should be necessary.

Am I using shelve incorectly?
 

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,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top