你可能想看看我的项目。
pip install rocksdict
这是一个基于 RockDB 的快速磁盘键值存储,它可以将任何 python 对象作为值。我认为它可靠且易于使用。它的性能与 GDBM 相当,但与仅适用于 Linux 上的 python 的 GDBM 相比,它是跨平台的。
https://github.com/Congyuwang/RocksDict
下面是一个演示:
from rocksdict import Rdict, Options
path = str("./test_dict")
# create a Rdict with default options at `path`
db = Rdict(path)
db[1.0] = 1
db[1] = 1.0
db["huge integer"] = 2343546543243564534233536434567543
db["good"] = True
db["bad"] = False
db["bytes"] = b"bytes"
db["this is a list"] = [1, 2, 3]
db["store a dict"] = {0: 1}
import numpy as np
db[b"numpy"] = np.array([1, 2, 3])
import pandas as pd
db["a table"] = pd.DataFrame({"a": [1, 2], "b": [2, 1]})
# close Rdict
db.close()
# reopen Rdict from disk
db = Rdict(path)
assert db[1.0] == 1
assert db[1] == 1.0
assert db["huge integer"] == 2343546543243564534233536434567543
assert db["good"] == True
assert db["bad"] == False
assert db["bytes"] == b"bytes"
assert db["this is a list"] == [1, 2, 3]
assert db["store a dict"] == {0: 1}
assert np.all(db[b"numpy"] == np.array([1, 2, 3]))
assert np.all(db["a table"] == pd.DataFrame({"a": [1, 2], "b": [2, 1]}))
# iterate through all elements
for k, v in db.items():
print(f"{k} -> {v}")
# batch get:
print(db[["good", "bad", 1.0]])
# [True, False, 1]
# delete Rdict from dict
del db
Rdict.destroy(path)