Hacker News new | ask | show | jobs
by anentropic 2250 days ago
Shout out here for pydantic BaseSettings https://pydantic-docs.helpmanual.io/usage/settings/

That provides typed and validated auto-loading from env vars. I have been quite happy with that in conjunction with an optional .toml file, to do flexible config cleanly and simply like:

    import toml

    from myproj.conf.types import Settings  # a pydantic BaseSettings model


    try:
        _config = toml.load('myproj.toml')
    except FileNotFoundError:
        _config = {}


    settings = Settings(
        **{key.upper(): val for key, val in _config.items()}
    )