I am getting very strange results from the following:

def list(self)
import shelve
db = shelve.open(testfile)
list = []
cnt = 0
for id in db.keys():
print db[id].info()

the testfile was written with an class defined object;
What i am finding, is if I write 50 objects, the above code prints
only 38 objects, and there are odd gaps,. like,with the name of the
objects: test1, test2, test3...test50, the print outputs
test1, test3, test5, test6, ... etc with gaps in the file names output.

But using another function, that reads the files one by one, I can read all 50 object entries, so I know they are there on the shelf. Note when I read them one by one, I supply the id,
not from the "in db.keys()" method.
I have a feeling when you list a shelved set of objects, that scanning it
via "keys" is not correct, and that is why I am getting weird results.
Actually, when you think about it, what IS the "key", for a saved object?
There is a __dict__.keys for each record, but that implies multiple keys
within one record.
Man, I am very confused....
Well, then how DO you walk through a variable length list, that you stored
via shelve?

Recommended Answers

All 2 Replies

When providing sample code, please use [code=python] (or is it [code=Python]?) and [/code] tags so as to produce accurate spacing and syntax highlighting.

That said, I don't know the answer off the top of my head. But

  1. What is that naked def doing at the front of the code?
  2. I strongly recommend you avoid using datatypes such as "list" as variable and function names. You are overriding Python's internal namespace.

Fix those and the problem may just disappear. Or not.

I am not sure if you are working correctly with the module shelve. For an example see Starting Python post:
http://www.daniweb.com/forums/post490861-110.html

Remember shelve behaves like a 'persistent to file' dictionary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.