Skip to main content

configclasses

PyPI codecov Downloads Downloads

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):
        self.config = config
        ...
        

config = ClientConfig.from_path(".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 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/

Requirements

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:

# .env
HOST=0.0.0.0
PORT=8000
DB_URL=sqlite://:memory:
GENERATE_SCHEMAS=True
DEBUG=True
HTTPS_ONLY=False
GZIP=True
SENTRY=False
#config.py
from configclasses import configclass


@configclass
class DB:
    user: str
    password: str
    url: str


@configclass
class AppConfig:
    host: str
    port: int
    db: DB
    generate_schemas: bool
    debug: bool
    https_only: bool
    gzip: bool
    sentry: bool
# app.py
from api.config import AppConfig

app_config = AppConfig.from_path(".env")
app = Starlette(debug=app_config.debug)

if app_config.https_only:
    app.add_middleware(
        HTTPSRedirectMiddleware)
if app_config.gzip:
    app.add_middleware(GZipMiddleware)
if app_config.sentry:
    app.add_middleware(SentryAsgiMiddleware)

...

register_tortoise(
    app,
    db_url=app_config.db.url,
    modules={"models": ["api.models"]},
    generate_schemas=app_config.generate_schemas,
)

if __name__ == "__main__":
    uvicorn.run(app, host=app_config.host, port=app_config.port)

Loading predefined environmental variables:

The same than before, but instead of:

app_config = AppConfig.from_path(".env")

You will do:

app_config = AppConfig.from_environ()

Loading a file from a string:

test_env = """HOST=0.0.0.0
PORT=8000
DB_URL=sqlite://:memory:
GENERATE_SCHEMAS=True
DEBUG=True
HTTPS_ONLY=False
GZIP=True
SENTRY=False"""
app_config = AppConfig.from_string(test_env, ".env")

Metadata

Release files for 12factor-configclasses 2.0.1

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

Source distribution (sdist)

Source distribution for 12factor-configclasses 2.0.1
File Size Uploaded
12factor_configclasses-2.0.1.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for 12factor-configclasses 2.0.1
File Interpreter ABI Platform
12factor_configclasses-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / 12factor_configclasses-2.0.1.tar.gz

Download URL 12factor_configclasses-2.0.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5d0c2a74dcda4e59b268dce7885e223836aa7f29c34a0966574620c7900072c9
BLAKE2b-256 checksum
How to use checksums
1abc1af5af8dd131e00ab79faefaf7c32392f8385dba8420b3f20b19e77bf2a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Windows/10

Release files / 12factor_configclasses-2.0.1-py3-none-any.whl

Download URL 12factor_configclasses-2.0.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f7b14659c30a34fd4ef560c1e18417653230c4cc164d569e680261d06ebc8b0
BLAKE2b-256 checksum
How to use checksums
0d1e16d9281e421d9a15bd71326b851170a87a9492306e09ba839ac6361e99b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.6 Windows/10

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

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