YAML Configuration Manager

1.0.0 · active · verified Thu Apr 16

Yacman is a Python library designed for managing YAML configuration files. It provides a robust YAML configuration manager with features for merging multiple configuration layers, validation, and atomic updates. The library recently released version 1.0.0, which introduced significant changes to constructor patterns and enhanced multi-process locking mechanisms, and maintains a consistent release cadence with active development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `YAMLConfigManager` from a dictionary, access and modify configuration values, and safely write changes to a file using the `write_lock` context manager and `rebase_and_write` method. Note the direct initialization `YAMLConfigManager(data)` is primarily for dictionary data; for file-based configuration, you would typically use `YAMLConfigManager(filepath='your_config.yaml')`.

import os
from yacman import YAMLConfigManager, write_lock, read_lock

# Example data
data = {"my_list": [1,2,3], "my_int": 8, "my_str": "hello world!", "my_dict": {"nested_val": 15}}

# Create a YAMLConfigManager from a dictionary
ym = YAMLConfigManager(data)

# Access data like a dictionary
print(f"my_list: {ym['my_list']}")
print(f"my_int: {ym['my_int']}")
print(f"nested_val: {ym['my_dict']['nested_val']}")

# Modify data
ym["new_var"] = 15

# To write changes to a file, use a write_lock context manager
# and explicitly rebase_and_write for multi-process safety.
# For a real scenario, you would create ym from a file path: ym = YAMLConfigManager(filepath='config.yaml')
# For this quickstart, we'll simulate writing to a dummy file.
config_file_path = "./temp_config.yaml"
with open(config_file_path, 'w') as f:
    f.write(str(ym))

# Now, let's load it from the file and demonstrate writing safely
ym_from_file = YAMLConfigManager(filepath=config_file_path)

# Use a write-lock, and rebase before writing to ensure you capture any changes
# since you loaded the file (important in multi-process environments)
with write_lock(ym_from_file) as locked_ym:
    locked_ym['another_key'] = 'another_value'
    locked_ym.rebase_and_write()

print(f"Updated config from file: {ym_from_file.to_dict()}")

# Cleanup (optional)
os.remove(config_file_path)

view raw JSON →