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?

Dani AI

Generated

Clarification first: a shelve is just a persistent dictionary — the "key" is the string you used when you assigned the object (for example db['test1'] = myobj), not the object's __dict__ keys. So if you stored 50 objects under 50 distinct string keys you should be able to iterate every stored key like you would a normal dict. was right about that behavior.

A few concrete fixes and checks (also echoing : don’t shadow built-ins). Avoid using names like list or id for your variables. Take a stable snapshot of keys before walking the shelf and inspect the raw key values so hidden characters don’t hide entries:

keys = list(db.keys())        # make a snapshot
for k in sorted(keys):
    print(repr(k), db[k].info())

Troubleshooting checklist (do these in order):

  • Compare counts: len(keys) vs len(set(keys)) to find duplicates.
  • Print repr(key) and type(key) to reveal leading/trailing whitespace, strange encodings, or non-string keys.
  • Re-open the shelf in a fresh interpreter after writing to confirm the file actually contains all items (this rules out caching or process concurrency issues).
  • Ensure writes were flushed: call db.sync() or db.close() after writing. If you mutated objects in-place when saving, either reassign them back to db[key] or use writeback=True.
  • If the shelf is shared by multiple processes, different DBM backends can behave differently; note the Python version and backend if deeper help is needed.

Those steps will expose whether keys were never written, were overwritten, or are simply hidden by whitespace/encoding issues. If a detailed reproducible snippet is available (Python version + exact store code + platform), it becomes straightforward to pinpoint the root cause.

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.