I'm learning about database files and the dbm module in Python 3.1.3, and am having trouble using some of the methods from the anydbm module in Python 2.
The keys method works fine,
import dbm
db = dbm.open('dbm', 'c')
db['modest'] = 'mouse'
db['dream'] = 'theater'
for key in db.keys():
print(key)
yields:
b'modest'
b'dream'
but items and values,
for k,v in db.items():
print(k, v)
for val in db.values():
print(val)
bring up an AttributeError: '_dbm.dbm' object has no attribute 'items'.
Also, this:
for key in db:
print(key)
gets a TypeError: '_dbm.dbm' object is not iterable.
Do these methods just not work in the dbm module in Python 3? If that's true, is there anything else that I could use instead?
I think this depends which implementation it chooses to use. On my system, dbm in Python 3 chooses to use ndbm, which is equivalent to the dbm
module in Python 2. When I use that module explicitly, I see the same limitations.
It appears anydbm in Python 2 chooses dumbdbm, which is slower, but does support the full dictionary interface.
You might want to look at the shelve
module, in both Python 2 and 3, which adds another layer over these interfaces (allowing you to store any pickleable object).