Notebook 代码重构至工具库
notebook.refactor_to_utils
将 notebook 中的代码移动或添加到 *_utils.py 库文件中
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-02
- Stars
- ★ 155
- Forks
- 170
01 前言 Front Matter
- model
- haiku
- description
- Move or add notebook code to a *_utils.py library file
03 SKILL.md 正文
Goal
-
You are an expert Python developer
-
I will pass you a Python file paired with a Jupyter notebook using jupytext in
py:percentformat
Constraints
-
For all the code follow the rules from
.claude/skills/coding.rules.md -
Important Notes
- Always sync before editing:
uvx jupytext --sync notebook.py - Always sync after editing:
uvx jupytext --sync notebook.py
- Always sync before editing:
Scenarios
- This skill handles two complementary scenarios:
Scenario 1: Moving Existing Functions to Utils
- When you need to extract functions from an existing notebook and move them to utils
Scenario 2: Adding New Code to Utils
- When you need to add new functions or code to an existing utils file corresponding to the notebook
Workflow
Identify or Create the Utils File
- Find or create the library/utility file that corresponds to the notebook
- Naming convention: Notebook name with underscores
- E.g.,
Lesson94-Information_Theory.ipynb->Lesson94_Information_Theory_utils.py - E.g.,
tutorial_advanced.ipynb->tutorial_advanced_utils.py
Organize Code Structure in Utils File
-
The utils file should have a structure that mirrors the flow of the notebook
-
Use section separators to organize code by notebook sections
- Example:
# ############################################################################# # Cell 1: Visual Bin - Population of Marbles # ############################################################################# def cell1_draw_bin_with_marbles_interactive(...): ... def _cell1_helper_function(...): ... # ############################################################################# # Cell 2: Entropy Calculations # ############################################################################# def cell2_calculate_entropy(...): ...
- Example:
-
Add the functions in the part of the utility file that corresponds to the notebook
-
Group related functions together
-
Use private functions (prefix with
_) for helpers not called from the notebook
Move or Add Functions
For Moving Functions (Scenario 1):
-
Follow these steps:
- Copy all functions from the notebook to the utils file (without changing code)
- Remove the functions from the notebook
- Update notebook cells to call the utils functions
For Adding Functions (Scenario 2):
-
Follow these steps:
- Implement new code directly in the utils file
- Create caller code in the notebook that imports and uses the functions
- Add the code in the appropriate section following the utils file structure
Update Notebook to Call Utils
-
Replace function implementations with imports and function calls
-
Pattern:
import notebook_name_utils as utils # In code cells: utils.function_name(...) -
Each code cell should be minimal:
# Display results of Cell 2 analysis. utils.cell2_calculate_entropy()
Code Reuse
- Reuse code already existing in the
*_utils.pyfile - Reuse code already existing in the
helpersdirectory - Avoid duplication across utils functions
- Extract common patterns into helper functions
Sync with Jupytext
- After all modifications are complete, sync to update both files following the
conventions in
# Code Architecture and Responsibility->## Utilities vs. Notebook Responsibilitiesin.claude/skills/notebook.rules.md:> uvx jupytext --sync <path/to/notebook.py>
Conventions
- Always follow the conventions and guidelines in
.claude/skills/notebook.rules.md
Verification
- [ ] Confirm no function moved to
*_utils.pychanged behavior - [ ] Confirm every notebook cell that used to hold inline code now calls the corresponding utils function
- [ ] Confirm the
.ipynband paired.pyfile are in sync via Jupytext
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