Current collected state Current
Observed since 20 Oct, 15:12 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
12factor-configclasses
Like dataclasses but for config.
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:
...
int, float, str or bool.@configclass(prefix="<PREFIX>") to append this prefix to your configclass' attribute names.Python 3.8+
Depending on your chosen config file format you can install:
pip install 12factor-configclasses[dotenv]pip install 12factor-configclasses[yaml]pip install 12factor-configclasses[toml]pip install 12factor-configclassespip install 12factor-configclassesOr install all supported formats with:
pip install 12factor-configclasses[full]
There are three ways to use it.
# .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)
The same than before, but instead of:
app_config = AppConfig.from_path(".env")
You will do:
app_config = AppConfig.from_environ()
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")
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
tomlkitEnvironment{
"extras": "toml",
"marker": "extra == \"toml\" or extra == \"full\"",
"requires_python": ">=3.9,<4.0",
"evaluation": "NOT_EVALUATED"
} | tomlkit[toml] (>=0.12,<0.13) ; extra == "toml" or extra == "full" | runtime |
python-dotenvEnvironment{
"extras": "dotenv",
"marker": "extra == \"dotenv\" or extra == \"full\"",
"requires_python": ">=3.9,<4.0",
"evaluation": "NOT_EVALUATED"
} | python-dotenv[dotenv] (>=1.0,<2.0) ; extra == "dotenv" or extra == "full" | runtime |
pyyamlEnvironment{
"extras": "yaml",
"marker": "extra == \"yaml\" or extra == \"full\"",
"requires_python": ">=3.9,<4.0",
"evaluation": "NOT_EVALUATED"
} | pyyaml[yaml] (>=6,<7) ; extra == "yaml" or extra == "full" | runtime |
bdist_wheel · 7.44 KB · Python py3
sdist · 6.29 KB · Python source
Observed since 20 Oct, 15:12 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:186d54ec0871e7de42e9516eef5e038fe9e6fcfad1b8b6a02ee14c8fedc8de7c