Integer keys in shelve

Fredrik Lundh fredrik at pythonware.com
Tue Mar 14 10:24:58 CET 2006


josegomez at gmx.net wrote:

> I want to use shelve to store lists which are indexed by an integer
> key. The keys are not concurrent or anything, they are just defined as
> integer values. However, shelve complains that I cannot have integer
> keys. Here's the error:
> >>> d[1]
> Traceback (most recent call last):
>   File "<input>", line 1, in ?
>   File "E:\Python23\lib\shelve.py", line 118, in __getitem__
>     f = StringIO(self.dict[key])
>   File "E:\Python23\lib\bsddb\__init__.py", line 116, in __getitem__
>     return self.db[key]
> TypeError: Integer keys only allowed for Recno and Queue DB's
>
> I understand that Recno is part of the bsddb3, but I don't know how to
> instruct shelve to use the Recno back-end.
>
> The other option is to str() the integer key, but I'd rather use an
> integer key, and avoid all the extra clutter.
>
> Did I miss something?

the documentation ?

    http://www.python.org/doc/current/lib/module-shelve.html

    A "shelf" is a persistent, dictionary-like object. The difference
    with "dbm" databases is that the values (not the keys!) in a
    shelf can be essentially arbitrary Python objects -- anything
    that the pickle module can handle. This includes most class
    instances, recursive data types, and objects containing lots
    of shared sub-objects. The keys are ordinary strings.

</F>






More information about the Python-list mailing list