Skip to content

rupurt/pydanticutils

Repository files navigation

pydanticutils

ci status pypi versions

Pydantic utility helpers

Usage

  1. Install the package from pypi
> pip install pydanticutils
  1. Create a pydantic settings class
class DatabaseSettings(BaseModel):
    host: str = Field(default="0.0.0.0")
    port: int = Field(default=5432)


class AppSettings(BaseSettings):
    model_config = SettingsConfigDict(env_prefix="APP_", env_nested_delimiter="__")

    log_level: str = Field(default="INFO")
    database: DatabaseSettings = Field(default_factory=DatabaseSettings)
  1. Read a configuration file into the settings class
from pydanticutils import read_yaml

settings = read_yaml("/path/to/config.yaml", AppSettings)

Development

This repository manages the dev environment as a Nix flake and requires Nix to be installed

> nix develop -c $SHELL
> make setup
> make test

Publish Package to PyPi

> make pypi

License

pydanticutils is released under the MIT license