基于dataclass的科研配置文件取用工具
Project description
Conf Root
基于Pydantic的科研配置文件取用工具。主要想法是在大量进行实验时,参数经常变动而且难以记录。最好有一种方式能在人类能读懂的配置文件中写出所有的参数,这样每次运行的记录都相对完整且易于整理。库的主要功能如下:
- 将您定义的类转化为Pydantic Model,并生成配置文件,可以选择使用配置文件优先或输入参数优先地作为类变量的值。
- 将某些科研项目中的argparse转换为Pydantic Model,进而生成配置文件;从而可以在配置文件中修改输入。
- 提供脚本
conf-root-web。提供一个web界面,允许您可视化修改类定义的配置文件(如需使用此命令需安装额外依赖pip install conf_root[web])
安装方式如下。
pip install conf_root
Note: 仅用于配置文件,不提倡在配置文件类中使用动态的变量和方法。
装饰类的定义生成配置文件
项目使用Pydantic Model对配置类进行封装,并产生配置文件。
note: 类定义的字段需要类型注解。请参阅 pydantic 的文档。
下面是一个对AppConfig进行封装
from conf_root import ConfRoot
from pydantic import BaseModel
# @ConfRoot().config(filename='config')
# @ConfRoot().config
# 这个装饰器也支持上面这两种调用方式
@ConfRoot().config('config')
class AppConfig(BaseModel):
database_host: str = 'localhost'
database_port: int = 5432
# 在类实例化时,检测是否存在配置文件(文件名为name+后缀)。
# 如不存在则新建文件。
# 如存在配置文件,则加载文件中的配置。
app_config = AppConfig()
参数解释
ConfRoot.init(agent_class: Optional[Type[BasicAgent]] = YamlAgent, priority: Literal['file', 'param'] = 'file')
- agent_class 为配置存储的形式。当前支持JsonAgent/YamlAgent/SingleFileYamlAgent。
- 对于存储到多个文件的agent(JsonAgent、YamlAgent)。存储到由参数指定的路径中,将覆盖原有结果。
- 对于存储到单个文件的agent(SingleFileYamlAgent)。存储到由参数指定的同一路径中时,将根据类的名称存取其中的数据。
- 如果指定为None,可以不产生配置文件存储。
- 可以自行对BasicAgent进行拓展以适配更多类型的序列化方式。
- priority 为加载时的优先级模式。在加载时将同时考虑配置文件和类的初始化参数。默认为
file,即以配置文件中的值优先加载到类中。param即以输入的值为优先加载到类中。
ConfRoot.config
可以使用不同方式调用。详见上方示例,其主要参数为 filename;支持不同的调用方式。
- filename。保存的文件名。默认时为根据
{cls.__qualname__}产生的文件名。
config 函数将为原类型添加特殊的类变量,这允许动态地修改配置类的读写行为,实现如保存到另一个路径等功能,详情请参考 examples/ 中的示例。
__CONF_LOCATION__: 用于修改配置文件的路径。__CONF_AGENT__: 用于修改Agent对象。class_name: 返回在(Yaml)配置文件中类的名称。
__CONF_ROOT__: 用于修改ConfRoot对象。
config 还会在原类型的基础上添加 save_configuration 方法,方便直接写入配置文件。
对于无法序列化的 type 和 function 类型,将会给出警告并跳过而不会直接报错。
嵌套,自定义序列化,自定义验证器作为pydantic功能的一部分,可以参见pydantic的文档。
解析 Argparse
在科研项目中会出现一大堆parser.argument,仅需添加两行代码就可以将其命令行参数配置转换为配置文件,并在配置文件中剪辑参数。
这样保存和切换不同的配置文件即可运行不同的版本。 不必重复输入一长串的命令行参数,也不再需要专门的run.sh或者run.bat。
import argparse
from conf_root import ConfRoot
# 科研项目经常出现一大堆parser.argument
parser = argparse.ArgumentParser()
parser.add_argument("--dataSet", type=str, default="wiki", help="cora, citeseer, wiki, corafull, FedDBLP")
# ...
parser.add_argument("--dropout", type=float, default=0.5)
args = parser.parse_args()
# 解析Argparse并转换为pydantic Model,并且用ConfRoot.config封装它
# cls_name 的默认值为 'argparse'
ArgsClass = ConfRoot().from_argparse(parser, cls_name='argparse')
# 之后可以使用以下代码,创建并使用配置文件。
args_model = ArgsClass(**vars(args))
参数解释
ConfRoot.from_argparse(parser: argparse.ArgumentParser, cls_name: str = 'ArgparseConfig', *args, **kwargs)
解析Argparse并转换为pydantic Model,并且用ConfRoot.config封装它
- paser。需解析的argparse.ArgumentParser。
- cls_name。产生的Model类的类名。默认值为ArgparseConfig。
- *args, **kwargs。其余参数将被作为ConfRoot.config 的
from_argparse 目前仅支持常见的官方Argparse动作,但是对于自定义的Action无法做到转换。这不意味着有不支持的命令行参数会导致报错,这些参数将会被忽略跳过。
More Example
参见 examples/*。
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 conf_root-0.5.1.tar.gz.
File metadata
- Download URL: conf_root-0.5.1.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d79b57a3ababa1168f18c4756d226dd7b763e8b57219f3c93d4c9d561b5f8e6c
|
|
| MD5 |
bf92ca7d7ae66f311e8938dee7e0d712
|
|
| BLAKE2b-256 |
e07fb95687982f3022bd89f4653e20b555a8bda6bbaaf5d17773fa4b0b39b7d0
|
File details
Details for the file conf_root-0.5.1-py3-none-any.whl.
File metadata
- Download URL: conf_root-0.5.1-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8ec98276856605f361267bd9509431b45f260862dd1215788d1621b9e24a33c
|
|
| MD5 |
1939ae0def20edc298c96e54069826ac
|
|
| BLAKE2b-256 |
ba88075bb6d4cab9ff3ac36286f6046a14fe9937183fd767799ce4f9d0ee6388
|