Skip to main content

Confdantic is a Python library that enhances Pydantic's capabilities for working with JSON, YAML, and TOML formats. It preserves field descriptions as comments when serializing to YAML or TOML, making it great for generating user-friendly configuration files.

Project description

Confdantic

Tests PyPI version Supported versions Downloads license

Confdantic is a Python library that enhances Pydantic's capabilities for working with JSON, YAML, and TOML formats. It preserves field descriptions as comments when serializing to YAML or TOML, making it great for generating user-friendly configuration files.

Installation

From PyPi

pip install confdantic

From source

pip install git+https://github.com/zigai/confdantic.git

Example

from typing import Literal
from pydantic import Field
from confdantic import Confdantic

class DatabaseConfig(Confdantic):
    host: str = Field(
        "localhost",
        description="The hostname or IP address of the database server",
    )
    port: int = Field(
        5432,
        description="The port number on which the database server is listening.",
    )
    username: str
    password: str

class ApplicationConfig(Confdantic):
    debug: bool = Field(False, description="Enable debug mode")
    log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = Field(
        "INFO", description="Logging level"
    )
    database: DatabaseConfig
    allowed_hosts: list[str] = Field(
        default_factory=list,
        description="A list of host/domain names that this application can serve.",
    )

config = ApplicationConfig(
    database=DatabaseConfig(username="admin", password="secret"),
    allowed_hosts=["example.com"],
)
config.save("config.yaml", comments=True)
config.save("config.toml", comments=True)

config.yaml

debug: false  # Enable debug mode
log_level: INFO # Logging level | choices: DEBUG, INFO, WARNING, ERROR
database:
  host: localhost  # The hostname or IP address of the database server
  port: 5432 # The port number on which the database server is listening.
  username: admin
  password: secret
allowed_hosts:  # A list of host/domain names that this application can serve.
  - example.com

config.toml

debug = false # Enable debug mode
log_level = "INFO" # Logging level | choices: DEBUG, INFO, WARNING, ERROR
allowed_hosts = ["example.com"] # A list of host/domain names that this application can serve.

[database]
host = "localhost" # The hostname or IP address of the database server
port = 5432 # The port number on which the database server is listening.
username = "admin"
password = "secret"

License

MIT License

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

confdantic-0.0.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

confdantic-0.0.5-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file confdantic-0.0.5.tar.gz.

File metadata

  • Download URL: confdantic-0.0.5.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for confdantic-0.0.5.tar.gz
Algorithm Hash digest
SHA256 a67e1997aa96f759fe082b26ed57ecd03c4d14de86f31034d342e4efc869aac9
MD5 29e75eff418c5fcf02fca428e66ded23
BLAKE2b-256 93d4fa5b8d0dc372bade2a1ee63980ea480c9f9a6095d2bfff392d0bc5ae6c69

See more details on using hashes here.

File details

Details for the file confdantic-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: confdantic-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for confdantic-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 49c65f530053e55fc38ae96f60cffe2e8b936b6a07e1e0e452e1148ecf8d2691
MD5 b22e2c077490df27d72970f2c0f72d6d
BLAKE2b-256 419622e04e817e161bf5760c0787c8add7fc862b6414cd52e78f4a5605001141

See more details on using hashes here.

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