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
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
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
Built Distribution
File details
Details for the file confdantic-0.0.4.tar.gz
.
File metadata
- Download URL: confdantic-0.0.4.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c60bd3cd1ab808bbfd25441abbc0ca237fc68136f063d92e904b7ea29e9017d |
|
MD5 | 3c39a585cbd57b8aed81eb88bb8df7c4 |
|
BLAKE2b-256 | b51c962d41c352ba88bfcd2cf9e1388ed08b778fe3b655c3e28aafa17326f5bd |
File details
Details for the file confdantic-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: confdantic-0.0.4-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.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94658d7300b2a3d598248789b78dfece32a06523e4b823ef81717c77de28bfb2 |
|
MD5 | a1e6acde97ef7dc06ee8b2d1439e46d2 |
|
BLAKE2b-256 | 874ee18db6d2a66ce1b8bb73c1de3043cdbbc7cccad45a26cde07957be25f7c0 |