Skip to main content

使用 yaml 文件轻松配置应用程序

项目描述

易配置

测试状态 更新 PyPI - Python 版本 派皮 下载

使用 yaml 文件轻松配置应用程序

描述

Easyconfig 简化了(小型)应用程序的配置管理。

配置文件的验证和解析是通过pydantic 和 easyconfig 构建的。可以使用所有 pydantic 功能和模型功能,因此应该涵盖每个奇异的用例。如果您以前使用过 pydantic,您应该会感到宾至如归

为什么不是 pydantic 设置

一个 pydantic 设置对象是一个非可变对象。使用 easyconfig,您可以创建全局配置并将其导入到您的模块中。当您的应用程序启动时,您可以从设置文件中读取配置,并且对象值将相应地更改值。

此外,easyconfig 可以使用 pydantic 模型的指定默认值和注释创建默认配置文件。这样用户就可以得到一些指导如何改变程序的行为。

用法

像以前一样创建模型。然后将模型的一个实例传递给 easyconfig 函数。它将从具有相同值的模型中创建一个可变对象。

Easyconfig 还提供了一些 mixin 类,因此您可以为文件加载函数提供类型提示。这些 mixin 不是必需的,它们只是在 IDE 中提供类型提示。

为方便起见,您还可以 importAppBaseModelBaseModelfrom ,easyconfig因此您不必自己从 mixins 继承。

简单的例子

from pydantic import BaseModel
from easyconfig import AppConfigMixin, create_app_config


class MySimpleAppConfig(BaseModel, AppConfigMixin):
    retries: int = 5
    url: str = 'localhost'
    port: int = 443


# Create a global variable which then can be used throughout your code
CONFIG = create_app_config(MySimpleAppConfig())

# Use with type hints and auto complete
print(CONFIG.port)

# Load configuration file from disk.
# If the file does not exist it will be created
# Loading will also change all values of CONFIG accordingly
CONFIG.load_config_file('/my/configuration/file.yml')

创建的配置文件:

retries: 5
url: localhost
port: 443

嵌套示例

带有来自easyconfig的便利基类的嵌套示例。

from pydantic import Field
from easyconfig import AppBaseModel, BaseModel, create_app_config


class HttpConfig(BaseModel):
    retries: int = 5
    url: str = 'localhost'
    port: int = 443


class MyAppSimpleConfig(AppBaseModel):
    run_at: int = Field(12, alias='run at')  # use alias to load from/create a different key
    http: HttpConfig = HttpConfig()


CONFIG = create_app_config(MyAppSimpleConfig())
CONFIG.load_config_file('/my/configuration/file.yml')

创建的配置文件:

run at: 12
http:
  retries: 5
  url: localhost
  port: 443

注释

可以通过 pydantic 指定描述Field。描述将在 .yml 文件中创建为注释

from pydantic import Field
from easyconfig import AppBaseModel, create_app_config


class MySimpleAppConfig(AppBaseModel):
    retries: int = Field(5, description='Amount of retries on error')
    url: str = 'localhost'
    port: int = 443


CONFIG = create_app_config(MySimpleAppConfig())
CONFIG.load_config_file('/my/configuration/file.yml')

创建的配置文件:

retries: 5  # Amount of retries on error
url: localhost
port: 443

回调

可以注册回调,这些回调将在值更改或第一次加载配置时执行。如果应用程序允许动态重新加载配置文件(例如通过文件观察器),这是一个有用的功能。

from easyconfig import AppBaseModel, create_app_config


class MySimpleAppConfig(AppBaseModel):
    retries: int = 5
    url: str = 'localhost'
    port: int = 443


def setup_http():
    # some internal function
    create_my_http_client(CONFIG.url, CONFIG.port)


CONFIG = create_app_config(MySimpleAppConfig())
CONFIG.load_config_file('/my/configuration/file.yml')

# setup_http will be automatically called if a value changes in the MyAppSimpleConfig
# during a subsequent call to CONFIG.load_file() or when the config gets loaded for the first time
sub = CONFIG.subscribe_for_changes(setup_http)

# It's possible to cancel the subscription again
sub.cancel()

变更日志

0.2.4 (19.04.2022)

  • 默认值也得到验证

0.2.3 (08.04.2022)

  • 为 pydantic 字段添加了额外的 kwargs 检查
  • 添加了将生成的 yaml 作为字符串获取的选项

0.2.2 (31.03.2022)

  • 添加了便利基类AppBaseModelBaseModel
  • 适用于私有属性和类函数
  • 修复了无法正确创建多行注释的问题
  • 添加了从配置文件中排除条目的选项

0.2.1 (25.03.2022)

  • 允许回调文件默认值

0.2.0 (25.03.2022)

  • 切换到新的更灵活的 API
  • 文件默认值和配置默认值现在分开了

0.1.2 (08.03.2022)

  • 评论很好用
  • 修复了嵌套数据结构的问题
  • 允许为文件创建指定不同的值

0.1.1 (26.02.2022)

  • 修复了动态默认值的问题
  • 不会在 yaml 文件中创建默认为 None 的可选值

0.1.0 (10.01.2022)

  • 更新要求

0.0.2 (16.09.2021)

  • 验证用户默认值
  • 使用值的 json 表示来获取本机 yaml 数据类型
  • 使用枚举值而不是枚举类型

0.0.1 (14.09.2021)

  • 初始发行

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

easyconfig-0.2.4.tar.gz (17.0 kB 查看哈希

已上传 source

内置分布

easyconfig-0.2.4-py3-none-any.whl (19.3 kB 查看哈希

已上传 py3