Skip to main content

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

Project description

介绍

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

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

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

本库通过少量代码,和编程规范,解决上述问题:

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

合并配置的规则:

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

用法

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

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

from briefconf import BriefConfig

# 请自行更改环境变量的名称
configfile = os.getenv("PROJECT_CONFIG_FILE", default="config.yaml")


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

    __slots__ = ("is_production", )

    @classmethod
    def load(cls, config_path: str) -> Self:
        configs = cls._load_config(config_path)

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


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

__all__ = ["config"]

在其他文件中获得配置参数的值

from config_handle import config

assert config.is_production

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

版本迭代

如果将来 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.0.tar.gz (6.3 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.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for briefconf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a52b702cf6dfd37fc390911ab6ab346f262569ad6beb730c8ab1fbb264446300
MD5 5b530e1592ae1a79fa968e807bb536c3
BLAKE2b-256 4604619189fda272b1f5585139e833b13ae4ebccec0c839e2592cd511cbb0ced

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for briefconf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b028f0f7544e9f25389833da2a784f5bfcb5eb4fabcfa4a152775c2eaaba7af
MD5 3e3f6d6a2e03f89d441eac2db52462cd
BLAKE2b-256 b56c4684de199bf97dd866622b197c0a7fb10def24e9a7fd5ce908e16b2a8ac5

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