jmu*_*sch 6 python refactoring automated-tests
我正在尝试为每个模块及其子模块查找所有丢失的导入语句和错误。
是否有专门的工具可以用于我正在尝试做的事情?
我写的代码,但看起来真的很糟糕,也许这样的东西已经存在?:
import os
def find_missing_imports(walk):
for items in walk:
d = items[0]
f_list = items[1]
for f in f_list:
module = f[:-3]
# posix_path
module_path = d.lstrip('.').replace('/','.').lstrip('.')
try:
__import__(module_path, fromlist=[module])
except IndentationError, e:
#print(f,e)
pass
except NameError, e:
print(d,f,e)
pass
except Exception, e:
print(f,e)
pass
walk = [[root,files] for root,dirs,files in os.walk('.') for fn in files if fn.endswith('.py')]
find_missing_imports(walk)
Run Code Online (Sandbox Code Playgroud)
输出:
.[snip]
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ImageSelectionFrame.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ItemSpecificsDialog.py', NameError("name 'wx' is not defined",))
('./Sky_Group_Inventory_Scanner-wxpython/display_image/Dialogs', 'ReturnCorrectWatchTitle.py', NameError("name 'wx' is not defined",))
.[snip]
Run Code Online (Sandbox Code Playgroud)
我的项目在重构之前是一团糟但有点有用,现在重构后它坏了。
根据我在 codereview 上的初始帖子中的建议阅读“实用程序员”后:
我一直在挖掘以下源代码:
/usr/local/lib/python2.7/dist-packages/rope
ROPE 的文档似乎有点稀疏。我也一直在使用 Ninja-IDE,但一直无法找到解决我面临的问题的方法。
总的来说,我认为我错过了重构的全部意义。


任何帮助,填写缺失的术语,或者我什至问什么都会很棒。
pylint -E /path/to/module
pip install pylint
将 pylint 指向有问题的文件夹/模块:
pylint /path/to/module > pylint_output
这将创建一个包含全局评估的文件:
我的问题的有趣和直接答案是,在 pylint 结果中,会有具有这种布局的行:
************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)
************* Module module_name.sub_module.class_name.method_name
R: line_no, column: Issue description 'some_name' (issue-type)
C: line_no, column: Issue description 'some_name' (issue-type)
W: line_no, column: Issue description 'some_name' (issue-type)
E: line_no, column: Issue description 'some_name' (issue-type)
F: line_no, column: Issue description 'some_name' (issue-type)
Run Code Online (Sandbox Code Playgroud)
因此,在我的大多数情况下,问题类型 (undefined-variable) 表示尚未导入的模块。pylint -E /path/to/module将只返回未定义的变量错误。