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.0.tar.gz (51.4 kB 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.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coyaml-0.9.0.tar.gz
  • Upload date:
  • Size: 51.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for coyaml-0.9.0.tar.gz
Algorithm Hash digest
SHA256 d84671ff602e9fd323a47201b7831a3045b175c61be0dbb3da46269b6f7f6978
MD5 0c8e7a748f40db44b4d41420db09b596
BLAKE2b-256 9d8ef860ccc0f36767a7842fc8dfdba1cb4fcb307fef431685939dfa1492a5be

See more details on using hashes here.

File details

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

File metadata

  • Download URL: coyaml-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for coyaml-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f95bcda97380479bbb1a8aae99fd99ab442e804649fc57cd0a0aa98799cb2ba
MD5 dfcc8b3d1ea600f203ed1eb75be63c34
BLAKE2b-256 66b91ca68ea786fa91d0e743bbbdda94339bcdf0fdb1861dd53c84f0d19abc58

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