Refactorisation de Notebooks Jupyter

VérifiéSûr

Analyse les fichiers Jupyter Notebook (.ipynb) en JSON pour identifier et remplacer en masse des instructions d'importation ou des blocs de code spécifiques. Utile pour renommer des modules, mettre à jour des appels de fonction ou standardiser des motifs de code dans plusieurs notebooks d'un répertoire.

Spar Skills Guide Bot
DeveloppementIntermédiaire
14002/06/2026
Claude CodeCursorWindsurfCodex
#jupyter#notebook#refactoring#batch-replace#code-analysis

Recommandé pour

Notre avis

Analyse et effectue des remplacements par lots d'imports ou de blocs de code dans les fichiers Jupyter Notebook (.ipynb) de manière programmatique.

Points forts

  • Permet une recherche et un remplacement précis via regex sur plusieurs notebooks
  • Préserve la structure JSON du notebook et le format source des cellules
  • Automatise les tâches de refactorisation répétitives et sujettes à erreurs manuelles

Limites

  • Nécessite une regex attentive pour éviter des modifications non voulues
  • Inadapté aux transformations sémantiques (ex. renommer des variables au-delà de simples chaînes)
  • Peut nécessiter une relecture manuelle après remplacement pour valider l'exactitude
Quand l'utiliser

Utilisez cette compétence lorsque vous devez mettre à jour des imports, des fonctions obsolètes ou des motifs de code dans un grand nombre de notebooks Jupyter.

Quand l'éviter

Évitez cette compétence pour des modifications ponctuelles dans un seul notebook ou lorsque la transformation nécessite la compréhension de l'état d'exécution ou des dépendances de données.

Analyse de sécurité

Sûr
Score qualité85/100

The skill performs local file manipulations on Jupyter notebooks using Python and does not execute arbitrary shell commands, exfiltrate data, or disable security features. It uses standard JSON parsing and writing, with no destructive patterns.

Aucun point d'attention détecté

Exemples

Replace import statement in all notebooks
Replace all occurrences of 'from gwexpy.noise import asd' with 'from gwexpy.noise import asd_updated' in every .ipynb file under the current directory.
Rename function call across multiple notebooks
In all Jupyter notebooks in the 'experiments' folder, replace 'old_function(' with 'new_function(' in all code cells.
Change plotting library import
Replace 'import matplotlib.pyplot as plt' with 'import seaborn as sns' in all code cells of notebooks in the 'reports' subdirectory.

name: refactor_nb description: Jupyter Notebook (.ipynb) 内のコード要素を一括置換・リファクタリングする

refactor_notebooks Skill

This skill is for programmatically analyzing Jupyter Notebook (.ipynb) files and performing batch replacement or modification of specific import patterns or code blocks.

Instructions

  1. Analyze Notebook Structure:

    • Load .ipynb files as JSON using Python and iterate through the cells list.
    • Verify if the cell_type of each cell is code.
  2. Filter and Match:

    • Join the source field (list format) into a string and use regular expressions or keyword matching to identify target cells.
    • Target specific import statements (e.g., from gwexpy.noise import asd), function calls, or targeted comments.
  3. Implement Transformation:

    • Create a transformation script to rewrite the source list of the cells in memory.
    • The updated source must be in list format (each element as a string ending with a newline).
  4. Write and Verify:

    • Save the file using json.dump, maintaining an indent of 1 (a gwexpy convention) and specifying ensure_ascii=False.
    • Verify that the resulting notebook is valid JSON and that the intended changes have been applied using view_file or similar.

Usage Guidelines

  • When modifying multiple notebooks across a directory, create a loop script that collects .ipynb files using glob or similar.
  • For complex refactoring, adopt a workflow of saving the logic as a temporary .py script, executing it via run_command, and then deleting the script.
Skills similaires