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_USERor usetestuser- raise
ValueErrorifDB_PASSWORDis 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 containeradd_yaml_source(path: str) -> YConfig— load YAML fileadd_env_source(path: str = None) -> YConfig— load.envand OS varsresolve_templates() -> None— processenv,file,yaml,configtemplatesto(model: Type[BaseModel] | str) -> Any— convert to Pydantic model or import pathget(key: str, value_type: Type[Any] = str) -> Any— retrieve typed valueset(key: str, value: Any) -> None— set or override value__getitem__(key: str) -> Any,__setitem__(key: str, value: Any)— bracket access
-
YConfigFactory— registry for singleton configsset_config(config: YConfig, key: str = 'default') -> Noneget_config(key: str = 'default') -> YConfig
-
YNode— node wrapper for dicts and lists- Supports iteration:
for k in node,node.items(),node.values()
- Supports iteration:
Examples
-
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'
-
Dot Notation Read/Write
config['debug.db.url'] = 'sqlite:///local.db' assert config.debug.db.url.startswith('sqlite')
-
Pydantic Conversion by String
app_cfg: AppConfig = config.to('test_config.AppConfig')
-
Iterate over YNode
node = YNode({'a': 1, 'b': 2}) keys = list(node) # ['a', 'b'] items = list(node.items()) # [('a', 1), ('b', 2)]
-
Error Handling
try: _ = config['non.existent'] except KeyError: print("Missing key raised correctly")
License
Apache License 2.0
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d84671ff602e9fd323a47201b7831a3045b175c61be0dbb3da46269b6f7f6978
|
|
| MD5 |
0c8e7a748f40db44b4d41420db09b596
|
|
| BLAKE2b-256 |
9d8ef860ccc0f36767a7842fc8dfdba1cb4fcb307fef431685939dfa1492a5be
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f95bcda97380479bbb1a8aae99fd99ab442e804649fc57cd0a0aa98799cb2ba
|
|
| MD5 |
dfcc8b3d1ea600f203ed1eb75be63c34
|
|
| BLAKE2b-256 |
66b91ca68ea786fa91d0e743bbbdda94339bcdf0fdb1861dd53c84f0d19abc58
|