问题
I would like to use the dbm module on my Windows machine, but it is currently only supported on Unix. http://docs.python.org/library/dbm.html
Does anyone know of a similar module with similar syntax or a workaround to get dmb functional on windows? Being able to access a database written to the hard drive much like how I code to access a dictionary would be great. Thank you for your help!
回答1:
Actually, after more googling around, I found this:
http://docs.python.org/library/anydbm.html#module-anydbm
I've tried this on windows and it seems to be working fine =)
回答2:
Based on the following test on a Windows 7 system using Python 2.7.2 it appears that dbhash is supported on Windows instalations.
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'
回答3:
If Python 3 is of relevance, I'd go for an external k-v solution, as dumbdbm is no joy.
Some pure Python options:
semidbm - A faster alternative to dumbdbm, Python standard library only, pip and go. The one I'd go for if I want to ensure portability and availability to users.
PickleDB - Uses json to serialize data. Standrad library only, I haven't benchmarked but I suspect it's slower than semidbm because of the serialization overhead.
Petite DB - My own simple workaround using Python's zipfile module. Basic testing in the books but it's not production ready.
There are also Python wrappers to LMDB, UnQLite and SQLite4 LSM, all of which support Windows, though the SQLite4 bindings weren't tested.
The latter two are by Charles Leifer, who is both savvy with k-v stores and an avid Python developer (see Peewee).
As far as LMDB, I've tried it for a while. No complaints, but it uses a transactional model, where you can't use it dictionary-style like with other dbm's, unless you subclass/compose/submit a pull request etc. Also, it explicitly doesn't utilize compression (see also) which was something I was interested in.
So LMDB just didn't quite fit my specific needs. It does seem to be highly capable, the bindings worked fine, and installing them was untroublesome (pip worked for me, had no need to install LMDB seperately or any nuisance to that effect).
回答4:
I think anydbm on Windows will only load dumbdbm, since all the other modules appear to be Unix only. According to the Python documentation...
"The dumbdbm module is intended as a last resort fallback for the anydbm module when no more robust module is available. The dumbdbm module is not written for speed and is not nearly as heavily used as the other database modules."
来源:https://stackoverflow.com/questions/3408272/python-dbm-module-for-windows