[Tutor] Alternative for Shelve

Timo timomlists at gmail.com
Tue May 12 18:21:11 CEST 2009


Kent Johnson schreef:
> On Tue, May 12, 2009 at 3:59 AM, Timo <timomlists at gmail.com> wrote:
>   
>> Alan Gauld schreef:
>>     
>>> "Timo" <timomlists at gmail.com> wrote
>>>
>>>       
>>>> I have an issue with the Shelve module. It works great for my needs, the
>>>> only problem is that a file made with Shelve isn't interchangable between
>>>> different computers.
>>>>         
>>> I thought it would be.
>>> What kind of computers are you having issues with?
>>> And what kind of issues?
>>>       
>> Currently only tested between Ubuntu and Windows on the same computer.
>> This is the end of the traceback:
>> bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- </path/to/shelvefile>:
>> unsupported hash version: 9')
>>     
>
> shelve relies on the anydbm module for storage. anydbm may use any one
> of several actual database implementations (dbhash, gdbm, dbm or
> dumbdbm) depending on what is available. I guess you have different
> modules available on the two machines. You can find out which module
> is being used like this:
>
> In [7]: import anydbm
> In [8]: anydbm._defaultmod
> Out[8]: <module 'dbhash' from
> '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/dbhash.pyc'>
>   
I get the same result, only I have Python 2.6 on my Ubuntu installation 
and 2.5 on Windows. Maybe that is the problem. But still, then the user 
can't upgrade without breaking their database.

Also tried:
 >>> import whichdb
 >>> whichdb.whichdb('mydb.db')
'dbhash'

Same result on both.

Timo

> You could force the use of a common db module by opening the file
> explicitly. For example, to use dumbdbm, which is slow, simple and
> guaranteed to be available, something like this (untested) should
> work:
>
> import dumbdbm
> from shelve import Shelf
>
> shelf = Shelf(dumbdbm.open('myfile', 'c'))
>
> Kent
>   



More information about the Tutor mailing list