Python绳索:如何在所有子模块重构中查找所有缺少的导入和错误 [英] Python Rope: How to Find all missing imports and errors in all sub modules refactoring
问题描述
我正在尝试查找每个模块及其子模块的所有丢失的导入语句和错误.
I am trying to find all missing import statements and errors for each module and its sub modules.
有专门的工具可以用来做我想做的事情吗?
Is there a dedicated tool for what I am trying to do?
我编写的代码似乎很糟糕,也许已经存在这样的代码了:
The code that I wrote, but seems really terrible and maybe something like this exists already?:
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)
输出:
.[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]
我的项目在重构之前是一个烂摊子,但很有用,现在在重构之后就被破坏了.
My project before refactoring was a mess but sort of useful, now its broken after refactoring.
根据我在codereview上的第一篇文章中的建议阅读实用程序员"后:
After reading 'The Pragmatic Programmer' based on suggestions from my initial post on codereview:
我一直在研究以下源代码:
I have been digging around in the source code of:
/usr/local/lib/python2.7/dist-packages/rope
ROPE的文档似乎很少.我也一直在使用Ninja-IDE,但无法为我面临的问题找到解决方案.
Documentation for ROPE seems a little sparse. I have also been using Ninja-IDE, but haven't been able to find a solution for the problem that I am facing.
总的来说,我想我错过了重构的全部内容.
Overall I think I missed the boat on what refactoring is all about.
The current parent directory layout can be seen here.
与以前的
In comparison to what it was before.
在填写缺失的术语或我什至要求时提供的任何帮助都是很棒的.
Any help, on filling in missing terminology, or on what I am even asking would be great.
pylint -E /path/to/module
推荐答案
pip install pylint
将pylint指向相关的文件夹/模块:
Point pylint to the folder/module in question:
pylint /path/to/module > pylint_output
这将在以下位置创建一个具有全球评估"的文件:
This will create a file with Global Evaluations on:
- 未定义变量<-这就是我在每个文件中寻找的东西.
- 无效名称
- 行太长
- 多余的人
- 坏空白
- 属性定义的外部初始化
- 缺少文档字符串
- 缩进
- 持续失败
- 广泛除外
- 未使用的参数
- 未使用的进口
- 未使用变量
- 禁止自用
- 无成员
- 修复程序
- 不必要的通行证
- 多语句
- 太多陈述
- 重复代码
- 太多本地人
- missing-final-newline
- 太多实例属性
- 太多分支
- redefined-builtin
- 太多公共方法
- 语法错误
- 相对进口
- 也许不是会员
- 导入错误
- 超级上流
- 裸机除外
- 未定义循环变量
- 太多退货声明
- 参数过多
- 太少的公共方法
- star-args
- 重新导入
- 索引例外
- 无法到达
- 太多行
- 外部定义名称
- 上古财产
- 无意义的字符串声明
- 无意义声明
- 旧样式类
- 模块中没有名称
- 全局变量未定义
- 未分配表达式
- 不良订单
- 无人分配
- undefined-variable <-- This is what I was looking for in each file.
- invalid-name
- line-too-long
- superfluous-parens
- bad-whitespace
- attribute-defined-outside-init
- missing-docstring
- bad-indentation
- bad-continuation
- broad-except
- unused-argument
- unused-import
- unused-variable
- no-self-use
- no-member
- fixme
- unnecessary-pass
- multiple-statements
- too-many-statements
- duplicate-code
- too-many-locals
- missing-final-newline
- too-many-instance-attributes
- too-many-branches
- redefined-builtin
- too-many-public-methods
- syntax-error
- relative-import
- maybe-no-member
- import-error
- super-on-old-class
- bare-except
- undefined-loop-variable
- too-many-return-statements
- too-many-arguments
- too-few-public-methods
- star-args
- reimported
- indexing-exception
- unreachable
- too-many-lines
- redefined-outer-name
- property-on-old-class
- pointless-string-statement
- pointless-statement
- old-style-class
- no-name-in-module
- global-variable-undefined
- expression-not-assigned
- bad-except-order
- assignment-from-none
有趣的是,我的问题的直接答案是,在pylint结果中将存在具有这种布局的行:
Of interest and the direct answer to my question is that within the pylint results there will be lines that have this sort of layout:
************* 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)
- [R]违反良好做法"指标的因素
- [C]违反编码标准的发明
- [W]警告样式问题或次要编程问题
- [E]对于重要的编程问题(即很可能是错误)的错误
- [F]导致无法进一步处理的错误
- [R]efactor for a "good practice" metric violation
- [C]onvention for coding standard violation
- [W]arning for stylistic problems, or minor programming issues
- [E]rror for important programming issues (i.e. most probably bug)
- [F]atal for errors which prevented further processing
因此,在我的大多数情况下,问题类型(不确定变量)表示尚未导入的模块. pylint -E /path/to/module
将仅返回未定义变量错误.
So an issue-type of (undefined-variable) in most of my cases indicate modules that have not been imported. pylint -E /path/to/module
will return only the undefined-variable errors.
这篇关于Python绳索:如何在所有子模块重构中查找所有缺少的导入和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!