← Back to Skills Marketplace
freepengyang

Ops Coding Standards

by freepengyang · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
50
Downloads
0
Stars
1
Active Installs
1
Versions
Install in OpenClaw
/install ops-coding-standards
Description
运维后台(Python/Django)编码规范与最佳实践,适用于 ops_api 等运维系统开发。当要求写ops代码、代码审查、检查编码规范、Python Django开发规范时自动激活。
README (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. ✅ 日志配置了轮转
Usage Guidance
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.
Capability Analysis
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.
Capability Tags
requires-sensitive-credentials
Capability Assessment
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.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install ops-coding-standards
  3. After installation, invoke the skill by name or use /ops-coding-standards
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
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
Metadata
Slug ops-coding-standards
Version 1.0.0
License MIT-0
All-time Installs 1
Active Installs 1
Total Versions 1
Frequently Asked Questions

What is Ops Coding Standards?

运维后台(Python/Django)编码规范与最佳实践,适用于 ops_api 等运维系统开发。当要求写ops代码、代码审查、检查编码规范、Python Django开发规范时自动激活。 It is an AI Agent Skill for Claude Code / OpenClaw, with 50 downloads so far.

How do I install Ops Coding Standards?

Run "/install ops-coding-standards" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Ops Coding Standards free?

Yes, Ops Coding Standards is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Ops Coding Standards support?

Ops Coding Standards is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Ops Coding Standards?

It is built and maintained by freepengyang (@freepengyang); the current version is v1.0.0.

💬 Comments