A simple db using toml and pickle.
Project description
TomlDB
TomlDB 是一个基于 TOML 文件的简单数据库库,它支持 JSON 和 pickle 序列化,并可以选择将大型对象存储到文件系统中。该库提供了线程安全的读写操作,确保多个进程可以安全地访问同一个数据库文件。
安装
推荐使用uv在已经初始化的项目中
uv add t4wefan-tomldb
或者
pip install t4wefan-tomldb
功能描述
功能
- 存储和检索数据:使用
set方法存储键值对,使用get方法检索数据。 - 序列化支持:自动选择
JSON或pickle序列化方法存储数据。 - 文件系统存储:对于大型或无法 JSON 序列化的对象,可以选择使用文件系统存储。
- 线程安全:使用文件锁,确保多线程和多进程的安全访问。
- 基本数据库操作:支持
set、get、delete、exists和keys操作。
使用示例
from tomldb import TomlDB
# 创建一个数据库实例,选择是否将大型对象存储到文件系统
db = TomlDB() # 默认 store_to_fs=True,无法直接原生存进toml的对象会转为pickle然后存入本地的文件
# 存储一个复杂的字典
db.set('user', {'name': 'Alice', 'age': 30, 'emails': ['alice@example.com', 'a.smith@example.com']})
# 检查键是否存在
if 'user' in db:
print('user key exists in the database.')
# 检索数据
user_data = db.get('user')
print(user_data) # 输出: {'name': 'Alice', 'age': 30, 'emails': ['alice@example.com', 'a.smith@example.com']}
# 删除数据
db.delete('user')
# 获取所有键
keys = db.keys()
print(keys) # 输出: 所有存在的键的列表
注意事项
- 如果选择将对象存储到文件系统中(
store_to_fs=True),则会自动创建一个隐藏目录,用于存储pickle文件。 - 确保对数据库文件有读写权限。
- 在多进程环境中使用时,建议使用
FileLock提供的锁机制来确保线程安全。
许可证
本项目使用 MIT 许可证。有关更多信息,请参见 LICENSE 文件。
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
t4wefan_tomldb-0.1.3.tar.gz
(4.6 kB
view details)
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 t4wefan_tomldb-0.1.3.tar.gz.
File metadata
- Download URL: t4wefan_tomldb-0.1.3.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
272317859b58b9caa78c101cc10451b6b8b3f55ee21673f85760882df608dff7
|
|
| MD5 |
ead81227f3f7fe8855f3f8fe0e5e8d1f
|
|
| BLAKE2b-256 |
3ff315e84737c59eaa288322dc62b0ef5dbb9a367340284ba2cc3e07b4acfc9b
|
File details
Details for the file t4wefan_tomldb-0.1.3-py3-none-any.whl.
File metadata
- Download URL: t4wefan_tomldb-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57eef3bfaa9a611a37369bebd448f461ce4aa9455769584ccc1dea30aea2d793
|
|
| MD5 |
b24c72f7c18e71f39709547584134428
|
|
| BLAKE2b-256 |
e0918c90bd741120080278977b1f903521d409ca775aa4ccfd189f1f408919b2
|