Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Update Python source code (imports)

We are refactoring our code base.

Old:

from a.b import foo_method

New:

from b.d import bar_method

Both methods (foo_method() and bar_method()) are the same. It just changed the name an the package.

Since above example is just one example of many ways a method can be imported, I don't think a simple regular expression can help here.

How to refactor the importing of a module with a command line tool?

A lot of source code lines need to be changed, so that an IDE does not help here.

like image 710
guettli Avatar asked Nov 23 '21 13:11

guettli


1 Answers

You'll need to write/find some script that will do text replacement of all occurrences in some folder. I remember that Notepad++ could do that.

But as you mentioned, that regexp will not help here, then no scripts (even Open Source) will help here too. And you'll definitely need to have some intelligence here, that will build index of your dependencies/modules/files/packages/etc. and will be able to manipulate them on that level. And that is the purpose for which IDE were built for.

You can choose any that you like: PyCharm, Sublime, Visual Studio or any other that are not just text editor but something that has refactoring functionality.


In any case, I would suggest you to do following refactoring steps:

  • rename old methods and their usages to new names
  • then just replace package path & names in imports with newer versions
like image 67
wowkin2 Avatar answered Sep 28 '22 09:09

wowkin2