17 Aug 2013 by dryobates
Are you scared when you have to move classes or functions between modules? You’re tired by fixing all those references to your class? With Rope - python refactoring library - it won’t be nightmare anymore.
From time to time every developer has to do refactoring. In some methodologies like eXtreme Programming it’s part of the process and is done quite easily. XP encourages doing often refactoring and with good coverage with tests you know when your changes has broken the code.
In order to start using rope with current project, open some file from that project and run:
It can take some time with large projects. When it finished we can start using Rope to make refactoring.
I had a Django project which had forms mixed in models.py module. We prefer keep forms in their own modules. I could cut it from models.py and paste in forms.py but the I had to replace all places in code where are references to that form. It could be tedious. With Rope it was as simple as putting cursor on class, that I want move and run:
And then answer where I want to put it (“package.forms”) and confirm by pressing enter. If you want to check what would be changed instead of performing change, just select “preview” option. You will be presented with unified diff.
Rope is smart. When you move class to other module it not only changes references to that class in other modules, it also puts required imports in new module.
After refactoring it is wise to run tests. So do it and enjoy saved time :)
|||Rope - python refactoring library: http://rope.sourceforge.net|
|||Ropevim - Vim plugin utilising Rope https://bitbucket.org/agr/ropevim|