将大型 python 文件重构为较小文件的帮助工具

发布于 2025-01-09 02:16:26 字数 254 浏览 1 评论 0原文

我有一个 3000 多行的 python 文件(称之为 orig),其中包含 30 个实用函数。我想将其分成 5 个文件(例如 A.pyB.py 等)。

拆分后,是否有一个辅助工具可以将整个存储库中的所有 orig.func1orig.func2 更改为 A.func1B.func2

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2025-01-16 02:16:26

正如 @BrokenBenchmark 所建议的,我最终用 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)

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)
罪歌 2025-01-16 02:16:26

这是 Rope 可以帮助你的事情。

您需要进行 MoveGlobal 重构。如果 你的IDE/编辑器有Rope支持并且它支持移动重构,它可能也支持MoveGlobal。

This is something that Rope can help you with.

You need to do a MoveGlobal Refactoring. If your IDE/editor has Rope support and it supports move refactoring, it would probably support MoveGlobal as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文