Skip to main content

yaml-config-loader

轻量级 Python 配置加载工具包,提供类似 Spring Boot 的配置管理能力。

功能特性

  • YAML 配置加载:自动递归查找 application.yml,全局缓存配置
  • @configuration_properties:类似 Spring Boot 的配置属性绑定,支持嵌套 dataclass 自动转换
  • @component 单例容器:轻量级 IoC 容器,自动注册和获取 Bean
  • DotDict 点访问字典:支持 config.user.name 方式访问嵌套字典
  • 零配置:开箱即用,无需复杂设置

安装

pip install yaml-config-loader

快速开始

1. 创建 application.yml

user:
  name: "张三"
  age: 25
  email: "zhangsan@example.com"
  address:
    city: "北京"
    street: "长安街"

server:
  port: 8080
  host: "localhost"

2. 使用 @configuration_properties 绑定配置

from dataclasses import dataclass
from typing import Optional
from config_loader import configuration_properties, component, DotDict

@component
@configuration_properties(prefix="user")
@dataclass
class UserConfig:
    name: str = None
    age: int = None
    email: str = None
    address: Optional[DotDict] = None  # 嵌套配置自动转为 DotDict

    def _after_config(self):
        """配置绑定完成后的钩子方法"""
        print(f"[UserConfig] 初始化完成: {self.name}")

    def show(self):
        print(f"姓名: {self.name}")
        print(f"年龄: {self.age}")
        print(f"邮箱: {self.email}")
        print(f"城市: {self.address.city}")  # 点访问嵌套配置

3. 嵌套 dataclass 自动转换

from dataclasses import dataclass
from typing import Optional
from config_loader import configuration_properties, component

@dataclass
class Address:
    city: str = None
    street: str = None

@component
@configuration_properties(prefix="user")
@dataclass
class UserConfig:
    name: str = None
    address: Optional[Address] = None  # 自动转为 Address 实例

user = UserConfig()
print(user.address.city)  # 北京

4. 使用单例容器

from config_loader import component, get_bean

@component
class MyService:
    def hello(self):
        return "Hello, World!"

# 获取单例实例
service = get_bean(MyService)
print(service.hello())  # Hello, World!

5. 直接使用 YAML 加载工具

from config_loader import load_yaml, get_yaml_config, find_yaml

# 自动查找并加载 application.yml
load_yaml()

# 获取全局配置
config = get_yaml_config()
print(config["user"]["name"])  # 张三

# 查找配置文件路径
path = find_yaml()
print(path)

API 参考

装饰器

装饰器 说明
@configuration_properties(prefix="xxx") 绑定 YAML 配置到类属性
@component 注册为单例 Bean

函数

函数 说明
load_yaml(path=None) 加载 YAML 配置到全局缓存
find_yaml() 递归查找 application.yml 文件路径
get_yaml_config() 获取全局配置字典
get_loaded_yaml_path() 获取已加载的配置文件路径
get_bean(cls) 获取单例 Bean 实例
list_beans() 列出所有已注册的 Bean
to_dot_dict(d) 将普通字典转为 DotDict

类

类 说明
DotDict 支持点访问的字典

配置文件查找规则

find_yaml() 会按以下顺序递归查找:

  1. 当前工作目录
  2. 上级目录(最多向上查找 5 层)
  3. 项目中的 config/、conf/、resources/ 子目录

支持的文件名:application.yml、application.yaml

许可证

MIT License

Metadata

Release files for yaml-config-loader 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yaml-config-loader 1.0.0
File Size Uploaded
yaml_config_loader-1.0.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yaml-config-loader 1.0.0
File Interpreter ABI Platform
yaml_config_loader-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / yaml_config_loader-1.0.0.tar.gz

Download URL yaml_config_loader-1.0.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0ed6dea1d297c907db3f0801b8a0d5fb3ec3c6d493fb01dc86030868a8dc0449
BLAKE2b-256 checksum
How to use checksums
1762aadd112b2d98b675c5c4aa3c3586ca90be4ad0c2a1c95675e350a5ba48e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release files / yaml_config_loader-1.0.0-py3-none-any.whl

Download URL yaml_config_loader-1.0.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fbf71eda16842d509a7988fad977348a4aab0316ecedce4acab3324fe9ebe81
BLAKE2b-256 checksum
How to use checksums
f3dc2817f6232965e10566a0c137ec8f18d98e20b8a883210047e3b7762d1117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.7

Release history Release notifications | RSS feed

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page