Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 1.92 KB

README.md

File metadata and controls

87 lines (62 loc) · 1.92 KB

pyamldantic

Validate and serialize YAML config files with Pydantic, with support for environment variables.

This package uses uv for project management. To get started, make sure that uv is installed on your machine and updated to the latest version. Detailed installation instructions for uv can be found here.

Installation

uv add pyamldantic

Usage

config.yaml

database:
  host: $DATABASE_HOST
  name: $DATABASE_NAME
  password: $DATABASE_PASSWORD
  port: $DATABASE_PORT
  user: $DATABASE_USER
  timeout: $DATABASE_TIMEOUT?
environment: development
is_debug: true

config.py

from pyamldantic import YAMLConfig
from pydantic import BaseModel, SecretStr

class DatabaseSchema(BaseModel):
    host: str
    name: str
    password: SecretStr
    port: int
    user: str
    ssl: bool = False
    timeout: int | None = None

class Schema(BaseModel):
    database: DatabaseSchema
    environment: str
    is_debug: bool

config = YAMLConfig.load("config.yaml", schema=Schema)

main.py

from .config import config

if __name__ == "__main__":
    print(f"Initializing {config.environment} environment...")
    ...

Development

uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg

Testing

uv sync --frozen --group=testing
uv run --frozen pytest

Acknowledgments

This project was inspired by envyaml.

Contributing

Contributions are welcome! To get started, please refer to our contribution guidelines.

Issues

If you encounter any problems while using this package, please open a new issue here.