2010-08-04 106 views
2

我想在我的Windows機器上使用dbm模塊,但它目前僅在Unix上受支持。 http://docs.python.org/library/dbm.html適用於Windows的Python DBM模塊?

有誰知道類似的模塊或類似的語法或解決方案,以獲得dmb功能在Windows上的模塊?能夠訪問寫入硬盤的數據庫非常類似於我如何訪問字典的代碼,這非常棒。感謝您的幫助!

+0

你在Windows上使用cpython嗎? – 2010-08-04 20:41:58

回答

3

其實,更多的谷歌搜索周圍,我發現這一點:

http://docs.python.org/library/anydbm.html#module-anydbm

我試圖在Windows操作系統上,它似乎是工作的罰款=)

+0

如果您在Windows上使用Python 2進行基準測試,那麼當您切換到Python 3時,您將受益匪淺,因爲dbm會回退到[dumbdbm](https://docs.python.org/3/library/dbm .html#module-dbm.dumb),這是Python自己無法實現的dbm實現,而在Python 2中,它支持更快的[Berkeley DB](https://docs.python.org/2/library/dbhash的.html)。 爲了避免在Py3/Win上使用dumbdbm,我使用了[LMDB](https://github.com/dw/py-lmdb)和[semidbm](https://github.com/jamesls/semidbm) ,最後寫了我自己的解決方法,[Petite](https://github.com/h5rdly/Petite-DB) – Jay 2017-07-15 13:05:42

0

我想在Windows anydbm會只加載dumbdbm,因爲所有其他模塊只顯示爲Unix。根據Python文檔...

「的dumbdbm模塊的目的是作爲最後的手段對後備如果沒有更強大的模塊是最 anydbm模塊,dumbdbm 模塊不求速度的書面和不幾乎與其他數據庫模塊的 一樣被大量使用。「

2

基於使用Python 2.7.2的Windows 7系統上的以下測試,看起來dbhash在Windows安裝中受支持。

import os 

import anydbm 

import whichdb 

file = os.curdir + '/testdbm' # define a test file name in the current directory 

d = anydbm.open(file, 'c')  # create a new database using the test file name 

db_type = whichdb.whichdb(file) # get the dbm database type 

print(db_type)     # display the result 

'dbhash' 
1

如果Python 3的是相關的,我會去一個外部K-v的溶液,作爲dumbdbm是沒有喜悅。

一些純Python選項:

  • semidbm - 更快的替代方案,以dumbdbm,只有Python標準庫,PIP去。如果我想確保用戶的可移植性和可用性,我會選擇一個。

  • PickleDB - 使用json序列化數據。只有Standrad庫,我沒有進行基準測試,但由於序列化開銷,我懷疑它比semidbm慢。

  • Petite DB - 我自己使用Python的zipfile模塊的簡單解決方法。書中的基本測試,但它不是生產準備。

還有Python包裝到LMDBUnQLiteSQLite4 LSM,所有這些都支持Windows,儘管SQLite4綁定未測試。

後兩者是Charles Leifer,他對k-v stores和Python開發者都很瞭解(見Peewee)。

據LMDB,我試了一會兒。沒有抱怨,但它使用的是交易模式,在這裏你不能像使用其他dbm一樣使用它的字典樣式,除非你劃分/撰寫/提交拉請求等。 此外,它明確doesn't utilize compression(見also)這是什麼我感興趣。

因此,LMDB並不完全符合我的具體需求。它似乎是非常有能力的,綁定工作正常,並且安裝它們毫無庸俗之處(pip爲我工作,不需要單獨安裝LMDB或任何妨礙這種效果的麻煩)。