Install
openclaw skills install python-code-analyz专业Python代码分析与优化,支持语法检查、安全扫描、性能评估、复杂度分析及重构后的优化代码生成。
openclaw skills install python-code-analyz版本: 1.0.0
作者: sohot-gdjinni
标签: code-review, python, security, optimization
一个专业的 Python 代码分析与优化 Skill,提供:
| 功能 | 说明 |
|---|---|
| 🔍 语法检查 | Python 语法验证、AST 结构分析 |
| 🛡️ 安全扫描 | 检测硬编码密钥、裸 except、SQL 注入等 |
| ⚡ 性能分析 | 识别低效循环、冗余计算、缓存机会 |
| 📊 代码质量 | 复杂度评估、重复代码检测 |
| ✅ 修复版本 | 提供可直接使用的优化后代码 |
# 分析单个文件
python3 -m code_analyzer analyze /path/to/your/code.py
# 分析目录
python3 -m code_analyzer analyze /path/to/project/ --recursive
from code_analyzer import CodeAnalyzer
analyzer = CodeAnalyzer()
results = analyzer.analyze_file('your_code.py')
# 查看问题列表
for issue in results.issues:
print(f"[{issue.severity}] {issue.message}")
# 获取修复建议
fixed_code = results.get_fixed_code()
当你需要分析代码时,直接粘贴代码给我,我会:
def api_get(path):
import urllib.request
try:
req = urllib.request.Request('https://api.example.com' + path)
with urllib.request.urlopen(req) as resp:
return json.loads(resp.read())
except:
return None
📊 代码分析报告
==================================================
函数数量: 1
类数量: 0
导入语句: 1
🔍 发现的问题:
⚠️ [P0] 使用裸 except: 可能隐藏所有异常
⚠️ [P1] 硬编码 API 地址
⚠️ [P1] 缺少超时设置
⚠️ [P2] 导入语句在函数内
✅ 优化建议:
1. 使用具体的异常类型 (HTTPError, URLError)
2. 添加 timeout 参数
3. 将 import 移到文件顶部
import json
import urllib.request
import urllib.error
from typing import Optional, Dict
API_BASE_URL = 'https://api.example.com'
DEFAULT_TIMEOUT = 15
def api_get(path: str, timeout: int = DEFAULT_TIMEOUT) -> Optional[Dict]:
"""发送 GET 请求"""
try:
req = urllib.request.Request(API_BASE_URL + path)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return json.loads(resp.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print(f"HTTP错误: {e.code}")
return None
except urllib.error.URLError as e:
print(f"连接错误: {e.reason}")
return None
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
return None
except: 捕获所有异常# 克隆仓库
git clone https://github.com/yourusername/code-analyzer-skill.git
cd code-analyzer-skill
# 安装依赖
pip install -r requirements.txt
# 可选:安装为系统命令
pip install -e .
创建 .code_analyzer.yaml:
# 忽略的文件/目录
exclude:
- "*/venv/*"
- "*/__pycache__/*"
- "*/tests/*"
# 自定义规则
rules:
max_line_length: 120
max_function_lines: 50
max_complexity: 10
# 严重性覆盖
severity:
bare_except: "error" # 裸 except 升级为错误
missing_timeout: "warning" # 缺少超时降级为警告
┌─────────────────────────────────────────┐
│ 代码输入 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 1. 语法检查 (AST解析) │
│ - Python 语法验证 │
│ - 结构分析 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 2. 静态分析 │
│ - 安全扫描 │
│ - 性能检测 │
│ - 代码异味 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 3. 问题分级 │
│ - P0/P1/P2/P3 │
│ - 影响评估 │
└─────────────────┬───────────────────────┘
▼
┌─────────────────────────────────────────┐
│ 4. 生成修复 │
│ - 代码重构 │
│ - 类型提示 │
│ - 文档生成 │
└─────────────────────────────────────────┘
欢迎贡献!请遵循以下流程:
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)MIT License - 详见 LICENSE 文件
基于以下开源项目构建: