Skip to main content

A lightweight configuration base class for small to medium-sized Python projects.

Project description

介绍

这是一个可为中小型 Python 项目提供配置管理的基类。

配置文件使用 YAML 格式,人类易读。

最简易的配置管理方案,就是单个文件里存放全部的配置,解析 YAML 后,直接用字典、列表去访问。但这样的缺点是没有代码补全,在配置有增删时也无法让程序静态分析,但更为不便的是没法复用一些相对固定的配置。

本库提出的方案,仅通过少量代码,再加上一点编程规范,解决了上述问题:

  • 手工将程序所需的配置一一作为属性添加到数据类上,这样实现代码补全,方便静态分析
  • 提供解析配置文件的类方法,该方法接收单个配置文件路径,并从中得到其他配置文件路径,最终将这些配置合而为一,实现复用

用法

继承基类创建项目的配置类,基类中提供了一些有用的类方法。

# config_handle.py
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Self

from briefconf import BriefConfig

# 请自行更改环境变量的名称
configfile = os.getenv("PROJECT_CONFIG_FILE", default="config.yaml")
merged_config = os.getenv("PROJECT_MERGED_CONFIG", default="")  # 如果要查看合并后的配置,就通过此环境变量传递路径


@dataclass(frozen=True, slots=True)
class Config(BriefConfig):
    """所有配置参数都作为属性,列举在下面。通过类方法初始化配置实例,将配置的获取和配置参数分开放置,程序结构更加清晰。"""
    is_production: bool

    @classmethod
    def load(cls, config_path: str) -> Self:
        configs = cls._load_config(config_path)
        if merged_config:
            Path(merged_config).write_text(BriefConfig._dump(configs))

        return cls(
            is_production=configs["is_production"]
        )


config = Config.load(os.path.abspath(configfile))

在其他文件中获得配置参数的值,直接引入实例即可: from config_handle import config


更具体的使用案例,可以查看 tests/config_handle.py

合并规则

  1. 配置文件中需要包含键值对 other_configs_path: [],里面放上其他配置文件路径。
  2. other_configs_path 中靠后的配置会覆盖或追加到靠前的配置,具体为:
    • 如果值是字符串、数字、布尔类型,会覆盖靠前的配置;
    • 如果值是列表类型,会追加到后面;
    • 如果值是字典类型,对于重复的字段,根据值的类型,由前面规则决定;新字段会添加进字典。
  3. 当前配置文件会覆盖 other_configs_path 中合并后的配置。

如下面的简单例子,config.yaml 里引入 pgm_config.yaml,根据字典规则,新字段会添加,因此最终解析后的对象里拥有 data_dir: config_and_data_filesis_production: false

# config.yaml
other_configs_path:
- pgm_config.yaml

is_production: false
# pgm_config.yaml
data_dir: config_and_data_files

如果要自定义这个键(other_configs_path)的名称,可以通过修改类成员实现

class CustomConfig(BriefConfig):
    _key4merge_files: ClassVar[str] = "include" # 自定义键名

    is_production: bool

    @classmethod
    def load(cls, config_path: str) -> Self:
        pass

版本迭代

如果将来 BriefConfig 提供的类方法或行为与之前版本不兼容,会将旧版本保留在子模块中,这样升级后出现问题,只需要从子模块引入即可

from briefconf import BriefConfig

class Config(BriefConfig):
    ...

从子模块中引入旧的版本

from briefconf.v1 import BriefConfig

class Config(BriefConfig):
    ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

briefconf-0.1.4.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

briefconf-0.1.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file briefconf-0.1.4.tar.gz.

File metadata

  • Download URL: briefconf-0.1.4.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for briefconf-0.1.4.tar.gz
Algorithm Hash digest
SHA256 7a64ba7d285b10a682627b63801ebad1b46ae89467923bf28c633ebbda628b10
MD5 1d65cadc98d8eeb8a82091cdb4f6d53a
BLAKE2b-256 358e1c492abb73e49d9da49966c68d7aa5d4e3c7d425e839a50d05e1a25e56f2

See more details on using hashes here.

File details

Details for the file briefconf-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: briefconf-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for briefconf-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c3d02349e9f15f3953663f38dcc95a697d5b3f8ada820a3999ed9c087a5a93ed
MD5 0a3ca77b6c8c7f0cb2f28a017ccbc9e6
BLAKE2b-256 6497a4c091d16ced90589b84140ab0630674ba83f94296161e28c437710d5834

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page