Skip to main content

Yaml configuration library for python

Project description

Coyaml — a copilot library for effortless YAML management using dot notation. It offers a gentle learning curve with advanced features: Pydantic validation, environment variable resolution (with defaults), file and external YAML inclusion, recursive template resolution, and dependency injection via function or class paths in YAML.

Table of Contents

Installation

Install via pip:

pip install coyaml

Key Features

Dot Notation Access

Read and write nested keys using attribute or bracket syntax:

from coyaml import YConfig

config = YConfig()
config.add_yaml_source('tests/config/config.yaml')
print(config.debug.db.url)                # attribute-style
print(config['debug.db.url'])             # dot-string key
config['debug.db.url'] = 'sqlite:///db.sqlite'  # write by key

Pydantic Integration

Convert configuration sections into Pydantic models for type-safe access:

from pydantic import BaseModel
from coyaml import YConfig

class DebugConfig(BaseModel):
    db: DatabaseConfig

config = YConfig().add_yaml_source('tests/config/config.yaml')
debug: DebugConfig = config.debug.to(DebugConfig)
print(debug.db.url)

# Or convert whole config:
from test_config import AppConfig
app_config: AppConfig = config.to(AppConfig)
print(app_config.llm)

Environment Variables

Use ${{ env:VAR[:default] }} syntax to inject system or .env values with optional defaults:

# tests/config/config.yaml
index: 9
DEBUG:
  db:
    user: ${{ env:DB_USER:testuser }}
    password: ${{ env:DB_PASSWORD }}

config.resolve_templates() will substitute:

  • DB_USER or use testuser
  • raise ValueError if DB_PASSWORD is unset and no default provided

File Content Insertion

Embed external file contents via ${{ file:path/to/file }}:

init_script: ${{ file:tests/config/init.sql }}

External YAML Import

Merge another YAML file recursively using ${{ yaml:path/to/other.yaml }}:

app:
  extra: ${{ yaml:tests/config/extra.yaml }}

Template Resolution

Support ${{ config:other.key }} to reference existing config values, nested resolves, and catch missing keys with KeyError.

Dependency Injection

Specify full import paths in YAML and load at runtime:

services:
  init: myapp.db.initialize_database
fn = config.services.init.to_callable()  # returns function
fn()

Quick Start

from coyaml import YConfig, YConfigFactory

# Create or retrieve singleton
config = YConfig()
YConfigFactory.set_config(config)
config = YConfigFactory.get_config()

# Load sources
config.add_yaml_source('tests/config/config.yaml')
config.add_env_source('tests/config/config.env')

# Resolve all templates (env, file, yaml, config)
config.resolve_templates()

# Access values
i = config.index                  # integer from YAML
env1 = config.ENV1                # from .env
url = config['debug.db.url']

# Validation / conversion
from pydantic import BaseModel
class MySettings(BaseModel):
    index: int
    ENV1: str
settings = config.to(MySettings)

API: Core Classes

  • YConfig — main configuration container

    • add_yaml_source(path: str) -> YConfig — load YAML file
    • add_env_source(path: str = None) -> YConfig — load .env and OS vars
    • resolve_templates() -> None — process env, file, yaml, config templates
    • to(model: Type[BaseModel] | str) -> Any — convert to Pydantic model or import path
    • get(key: str, value_type: Type[Any] = str) -> Any — retrieve typed value
    • set(key: str, value: Any) -> None — set or override value
    • __getitem__(key: str) -> Any, __setitem__(key: str, value: Any) — bracket access
  • YConfigFactory — registry for singleton configs

    • set_config(config: YConfig, key: str = 'default') -> None
    • get_config(key: str = 'default') -> YConfig
  • YNode — node wrapper for dicts and lists

    • Supports iteration: for k in node, node.items(), node.values()

Examples

  1. Loading YAML & .env

    config = YConfig()
    config.add_yaml_source('tests/config/config.yaml')
    config.add_env_source('tests/config/config.env')
    YConfigFactory.set_config(config)
    config = YConfigFactory.get_config()
    
    assert config.index == 9
    assert config.ENV1 == '1.0'
    assert config.get('ENV2') == 'String from env file'
    
  2. Dot Notation Read/Write

    config['debug.db.url'] = 'sqlite:///local.db'
    assert config.debug.db.url.startswith('sqlite')
    
  3. Pydantic Conversion by String

    app_cfg: AppConfig = config.to('test_config.AppConfig')
    
  4. Iterate over YNode

    node = YNode({'a': 1, 'b': 2})
    keys = list(node)           # ['a', 'b']
    items = list(node.items())  # [('a', 1), ('b', 2)]
    
  5. Error Handling

    try:
        _ = config['non.existent']
    except KeyError:
        print("Missing key raised correctly")
    

License

Apache License 2.0

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

coyaml-0.9.1.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coyaml-0.9.1-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file coyaml-0.9.1.tar.gz.

File metadata

  • Download URL: coyaml-0.9.1.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for coyaml-0.9.1.tar.gz
Algorithm Hash digest
SHA256 f51869a7513dcccc704a64bf0ddeb2a4360f5a76f551e1060b391b06391305d8
MD5 36b18dbae9a67a0746afbe969c2e9a3f
BLAKE2b-256 4d927e1adc01c97c1776e8cce53f0822fb48a833c842241e1149ef804d08347f

See more details on using hashes here.

File details

Details for the file coyaml-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: coyaml-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for coyaml-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 50f2dd4e44df15054e211c2b1bbb072c9f6649449fb46f32b000852f4b5d894d
MD5 a74b050bb65bd0ed042b911f369977c8
BLAKE2b-256 db1fb7e0bdf30b32102aaeb933e21f4fe221291a248aee85fc25594362c3d522

See more details on using hashes here.

Supported by

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