Python Rope:如何在所有子模块重构中查找所有缺失的导入和错误

问题描述 投票:0回答:1

我正在尝试查找每个模块及其子模块所有丢失的导入语句和错误。

有专门的工具来完成我想要做的事情吗?

我写的代码,但看起来真的很糟糕,也许这样的东西已经存在了?:

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]

我的项目在重构前是一团糟,但还算有用,现在重构后就坏了。

根据我在 codereview 上的第一篇文章中的建议阅读了“务实的程序员”之后:

我一直在挖掘以下内容的源代码:

/usr/local/lib/python2.7/dist-packages/rope

ROPE 的文档似乎有点稀疏。我也一直在使用 Ninja-IDE,但一直无法找到解决我面临的问题的方法。

总的来说,我认为我错过了重构的意义。

可以在这里看到当前的父目录布局。

enter image description here

与之前

相比。

before

任何有关填写缺失术语或我什至要问的问题的帮助都会很棒。

python refactoring automated-tests
1个回答
11
投票

pip install pylint


将 pylint 指向有问题的文件夹/模块:

pylint /path/to/module > pylint_output


其中

/path/to/module

 是您有兴趣查看的 python 模块的位置。

例如:

my_project |____ moduleA |____ __init__.py |____ A.py |____ moduleB |____ __init__.py |____ B.py

  • ./my_project/moduleA
    
    
  • ./my_project/moduleB
    
    
这将创建一个包含全局评估的文件:

  • 未定义变量 <-- This is what I was looking for in each file.
  • 名称无效
  • 线太长
  • 多余的括号
  • 坏空白
  • 属性定义外部初始化
  • 缺少文档字符串
  • 不良缩进
  • 糟糕的延续
  • 广泛-除了
  • 未使用的参数
  • 未使用的导入
  • 未使用的变量
  • 不可自用
  • 无会员
  • 修复我
  • 不必要的通行证
  • 多重陈述
  • 太多的陈述
  • 重复代码
  • 太多本地人
  • 缺少最后换行符
  • 实例属性过多
  • 太多分支
  • 重新定义内置
  • 太多公共方法
  • 语法错误
  • 相对导入
  • 也许没有会员
  • 导入错误
  • 超老派
  • 裸露-除了
  • 未定义循环变量
  • 太多返回语句
  • 太多的争论
  • 公共方法太少
  • star-args
  • 重新进口
  • 索引异常
  • 无法访问
  • 太多行
  • 重新定义外部名称
  • 旧阶级财产
  • 无意义的字符串语句
  • 毫无意义的陈述
  • 旧式班
  • 模块中无名称
  • 全局变量未定义
  • 表达式未分配
  • 订单除外
  • 无分配
有趣的是我的问题的直接答案是,在 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)

    [R]“良好实践”指标违规的因素
  • [C]违反编码标准公约
  • [W]警告文体问题或小编程问题
  • [E]重要编程问题的错误(即最有可能的错误)
  • [F]atal 阻止进一步处理的错误
因此,在我的大多数情况下,(未定义变量)的问题类型表示尚未导入的模块。

pylint -E /path/to/module

 将仅返回未定义变量错误。

© www.soinside.com 2019 - 2024. All rights reserved.