← 全部工具

12factor-configclasses

热度 60 更新于 开发与构建

Like dataclasses but for config.

pypiauto-collected

安装

暂未验证可直接使用的安装命令,请查看项目官方文档或 Release。

configclasses

[](https://codecov.io/gh/headsrooms/configclasses) <a href="https://codeclimate.com/github/kingoodie/configclasses/maintainability"<img src="https://api.codeclimate.com/v1/badges/9094f65f5caef64fb993/maintainability" /</a [](https://pepy.tech/project/12factor-configclasses) [](https://pepy.tech/project/12factor-configclasses)

Like dataclasses but for config.

Specify your config with a class and load it with your env vars or env files.

# .env
HOST=0.0.0.0
PORT=8000
...
import httpx
from configclasses import configclass


@configclass
class ClientConfig:
    host: str
    port: int


class UserAPIClient(httpx.AsyncClient):
    def __init__(self, config: ClientConfig, *args, **kwargs):

...

config = ClientConfig.frompath(".env") async with UserAPIClient(config) as client: ...


## Features

- Fill your configclasses with existent env vars.
- Define default values in case these variables have no value at all.
- Load your config files in env vars following [12factor apps](https://12factor.net) recommendations.
- Support for _.env_, _yaml_, _toml_, _ini_ and _json_.
- Convert your env vars with specified type in configclass: `int`, `float`, `str` or `bool`.
- Use nested configclasses to more complex configurations.
- Specify a prefix with `@configclass(prefix="<PREFIX>")` to append this prefix to your configclass'  attribute names.
- Config groups (__TODO__): https://cli.dev/docs/tutorial/config_groups/

Python 3.8+

Installation

Depending on your chosen config file format you can install:

  • .env - pip install 12factor-configclasses[dotenv]
  • .yaml - pip install 12factor-configclasses[yaml]
  • .toml - pip install 12factor-configclasses[toml]
  • .ini - pip install 12factor-configclasses
  • .json - pip install 12factor-configclasses

Or install all supported formats with:

pip install 12factor-configclasses[full]

Usage

There are three ways to use it.

Loading an .env file: