shelve.open call gives error

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Feb 8 17:29:59 EST 2008


En Fri, 08 Feb 2008 06:36:53 -0200, waltbrad <waltbrad at hotmail.com>  
escribió:

> Working through the Mark Lutz book Programming Python 3rd Edition.
>
> A couple of modules in the "Preview" chapter give me errors. Both on a
> shelve.open call:
>
> Pretty simple code, (2nd example):
> =====code begin=====
> import shelve
> from people import Person, Manager
>
> bob = Person('Bob Smith', 42, 30000, 'sweng')
> sue = Person('Sue Jones', 45, 40000, 'music')
> tom = Manager('Tom Doe', 50, 50000)
>
> db = shelve.open('class-shelve')
> db['bob'] = bob
> db['sue'] = sue
> db['tom'] = tom
> db.close()
> ====code end====

shelve uses the anydbm module; anydbm tries to select the best database  
module available, but apparently fails in your system. If you are just  
learning Python, I don't think it's worth trying to fix it; instead, let's  
force anydbm to use the fallback module dumbdbm (implemented in pure  
python, slow, but bullet-proof, or at least arrow-proof :) )

Add these two lines at the start of your script:

import dumbdbm, anydbm
anydbm._defaultmod = dumbdbm

Remove the class-shelve.* files, if any, before running it.

-- 
Gabriel Genellina




More information about the Python-list mailing list