3

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?

asked Jun 1, 2011 at 16:13

2 Answers 2

2

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).

answered Jun 1, 2011 at 16:37
Sign up to request clarification or add additional context in comments.

1 Comment

Shelve worked great for iterating through db, but using the items and values methods brought up- UnpicklingError: unpickling stack underflow. Is there any way to access dumbdbm with Python 3?
1

The purpose of these sort of simple databases is to act as key/value stores. It you want all the values, you have to iterate over all the keys. Ie:

values = [db[key] for key in db.keys()]

That will not be fast. It may me that this sort of key/value store isn't really what you need. Perhaps SQLite would be better?

That said, you can access dumbdbm under the name of dbm.dumb in Python 3.

>>> import dbm
>>> db = dbm.dumb.open('dbm', 'c')
>>> 
>>> db['modest'] = 'mouse'
>>> db['dream'] = 'theater'
>>> for key in db.keys():
... print(key)
... 
b'modest'
b'dream'
>>> for k,v in db.items():
... print(k, v)
... 
b'modest' b'mouse'
b'dream' b'theater'
>>> for val in db.values():
... print(val)
... 
b'mouse'
b'theater'
answered Jun 2, 2011 at 4:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.