Tomli 项目使用教程
tomliA lil' TOML parser项目地址:https://gitcode.com/gh_mirrors/to/tomli
1. 项目的目录结构及介绍
Tomli 项目的目录结构如下:
tomli/
├── LICENSE
├── README.md
├── pyproject.toml
├── src/
│ └── tomli/
│ ├── __init__.py
│ ├── _parser.py
│ ├── _re.py
│ └── _types.py
├── tests/
│ ├── __init__.py
│ ├── test_api.py
│ ├── test_parser.py
│ └── test_types.py
└── setup.py
目录介绍
LICENSE
: 项目的许可证文件。README.md
: 项目说明文档。pyproject.toml
: 项目配置文件。src/
: 源代码目录。tomli/
: Tomli 库的主要代码目录。__init__.py
: 初始化文件。_parser.py
: 解析器实现。_re.py
: 正则表达式相关代码。_types.py
: 类型定义。
tests/
: 测试代码目录。__init__.py
: 初始化文件。test_api.py
: API 测试代码。test_parser.py
: 解析器测试代码。test_types.py
: 类型测试代码。
setup.py
: 项目安装脚本。
2. 项目的启动文件介绍
Tomli 项目的启动文件主要是 src/tomli/__init__.py
。这个文件包含了 Tomli 库的主要入口点,提供了加载和解析 TOML 文件的接口。
# src/tomli/__init__.py
from ._parser import load, loads, TOMLDecodeError
__all__ = ("load", "loads", "TOMLDecodeError")
主要功能
load(f)
: 从文件对象加载 TOML 数据。loads(s)
: 从字符串加载 TOML 数据。TOMLDecodeError
: TOML 解析错误类。
3. 项目的配置文件介绍
Tomli 项目的配置文件是 pyproject.toml
。这个文件使用 TOML 格式,包含了项目的元数据和构建配置。
# pyproject.toml
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "tomli"
version = "2.0.1"
description = "A lil' TOML parser"
authors = [
{ name = "Taneli Hukkinen", email = "hukkin@users.noreply.github.com" }
]
license = { file = "LICENSE" }
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Typing :: Typed"
]
[tool.setuptools]
package-dir = {"" = "src"}
[tool.setuptools.packages.find]
where = ["src"]
主要配置项
[build-system]
: 构建系统配置。[project]
: 项目元数据。name
: 项目名称。version
: 项目版本。description
: 项目描述。authors
: 项目作者。license
: 项目许可证。readme
: 项目说明文档。requires-python
: 支持的 Python 版本。classifiers
: 项目分类器。
[tool.setuptools]
: setuptools 配置。package-dir
: 包目录配置。[tool.setuptools.packages.find]
: 包查找配置。
tomliA lil' TOML parser项目地址:https://gitcode.com/gh_mirrors/to/tomli