Skip to main content

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

Project description

pyamldantic

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

Installation

pip install 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 pydantic import BaseModel, SecretStr
from pyamldantic import YAMLConfig

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...")
    ...

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyamldantic-1.0.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

pyamldantic-1.0.0-py3-none-any.whl (4.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page