Tips for refactoring a parametrization

Hi,

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