Cross-platform LevelDB interface for Python
Project description
duck-leveldb
Cross-platform LevelDB 接口,自动选择可用后端:plyvel(优先)→ ctypes + leveldb.dll(回退)。
Windows / Linux / macOS 均可使用,无需手动配置后端。
安装
pip install duck-leveldb
Windows 下 ctypes 模式需要
leveldb.dll,已内置在包中。如果不想内置,可通过LEVELDB_DLL环境变量指定路径。
快速开始
from duck_leveldb import LevelDB
with LevelDB("/tmp/my.db", create=True) as db:
db.put(b"hello", b"world")
print(db.get(b"hello")) # b"world"
db.delete(b"hello")
print(db.get(b"hello")) # None
API
LevelDB
| 方法 | 说明 |
|---|---|
LevelDB(path, create=True) |
打开/创建数据库 |
.get(key) -> bytes | None |
读取 |
.put(key, value) |
写入 |
.delete(key) |
删除 |
.iterator() -> Iterator |
创建迭代器 |
.close() |
关闭 |
支持 with 上下文管理器 |
Iterator
with db.iterator() as it:
# 正向遍历
for k, v in it:
print(k, v)
# 反向遍历
it.seek_to_last()
while it.valid():
print(it.key(), it.value())
it.prev()
# 定位到指定 key
it.seek(b"target")
| 方法 | 说明 |
|---|---|
.seek_to_first() |
跳转到第一个 key |
.seek_to_last() |
跳转到最后一个 key |
.seek(key) |
跳转到 >= key 的位置 |
.next() / .prev() |
前进 / 后退 |
.valid() -> bool |
当前位置是否有效 |
.key() -> bytes |
当前 key |
.value() -> bytes |
当前 value |
.close() |
关闭 |
支持 with 上下文管理器,支持 for k, v in iterator |
后端
| 后端 | 条件 | 说明 |
|---|---|---|
| plyvel | import plyvel 成功 |
性能好,推荐 |
| ctypes | 回退 | 内置 leveldb.dll,Windows 即开即用 |
开发
# 测试
python scripts/run_tests.py
# 仅测试,跳过覆盖率
python scripts/run_tests.py --no-cov
# 按关键字筛选
python scripts/run_tests.py -k iterator
# 打包
python scripts/publish.py --build-only
# 发布到 PyPI
python scripts/publish.py
# 发布到 TestPyPI
python scripts/publish.py --repo testpypi
发布前需在 scripts/pypi_config.toml 中填入 PyPI token(参考 scripts/pypi_config.toml.example)。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file duck_leveldb-1.0.0.post20260620.tar.gz.
File metadata
- Download URL: duck_leveldb-1.0.0.post20260620.tar.gz
- Upload date:
- Size: 340.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03bb84b07277e522daf3ba01b84bcab1c9dc5457e294c680a4b6f9b1eecc9b9d
|
|
| MD5 |
2c8b28917abc2b40084664692d102642
|
|
| BLAKE2b-256 |
f6eef2e6ef62924f002ae4fe428ed3f43de7196ce45d307340c1a2210a3458d5
|
File details
Details for the file duck_leveldb-1.0.0.post20260620-py3-none-any.whl.
File metadata
- Download URL: duck_leveldb-1.0.0.post20260620-py3-none-any.whl
- Upload date:
- Size: 339.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d3052276efeea97d3a16004b50f6b0fd08efe0fa609538e8ed078cf705bc65a
|
|
| MD5 |
01f2f5cace6a5bdccb801d1dc7b3a628
|
|
| BLAKE2b-256 |
1e2c8cef78915c837ca47c237f01f7699a117aa9808abe4ff68f114fcba85d0f
|