home / skills / benchflow-ai / skillsbench / yaml-config
/tasks/adaptive-cruise-control/environment/skills/yaml-config
This skill helps you read and write YAML safely, handle defaults, and merge configurations with robust error handling.
npx playbooks add skill benchflow-ai/skillsbench --skill yaml-configReview the files below or copy the command above to add this skill to your agents.
---
name: yaml-config
description: Use this skill when reading or writing YAML configuration files, loading vehicle parameters, or handling config file parsing with proper error handling.
---
# YAML Configuration Files
## Reading YAML
Always use `safe_load` to prevent code execution vulnerabilities:
```python
import yaml
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
# Access nested values
value = config['section']['key']
```
## Writing YAML
```python
import yaml
data = {
'settings': {
'param1': 1.5,
'param2': 0.1
}
}
with open('output.yaml', 'w') as f:
yaml.dump(data, f, default_flow_style=False, sort_keys=False)
```
## Options
- `default_flow_style=False`: Use block style (readable)
- `sort_keys=False`: Preserve insertion order
- `allow_unicode=True`: Support unicode characters
## Error Handling
```python
import yaml
try:
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
except FileNotFoundError:
config = {} # Use defaults
except yaml.YAMLError as e:
print(f"YAML parse error: {e}")
config = {}
```
## Optional Config Loading
```python
import os
import yaml
def load_config(filepath, defaults=None):
"""Load config file, return defaults if missing."""
if defaults is None:
defaults = {}
if not os.path.exists(filepath):
return defaults
with open(filepath, 'r') as f:
loaded = yaml.safe_load(f) or {}
# Merge loaded values over defaults
result = defaults.copy()
result.update(loaded)
return result
```
This skill helps read, write, and manage YAML configuration files safely and predictably. It focuses on using safe parsing, preserving readability and insertion order, and providing robust error handling and optional defaults. Use it when handling vehicle parameters, application configs, or any YAML-based settings.
The skill uses yaml.safe_load to parse YAML without executing arbitrary code and yaml.dump with readable options to write files. It provides utilities to detect missing files, merge loaded settings with default values, and handle parsing errors cleanly. Options like default_flow_style=False and sort_keys=False are applied to keep files human-friendly and preserve insertion order.
Why use yaml.safe_load instead of yaml.load?
safe_load prevents execution of arbitrary Python objects embedded in YAML, eliminating a class of security risks.
How should I handle missing config files?
Return a defaults dict or a safe empty config. Detect missing files with os.path.exists or catch FileNotFoundError and proceed with defaults.