Tips for refactoring a parametrization


I’m currently refactoring a parametrization in a big application. The parametrization looks a bit like this

tab1  = Tab( 'tab1')
tab1.section_a = Section('a')
tab1.section_b = Section('b')
tab2  = Tab('tab2')
tab2.section_c = Section('c')
tab2.section_d = Section('d')

Using the refactoring tool included in PyCharm (right click > refactor), it is relatively easy to rename tab2 to tab1 thoughout the entire app to merge the two tabs. However, I don’t know how to refactor tab2.section_c, to tab1.section_c in order to move only section_c.

The refactor tool seems too smart and knows that the ‘tab’ and the ‘section’ are two different components, so it wants to refactor them separately. However, that would also move section_d, so that is not what I want.

The replace menu (ctrl + r) would do what I need, but it only works in one file, so that could work for small apps. But that would be very tedious as there are > 50 different files to go trough, for each tab that I would like to move…

Do you guys have any tips?

You can right-click on a folder in PyCharm and hit ‘Replace in path’ to replace in many files at once. You can even make use of regex replacements in PyCharm by hitting the “.*” sign in the replace bar, which make the possibilities endless. I use it very often.

1 Like

Thanks! I did not know that one, it looks like it worked! I think it is called “replace in files” instead of “replace in path” by the way :wink:

That might be so in your version indeed. I’m running version 2020.2.3 in Arch Linux, in which it is called “Replace in Path” (it seems)

@Puijterwaal The shortcut for this is ctrl+shift+r.
In general (also with find, +f), if you press ctrl + r (or ctrl + f) you do a local search in the file.
If you use ctrl+shit+r (or ctrl+shit+f) you do a ‘gloabal’ seach in all files.

Btw, if you want to seach in all custom app files, you can choose the ‘directory’ tab, and choose the appropriate folder to seach from.

1 Like