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?

9 Years
Discussion Span
Last Post by vegaseat

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.