← 返回 Skills 市场
freepengyang

Ops Coding Standards

作者 freepengyang · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
50
总下载
0
收藏
1
当前安装
1
版本数
在 OpenClaw 中安装
/install ops-coding-standards
功能描述
运维后台(Python/Django)编码规范与最佳实践,适用于 ops_api 等运维系统开发。当要求写ops代码、代码审查、检查编码规范、Python Django开发规范时自动激活。
使用说明 (SKILL.md)

ops运维后台编码规范

通用运维后台 Python/Django 编码规范,适用于所有 ops 系统开发。


一、规范范围

  • 所有新代码必须遵循 PEP8 编码规范
  • PEP8 规范参考:https://pep8.org/

二、文件头注释(新建 .py 文件时必须)

# -*- coding: utf-8 -*-
"""
@File    : ${NAME}.py
@Desc    : ${DESCRIPTION}
@Author  : ${USER}
@Date    : ${DATE}
"""

三、函数 docstring 规范

3.1 代码行数多 → 必须分步说明

def sync_server_data(server_id):
    """
    同步服务器数据

    第一步:校验目标服务器状态
    第二步:执行数据导出
    第三步:数据传输到目标节点
    第四步:更新数据库记录
    第五步:发送通知

    :param server_id: 服务器ID
    :return: bool  是否成功
    """
    pass

3.2 代码行数少 → 简要注释即可

def get_server_status(server_id):
    """查询游戏服运行状态"""
    return Server.objects.get(id=server_id).status


def disable_user(uid):
    """批量禁用用户"""
    User.objects.filter(id=uid).update(is_active=False)

四、PEP8 代码规范

代码在 PyCharm / IDE 里应避免出现黄色波浪线(PEP8 违规提示)。

违规 错误 正确
运算符缺空格 a+b a + b
逗号后缺空格 def f(a,b) def f(a, b)
单行过长 超过120字符 换行或拆分
import 顺序错误 先 import 第三方 标准库 → 第三方 → 本地

格式化:每次完成后用 IDE reformat(Ctrl+Alt+L / Cmd+Option+L),再提交。


五、Django + Celery 事务规范(高危!)

5.1 事务内调用异步任务的坑

transaction.atomic() 上下文内的 SQL 不会自动提交给 Celery 异步任务。异步任务在独立数据库连接中执行,不受当前事务影响。

❌ 错误

with transaction.atomic():
    Task.objects.create(...)
    async_task.delay(...)  # 事务回滚,但任务已在独立连接执行

✅ 正确

with transaction.atomic():
    Task.objects.create(...)
# 事务提交后再触发
async_task.delay(...)

5.2 异步任务路由

@app.task(queue='high_priority')
def critical_task():
    pass

@app.task(queue='default')
def normal_task():
    pass

六、日志轮转规范(防止磁盘撑爆)

所有 Web 服务必须配置日志轮转。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': '/var/log/ops_api/app.log',
            'when': 'midnight',
            'interval': 1,
            'backupCount': 30,
        },
    },
    'root': {
        'handlers': ['file'],
        'level': 'INFO',
    },
}

七、Django Admin 命令 vs 离线脚本

场景 推荐方式
Web 后台触发的运维动作 Django Admin 命令(management/commands/)
纯定时任务(crontab) 离线脚本
需要版本控制 Django Admin 命令

八、敏感信息过滤(必须遵守!)

❌ 禁止 ✅ 正确做法
硬编码密码/密钥/Secret 在代码中 使用环境变量 ${VAR} 或配置文件读取
在日志中打印敏感字段 过滤或脱敏处理
在错误信息中暴露内部路径/接口 返回通用错误信息

示例

# ❌ 硬编码
API_KEY = "sk-xxxxx-secret"

# ✅ 环境变量
API_KEY = os.environ.get("API_KEY", "")

# ❌ 日志打印敏感信息
logger.info(f"password={password}")

# ✅ 脱敏打印
logger.info(f"user_id={user_id}, action=login")

九、不要做什么

❌ 禁止 ✅ 正确做法
事务内直接调用 Celery 异步任务 事务提交后再触发
硬编码密码/密钥在代码中 环境变量或安全存储
不格式化代码就提交 Ctrl+Alt/L 格式化后提交
代码有黄色波浪线不处理 立即修复 PEP8 违规
在生产代码中暴露内部接口信息 统一错误返回值
.git 目录部署到生产 部署前删除

十、自检清单

每次提交前检查:

  1. ✅ 无 PEP8 黄色/红色波浪线
  2. ✅ 每个函数有 docstring
  3. ✅ Ctrl+Alt+L 格式化后提交
  4. ✅ 事务 + 异步任务场景已考虑
  5. ✅ 无硬编码敏感信息
  6. ✅ 日志配置了轮转
安全使用建议
This skill appears safe to install as a coding-guidance reference. Users should still treat it as advisory standards text and review generated code normally before committing or deploying.
功能分析
Type: OpenClaw Skill Name: ops-coding-standards Version: 1.0.0 The skill bundle provides coding standards and best practices for Python/Django operations backend development. It includes guidelines for PEP8 compliance, Celery transaction safety, logging rotation, and sensitive information handling (explicitly forbidding hardcoded secrets). The content in SKILL.md is purely educational and defensive, with no evidence of malicious intent, data exfiltration, or prompt injection attacks.
能力标签
requires-sensitive-credentials
能力评估
Purpose & Capability
The SKILL.md content is coherent with the stated purpose: Python/Django ops coding standards, PEP8 guidance, docstring conventions, Celery transaction advice, logging rotation, and sensitive-information handling.
Instruction Scope
The instructions are scoped to code-writing and code-review guidance. They do not direct the agent to execute commands, access accounts, modify systems, or override user intent.
Install Mechanism
There is no install spec and no code files; this is an instruction-only skill.
Credentials
The metadata declares no required binaries, environment variables, config paths, or primary credential. The capability signal mentioning sensitive credentials appears consistent with examples about avoiding hardcoded secrets, not actual credential access.
Persistence & Privilege
No persistence, background behavior, privilege escalation, local indexing, or account/session use is shown in the provided artifacts.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install ops-coding-standards
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /ops-coding-standards 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
- Initial release with comprehensive Python/Django coding standards for ops system backend development - Includes detailed guidelines for file headers, function docstrings, and PEP8 code style enforcement - Covers best practices for Django + Celery transaction handling to avoid common async pitfalls - Adds requirements for log rotation, sensitive information filtering, and secure credential management - Provides do's and don'ts tables and a pre-commit self-checklist to ensure code quality and security
元数据
Slug ops-coding-standards
版本 1.0.0
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 1
常见问题

Ops Coding Standards 是什么?

运维后台(Python/Django)编码规范与最佳实践,适用于 ops_api 等运维系统开发。当要求写ops代码、代码审查、检查编码规范、Python Django开发规范时自动激活。 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 50 次。

如何安装 Ops Coding Standards?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install ops-coding-standards」即可一键安装,无需额外配置。

Ops Coding Standards 是免费的吗?

是的,Ops Coding Standards 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Ops Coding Standards 支持哪些平台?

Ops Coding Standards 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Ops Coding Standards?

由 freepengyang(@freepengyang)开发并维护,当前版本 v1.0.0。

💬 留言讨论