{ "cells": [ { "cell_type": "markdown", "id": "dirty-construction", "metadata": {}, "source": [ "# Rope\n", "\n", "[Rope](https://github.com/python-rope/rope) is a Python refactoring library." ] }, { "cell_type": "markdown", "id": "later-article", "metadata": {}, "source": [ "## Installation\n", "\n", "Rope can be easily installed with\n", "\n", "```console\n", "$uv add rope\n", "```" ] }, { "cell_type": "markdown", "id": "concrete-cover", "metadata": {}, "source": [ "## Use\n", "\n", "Now we first import the `Project` type and instantiate it with the path to the project:" ] }, { "cell_type": "code", "execution_count": 1, "id": "floral-drinking", "metadata": {}, "outputs": [], "source": [ "from rope.base.project import Project\n", "\n", "proj = Project(\"requests\")" ] }, { "cell_type": "markdown", "id": "nervous-university", "metadata": {}, "source": [ "This creates a project folder named `.ropeproject` in our project." ] }, { "cell_type": "code", "execution_count": 2, "id": "modular-dodge", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hooks.py',\n", " 'utils.py',\n", " '_internal_utils.py',\n", " 'status_codes.py',\n", " '__version__.py',\n", " 'sessions.py',\n", " 'api.py',\n", " 'cookies.py',\n", " 'adapters.py',\n", " 'certs.py',\n", " 'exceptions.py',\n", " 'api_v1.py',\n", " 'auth.py',\n", " 'help.py',\n", " 'structures.py',\n", " 'compat.py',\n", " 'packages.py',\n", " '__init__.py',\n", " 'models.py']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[f.name for f in proj.get_files()]" ] }, { "cell_type": "markdown", "id": "interested-basketball", "metadata": {}, "source": [ "The `proj` variable can execute a number of commands such as `get_files` and `get_file`. In the following example we use this to assign the variable `api` to the file `api.py`." ] }, { "cell_type": "code", "execution_count": 3, "id": "supreme-terrorist", "metadata": {}, "outputs": [], "source": [ "!cp requests/api.py requests/api_v1.py" ] }, { "cell_type": "code", "execution_count": 4, "id": "heated-airline", "metadata": {}, "outputs": [], "source": [ "api = proj.get_file(\"api.py\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "choice-customer", "metadata": {}, "outputs": [], "source": [ "from rope.refactor.rename import Rename\n", "\n", "change = Rename(proj, api).get_changes(\"api.py\")\n", "\n", "proj.do(change)" ] }, { "cell_type": "code", "execution_count": 6, "id": "reported-reproduction", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "On branch main\r\n", "Changes not staged for commit:\r\n", " (use \"git add ...\" to update what will be committed)\r\n", " (use \"git restore ...\" to discard changes in working directory)\r\n", "\t\u001b[31mmodified: __init__.py\u001b[m\r\n", "\r\n", "Untracked files:\r\n", " (use \"git add ...\" to include in what will be committed)\r\n", "\t\u001b[31m.ropeproject/\u001b[m\r\n", "\t\u001b[31mapi_v1.py\u001b[m\r\n", "\r\n", "Changes not staged for commit (use \"git add\" and/or \"git commit -a\")\r\n" ] } ], "source": [ "!cd requests && git status" ] }, { "cell_type": "code", "execution_count": 7, "id": "attempted-ratio", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1mdiff --git a/__init__.py b/__init__.py\u001b[m\r\n", "\u001b[1mindex f8f9429..502e33a 100644\u001b[m\r\n", "\u001b[1m--- a/__init__.py\u001b[m\r\n", "\u001b[1m+++ b/__init__.py\u001b[m\r\n", "\u001b[36m@@ -118,7 +118,7 @@\u001b[m \u001b[mfrom .__version__ import __copyright__, __cake__\u001b[m\r\n", " from . import utils\u001b[m\r\n", " from . import packages\u001b[m\r\n", " from .models import Request, Response, PreparedRequest\u001b[m\r\n", "\u001b[31m-from .api import request, get, head, post, patch, put, delete, options\u001b[m\r\n", "\u001b[32m+\u001b[m\u001b[32mfrom .api_v1 import request, get, head, post, patch, put, delete, options\u001b[m\r\n", " from .sessions import session, Session\u001b[m\r\n", " from .status_codes import codes\u001b[m\r\n", " from .exceptions import (\u001b[m\r\n" ] } ], "source": [ "!cd requests && git diff __init__.py" ] }, { "cell_type": "markdown", "id": "consolidated-rescue", "metadata": {}, "source": [ "With `proj.do(change)`, the file `requests/__init__.py` has been changed to import from `new_api` instead of `api`." ] }, { "cell_type": "markdown", "id": "adult-practitioner", "metadata": {}, "source": [ "Rope can be used not only for renaming files, but also in various other cases; see also [Rope Refactorings](https://rope.readthedocs.io/en/latest/overview.html#refactorings)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11 Kernel", "language": "python", "name": "python311" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }