如何强制python使用dumbdbm模块创建新数据库?

时间:2022-10-30 23:17:26

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:

搁置模块在anydbm模块的顶部实现。此模块充当4个不同特定DBM实现的外观,它将按以下顺序选择创建新数据库时可用的第一个模块:

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

    dbhash(已弃用但仍是第一个anydbm选项)。这是bsddb模块的代理,.open()实际上是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.

    gdbm,GNU DBM库的Python模块,提供比dbm模块在与同一个库一起使用时提供的更多功能。

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

    dbm,一个使用ndbm,BSD DB和GNU DBM库的代理模块(在编译Python时选择)。

  • dumbdbm, a pure-python implementation.

    dumbdbm,一个纯python实现。

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

但是在我的系统中,虽然由于某些原因我有dbhash,但我希望它只使用dumbdbm创建数据库。

How can I achieve that?

我怎样才能做到这一点?

1 个解决方案

#1


5  

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

您无法控制shelve.open使用的db模块,但有一些解决方法。

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

最好的方法是自己创建数据库并手动将其传递给Shelf构造函数,而不是调用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.

第一个参数是提供类似dict的接口的任何对象,它可以存储字符串,这正是任何* dbm对象。

#1


5  

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

您无法控制shelve.open使用的db模块,但有一些解决方法。

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

最好的方法是自己创建数据库并手动将其传递给Shelf构造函数,而不是调用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.

第一个参数是提供类似dict的接口的任何对象,它可以存储字符串,这正是任何* dbm对象。