Mailing List Archive

Shelve-problems
Hi,

I try to do something like :

>>>import shelve
>>>db = shelve.open('2')
>>> db['1'] = 'test'

And quit python, restart it and this happens when I try to read the data
back in :

>>> import shelve
>>> db = shelve.open('2')
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/usr/lib/python1.5/shelve.py", line 152, in open
return DbfilenameShelf(filename, flag)
File "/usr/lib/python1.5/shelve.py", line 142, in __init__
Shelf.__init__(self, anydbm.open(filename, flag))
File "/usr/lib/python1.5/anydbm.py", line 83, in open
raise error, "db type could not be determined"
anydbm.error: db type could not be determined
>>>

Why?!

I am using Linux Suse 6.2. Could I ( my Linux distro.) be missing
something??

Thomas Weholt
Shelve-problems [ In reply to ]
Thomas Weholt wrote:

> Hi,
>
> I try to do something like :
>
> >>>import shelve
> >>>db = shelve.open('2')
> >>> db['1'] = 'test'
>
> And quit python, restart it and this happens when I try to read the data
> back in :
>
> >>> import shelve
> >>> db = shelve.open('2')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python1.5/shelve.py", line 152, in open
> return DbfilenameShelf(filename, flag)
> File "/usr/lib/python1.5/shelve.py", line 142, in __init__
> Shelf.__init__(self, anydbm.open(filename, flag))
> File "/usr/lib/python1.5/anydbm.py", line 83, in open
> raise error, "db type could not be determined"
> anydbm.error: db type could not be determined
> >>>
>
> Why?!
>
> I am using Linux Suse 6.2. Could I ( my Linux distro.) be missing
> something??
>
> Thomas Weholt

The Suse 6.2 distro does not have dbm built in. Recompile Python but first
edit the Modules/Setup file to allow gdbm to be compiled in as a shared
library. Shelve will work but I have not been able to build Tkinter
correctly this way. So I use /usr/bin/python for Tkinter apps and
/usr/local/bin/python for shelve apps.

John Lloyd
Shelve-problems [ In reply to ]
Thomas Weholt <thomas@bibsyst.no> wrote:
> I try to do something like :
>
> >>>import shelve
> >>>db = shelve.open('2')
> >>> db['1'] = 'test'
>
> And quit python, restart it and this happens when I try to read the data
> back in :
>
> >>> import shelve
> >>> db = shelve.open('2')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python1.5/shelve.py", line 152, in open
> return DbfilenameShelf(filename, flag)
> File "/usr/lib/python1.5/shelve.py", line 142, in __init__
> Shelf.__init__(self, anydbm.open(filename, flag))
> File "/usr/lib/python1.5/anydbm.py", line 83, in open
> raise error, "db type could not be determined"
> anydbm.error: db type could not be determined
> >>>
>
> Why?!

not sure -- two possible causes comes to mind:

1) you didn't close the database. I have a vague
memory that one of the database drivers won't
properly flush changes to the file if you don't
(cannot remember which one, though).

2) you don't have any database handlers on your
machine, so Python falls back to the "dumbdbm"
driver. and while "anydbm" can fall back on the
"dumbdbm" module when creating a database, it
fails to recognize it when you reopen it...

(guess someone should fix "whichdb"...)

one way around this is to use shelve with an
explicit database, like this:

import shelve
import gdbm

def gdbm_shelve(filename, flag="c"):
return shelve.Shelf(gdbm.open(filename, flag))

db = gdbm_shelve("dbfile")

(taken from the eff-bot library guide, see below).

</F>

<!-- coming monday:
http://www.pythonware.com/people/fredrik/librarybook.htm
(the eff-bot guide to) the standard python library. -->