使用Python导入映射模块以便于重构 [英] Mapping module imports in Python for easy refactoring

查看:74
本文介绍了使用Python导入映射模块以便于重构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆Python模块要清理,重组和重构(有一些重复的代码,一些未使用的代码...),我想知道是否有工具可以绘制哪个模块使用的地图哪个其他模块.

I have a bunch of Python modules I want to clean up, reorganize and refactor (there's some duplicate code, some unused code ...), and I'm wondering if there's a tool to make a map of which module uses which other module.

理想情况下,我想要一张这样的地图:

Ideally, I'd like a map like this:

main.py
 -> task_runner.py
  -> task_utils.py
  -> deserialization.py
   -> file_utils.py
 -> server.py
  -> (deserialization.py)
  -> db_access.py

checkup_script.py
re_test.py
main_bkp0.py
unit_tests.py

...,这样我就可以知道我可以先开始移动哪些文件(file_utils.py,db_access.py),main.py不使用哪些文件,因此可以将其删除等等.(我m实际上使用了大约60个模块)

... so that I could tell which files I can start moving around first (file_utils.py, db_access.py), which files are not used by my main.py and so could be deleted, etc. (I'm actually working with around 60 modules)

编写一个执行此操作的脚本可能不会很复杂(尽管要处理的导入语法不同),但我也希望我不是第一个要这样做的人.想要这样做(如果有人为此设计了一个工具,它可能包括其他简洁的功能,例如告诉我可能未使用哪些类和函数).

Writing a script that does this probably wouldn't be very complicated (though there are different syntaxes for import to handle), but I'd also expect that I'm not the first one to want to do this (and if someone made a tool for this, it might include other neat features such as telling me which classes and functions are probably not used).

您是否知道任何有助于代码重组的工具(甚至是简单的脚本)?

Do you know of any tools (even simple scripts) that assist code reorganization?

您知道我要做什么的更确切的用语吗?代码重组?

Do you know of a more exact term for what I'm trying to do? Code reorganization?

推荐答案

Python的 modulefinder 做到这一点.编写将这些信息转换为导入图的脚本非常容易(您可以使用 graphviz ):这是一个清晰的解释.还有 snakefood 可以为您完成所有工作(也可以使用AST!)

Python's modulefinder does this. It is quite easy to write a script that will turn this information into an import graph (which you can render with e.g. graphviz): here's a clear explanation. There's also snakefood which does all the work for you (and using ASTs, too!)

您可能想研究 pylint

You might want to look into pylint or pychecker for more general maintenance tasks.

这篇关于使用Python导入映射模块以便于重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆