'Helper tool to refactor large python file into smaller files

I have a 3000+ line python file (call it orig) containing 30ish utility functions. I'd like to split it into 5 files (say A.py, B.py, etc).

After the split, is there a helper tool to change all the orig.func1 and orig.func2 in the entire repo to A.func1, B.func2?



Solution 1:[1]

As @BrokenBenchmark suggested, I ended up writing my own helper in python

lines = open('calc.py').readlines()

out = []
for line in lines:
    if 'orig.' in line:
        m = re.search('orig.(.*?)\(', line)
        f = m.group(1)    
        if f in dir(A):
            line = line.replace('orig.', 'A.')
        elif f in dir(B):
            line = line.replace('orig.', 'B.')

        # and other modules

        else:
            raise ValueError
    out.append(line)
            
open('calc2.py', 'w').writelines(out)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jf328