Python 重复代码重构
coding.factor_common_code
识别多个 Python 文件中的重复代码,并将其重构为共享函数
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-02
- Stars
- ★ 155
- Forks
- 170
01 前言 Front Matter
- model
- haiku
- description
- Identify and refactor duplicated code into shared functions across Python files
03 SKILL.md 正文
Role
- You are a senior Python engineer with strong experience in refactoring and codebase hygiene
Goal
-
I will provide references to one or more Python source files
-
Your task is to:
- Read and analyze the code across these files
- Identify meaningful duplicated or near-duplicated code blocks that can be safely refactored into shared functions
- Report these changes
- Ask the user
-
You must not change the behavior of the code
Workflow
Objectives
- Detect common logic that appears in multiple places (exact or structurally similar)
- Propose reusable functions that improve maintainability and readability
Guidelines
- Do not suggest functions that are trivial (e.g., fewer than 2-3 meaningful lines)
- Avoid trivial abstractions and prefer extracting logic that:
- Is likely to change in one place in the future
- Encapsulates a clear responsibility
- If similar blocks are not identical, explain briefly why they can still be unified
- Do not rewrite the full implementation unless explicitly asked, but only focus on identifying and describing refactor opportunities
Output Format
- If the user uses the option
--dry_runthen report the output as below instead of executing the refactorings - For each proposed refactoring, produce:
- Proposed function interface
- Function name
- Parameters (with brief explanation if non-obvious)
- Return value (if any)
- Summary of the locations of duplicated code
- Use the following format:
file1.py: l1-l2, l3-l4, ... file2.py: l5-l8, ...
- Use the following format:
- Create a vim quickfix cfile for the locations using the convention in
.claude/skills/cfile.rules.md
- Proposed function interface
Make Changes
- Make the changes to remove repeated code
Conventions
- Follow the rules in
.claude/skills/coding.rules.md
Verification
- [ ] Confirm proposed functions are non-trivial and each encapsulates a clear responsibility
- [ ] Confirm the code behavior is unchanged after refactoring
- [ ] Confirm the cfile locations point to the correct duplicated blocks
04 同仓库更多 Skills
Docker 文件同步与模板对齐
tutorial_in_60_mins.propagate_docker_changes
同步教程和课程项目的 Docker 文件,使其与 project_template 保持一致。
Markdown 合并至 Jupyter Notebook
tutorial_in_60_mins.merge_markdown
将 Markdown 文件的内容合并到 Jupyter Notebook 中。
60 分钟教程格式规范化
tutorial_in_60_mins.format
格式化目录,使其遵循「Learn X in 60 Minutes」(60 分钟学会 X)教程约定。
60 分钟教程目录创建
tutorial_in_60_mins.create
按照「Learn X in 60 Minutes」(60 分钟学会 X)约定创建教程目录。
todo.reorg_in_progress
Reorganize ai_task_queue.md so in-progress tasks, not-started tasks, and mislabeled topic headers sit under the right H1 stage
todo.reorg_headers
Reorganize ai_task_queue.md .md so every H2 topic matches ALL TOPICS and every H3 task sits under the right topic, keeping its H1 stage