An INI file parser
Project description
介绍
一个解析 ini 文件的库
特性
- API 简洁性: 属性 / 字典双模式访问
1.1 属性访问模式
1.2 字典访问模式
1.3 混合访问模式
1.4 支持 in 操作符 - 异常处理
2.1 使用异常策略(默认)
2.2 使用结果策略
2.3 使用静默策略
环境配置
- 系统: Linux/Windows
- 语言: Python 3.9+
- 依赖安装: pip install etiniconf
使用样例:
- INI文件 config.ini
[server]
host = 192.168.0.10
port = 22
- 使用样例
import etiniconf
def main(filepath):
"""
异常策略(默认):
conf = etiniconf.ConfiniParserFactory.create_with_exception_strategy(filepath)
结果策略:
conf = etiniconf.ConfiniParserFactory.create_with_result_strategy(filepath)
静默策略:
conf = etiniconf.ConfiniParserFactory.create_with_silent_strategy(filepath)
"""
# 使用静默策略
conf = etiniconf.ConfiniParserFactory.create_with_silent_strategy(filepath)
# 1. 属性访问模式
try:
host = conf.server.host
port = conf.server.port
print(f"host={host}, port={port}")
except AttributeError as e:
print(f"Error: {e}")
# 2. 字典访问模式
try:
host = conf['server']['host']
port = conf['server']['port']
print(f"host={host}, port={port}")
except etiniconf.ConfiniError as e:
print(f"Error: {e}")
# 3. 混合访问模式
try:
host = conf.server['host']
port = conf['server'].port
print(f"host={host}, port={port}")
except (AttributeError, etiniconf.ConfiniError) as e:
print(f"Error: {e}")
# 4. 支持 in 操作符
section = 'server1'
if section in conf:
print(f"The {section} does exist")
else:
print(f"The {section} does not exist")
if __name__ == '__main__':
f = 'config.ini'
main(f)
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
etiniconf-0.1.0.tar.gz
(4.0 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 etiniconf-0.1.0.tar.gz.
File metadata
- Download URL: etiniconf-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51725b57a78e2b7bed640bf9b3573a5bcd0b134483e114db9d51ace004b40111
|
|
| MD5 |
b63e864606f8b0cf4b8428ce2c5e37ea
|
|
| BLAKE2b-256 |
23b7ca67ccede55bd0055e7745f12f79d5f4215bd889cfe25891749cf58f325f
|
File details
Details for the file etiniconf-0.1.0-py3-none-any.whl.
File metadata
- Download URL: etiniconf-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59ba85d073bfeac1a8ec82bef505f1aaa8bb8f130c0a757994772376802c4c4b
|
|
| MD5 |
a3f312c85da81ec1841262649c4f983a
|
|
| BLAKE2b-256 |
8e7c654f324222fad0e6bde18dcc761c0fceaa42a690a76191c14b2facd780a9
|