configclasses
Like dataclases but for config.
Specify your config with a class and load it with your env vars or env files.
>>> import httpx
... from configclasses import configclass
>>> class UserAPIClient(httpx.AsyncClient):
... def __init__(self, config: ClientConfig, *args, **kwargs):
... self.config = config
... super().__init__(*args, **kwargs)
...
... async def get_users(self, headers: Optional[Headers] = None) -> Dict[str, Any]:
... response = await self.get(f"{self.path}/users", auth=headers)
... response.raise_for_status()
... return response.json()
...
>>> @configclass
... class ClientConfig:
... host: str
... port: int
...
>>> config = ClientConfig.from_path(".env")
... async with UserAPIClient(config) as client:
... users = await client.get_users(auth_headers)
...
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,strorbool. - 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
pip install 12factor-configclasses
Supported formats
- .env ->
pip install 12factor-configclasses[dotenv] - .yaml ->
pip install 12factor-configclasses[yaml] - .toml ->
pip install 12factor-configclasses[toml] - .ini
- .json
Install all dependencies with:
pip install 12factor-configclasses[full]
Example
# .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
...
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)
Metadata
Release files for 12factor-configclasses 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| 12factor-configclasses-0.2.5.tar.gz | 6.3 kB | Without provenance | Details for 12factor-configclasses-0.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| 12factor_configclasses-0.2.5-py3-none-any.whl | Python 3 | none | any | Details for 12factor_configclasses-0.2.5-py3-none-any.whl |
Total release size: 12.8 kB
Release files / 12factor-configclasses-0.2.5.tar.gz
| Download URL | 12factor-configclasses-0.2.5.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
730c439415f92db1ecb5cb3c83172830866633a5b8702811b1b693f8fe461935
|
|
BLAKE2b-256 checksum How to use checksums |
7b2f5a3c2f5bbd5a7d6220137daf9b958ec0481276447c2e1b94f9682c433450
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/0.12.17 CPython/3.8.0 Windows/10
|
Release files / 12factor_configclasses-0.2.5-py3-none-any.whl
| Download URL | 12factor_configclasses-0.2.5-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
694ce4c2ed6491fe170c494d0d7402c3f794e1de989608f968698a40dc3dccc7
|
|
BLAKE2b-256 checksum How to use checksums |
b291a6ac5ca057542bcadf82a7a3a23039dd78c0550716e99c0292cdaf7a3a3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/0.12.17 CPython/3.8.0 Windows/10
|
Security for 12factor-configclasses 0.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.