360 Secure SkillHub
360 安全技能中心

yaml-config

该 Skill 提供使用 Python 的 PyYAML 库安全读写 YAML 配置文件的方法,涵盖 safe_load 读取、yaml.dump 写入、异常处理以及带默认值的可选配置加载,适用于读取车辆参数等配置场景。

wu-uk
wu-uk
热度 41.59
最近更新 2026-05-11 17:42:48
分类:开发依赖:代码执行 / 读写文件应用场景:代码生成 / 数据处理 / 自动化脚本

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add adaptive-cruise-control-yaml-config
Metadata
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:

import yaml

with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f)

# Access nested values
value = config['section']['key']

Writing YAML

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

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

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