版本: 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: 捕獲所有異常來源於7w4.net。
# 克隆倉庫
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 檔案
基於以下開源專案構建: - Python AST 模組 - Bandit (安全掃描) - Radon (複雜度分析)