views:

2133

answers:

4

I have a bunch of classes I want to rename. Some of them have names that are small and that name is reused in other class names, where I don't want that name changed. Most of this lives in Python code, but we also have some XML code that references class names.

Simple search and replace only gets me so far. In my case, I want to rename AdminAction to AdminActionPlug and AdminActionLogger to AdminActionLoggerPlug, so the first one's search-and-replace would also hit the second, wrongly.

Does anyone have experience with Python refactoring tools ? Bonus points if they can fix class names in the XML documents too.

A: 

Does your editor or IDE support search/replace with regular expressions? You could get past your problem using a regex with word boundaries: e.g. replacing "\bAdminAction\b" with "AdminActionPlug" wouldn't match "AdminActionLogger".

dF
+1  A: 

Most editors support the "whole word" search option. It's usually a checkbox in the search dialog, and what it does is only match the search term if it has leading and trailing spaces, dots, and most other delimiters. It will probably work in your case.

Turambar
I think this is very dangerous practice.Search-replace may work for classes, but absolutely unusable for renaming methods.You can do this with compile time bound languages, but Python's late binding nature makes it very hard to detect renaming problems later on.
extropy
+10  A: 

In the meantime, I've tried it two tools that have some sort of integration with vim.

The first is Rope, a python refactoring library that comes with a Vim (and emacs) plug-in. I tried it for a few renames, and that definately worked as expected. It allowed me to preview the refactoring as a diff, which is nice. It is a bit text-driven, but that's alright for me, just takes longer to learn.

The second is Bicycle Repair Man which I guess wins points on name. Also plugs into vim and emacs. Haven't played much with it yet, but I remember trying it a long time ago.

Haven't played with both enough yet, or tried more types of refactoring, but I will do some more hacking with them.

Thomas Vander Stichele
A: 

Your IDE can support refactorings !! Check it Eric, Eclipse, WingIDE have build in tools for refactorings (Rename including). And that are very safe refactorings - if something can go wrong IDE wont do ref.

Also consider adding few unit test to ensure your code did not suffer during refactorings.

przemo_li