A Python library for reading, writing and managing .properties configuration files with file lock support.
Project description
pr_properties
一个轻量级的 Python properties 文件处理库,支持线程安全的读写操作。
安装
pip install pr-properties
快速开始
示例 1:从文件读取并修改
from pr_properties import PropertiesHandler
# 读取配置文件
props = PropertiesHandler('config.properties')
props.read()
# 获取/设置值
value = props.get('username', 'guest')
print("username =", value)
props['new_key'] = 'new_value'
# 删除某个键
if 'password' in props.keys():
del props['password']
# 保存文件
props.write()
示例 2:直接从字符串内容加载
from pr_properties import PropertiesHandler
content = """
# 应用配置
host = localhost
port = 8080
; 数据库设置
db_user = root
db_pass = 123456
"""
props = PropertiesHandler().read_content(content)
print("host =", props['host'])
print("port =", props.get('port'))
# 修改并打印输出结果(不会写入文件)
props['db_user'] = 'admin'
print(str(props))
示例 3:遍历配置项
from pr_properties import PropertiesHandler
props = PropertiesHandler('config.properties').read()
# 遍历所有键值对
for key, value in props.items():
print(f"{key} -> {value}")
示例 4:结合默认值
from pr_properties import PropertiesHandler
props = PropertiesHandler('config.properties').read()
# 如果键不存在,则返回默认值
debug_mode = props.get('debug', 'false')
print("Debug mode:", debug_mode)
特性
- 🔒 线程安全的文件操作
- 📝 支持注释和空行
- 🔄 自动备份机制
- 🎯 字典风格的 API
基本用法
读取文件
props = PropertiesHandler()
props.read('config.properties', encoding='utf-8')
操作属性
# 获取值
host = props.get('database.host', 'localhost')
port = props['server.port']
# 设置值
props['app.name'] = 'MyApp'
props['debug'] = 'true'
# 删除
del props['old_key']
保存文件
props.write() # 自动创建 .pr_bak 备份
Properties 格式
# 注释行
database.host=localhost
database.port=5432
# 应用配置
app.name=My Application
app.debug=false
线程安全
多线程环境下为每个线程创建独立实例:
# 推荐
props = PropertiesHandler('config.properties')
# 避免在多线程中使用全局实例
from pr_properties import pr_properties # 单线程可用
依赖
- Python >= 3.7
- filelock == 3.12.2
许可证
Apache License 2.0
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
pr_properties-1.8.7.tar.gz
(72.3 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 pr_properties-1.8.7.tar.gz.
File metadata
- Download URL: pr_properties-1.8.7.tar.gz
- Upload date:
- Size: 72.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b8a451d89f7ab5670dfcd799f95c042ade8b415ca10804607cfbfbc812ea9fb
|
|
| MD5 |
df12ea5b343022e29772f58874550d7f
|
|
| BLAKE2b-256 |
d10507be533f453e5249120548b794c3b8ba3dbbb8957602d7cf50ef4b84cc0d
|
File details
Details for the file pr_properties-1.8.7-py3-none-any.whl.
File metadata
- Download URL: pr_properties-1.8.7-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8c338745a81bac3ecac300efcfd26a4ee875cd6a80c940eca744104b1107322
|
|
| MD5 |
e92ae5245c85884aaa924f3595acb55f
|
|
| BLAKE2b-256 |
5c4431284c5c6aac079e5e450635ba816578945b03ed9fc30508bad595b04ea7
|