5

The shelve module is implemented on top of the anydbm module. This module acts as a façade for 4 different specific DBM implementations, and it will pick the first module available when creating a new database, in the following order:

  • dbhash (deprecated but still the first anydbm choice). This is a proxy for the bsddb module, .open() is really bsddb.hashopen()

  • gdbm, Python module for the GNU DBM library, offering more functionality than the dbm module can offer when used with this same library.

  • dbm, a proxy module using either the ndbm, BSD DB and GNU DBM libraries (chosen when Python is compiled).

  • dumbdbm, a pure-python implementation.

But in my system although I have dbhash for some reason I want it to create the db just with the dumbdbm.

How can I achieve that?

Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
Ali_IT
  • 7,551
  • 8
  • 28
  • 44
  • Out of interest, why do you want to do this? – Gareth Latty Apr 26 '13 at 22:40
  • I'm trying to resemble a system. The base system creates a db file which uses dumbdbm and then uses that file as an input to another program. If I use any other db I have to change the other program to read the new file properly and we are trying to not change the code as far as we can. – Ali_IT Apr 26 '13 at 22:47
  • Only dumbdbm is available on Windows. If you want to move 'shelve' files between Linux and Windows, they have to use the dumbdbm format. – Åsmund Feb 07 '17 at 11:26

1 Answers1

6

You cannot control what db module shelve.open uses, but there are workarounds.

The best is usually to create the db yourself and pass it to the Shelf constructor manually, instead of calling shelve.open:

db = dumbdbm.open('mydb')
shelf = shelve.Shelf(db)

The first parameter is any object that provides a dict-like interface that can store strings, which is exactly what any *dbm object is.

abarnert
  • 354,177
  • 51
  • 601
  • 671