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

Python Rope: How to Find all missing imports and errors in all sub modules refactoring(Python Rope:如何在所有子模块重构中查找所有缺失的导入和错误)

本文介绍了Python Rope:如何在所有子模块重构中查找所有缺失的导入和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为每个模块及其子模块查找所有缺少的导入语句和错误.

是否有专门的工具来处理我正在尝试做的事情?

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

导入操作系统def find_missing_imports(步行):对于步行项目:d = 项目[0]f_list = 项目[1]对于 f_list 中的 f:模块 = f[:-3]#posix_pathmodule_path = d.lstrip('.').replace('/','.').lstrip('.')尝试:__import__(module_path, fromlist=[module])除了 IndentationError,e:#打印(f,e)经过除了 NameError,e:打印(d,f,e)经过除了例外,e:打印(f,e)经过walk = [[root,files] for root,dirs,files in os.walk('.') for fn in files if fn.endswith('.py')]find_missing_imports(步行)

输出:

.[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",)).[剪辑]

我在重构之前的项目是一团糟但有点用处,现在它在重构之后就坏了.

根据我在 codereview 上的最初帖子中的建议阅读实用程序员"后:

  • 任何关于填写缺失术语的帮助,或者关于我什至要求的任何帮助都会很棒.

    解决方案:

    pylint -E/path/to/module

    解决方案

    pip install pylint

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

    pylint/path/to/module >pylint_output

    /path/to/module 是您有兴趣查看的 python 模块的位置.

    例如:

    我的项目|____ 模块A|____ __init__.py|____ A.py|____ 模块B|____ __init__.py|____ B.py

    • ./my_project/moduleA
    • ./my_project/moduleB

    这将创建一个包含全局评估的文件:

    • undefined-variable <-- 这是我在每个文件中寻找的内容.
    • 无效名称
    • 行太长
    • 多余的括号
    • 错误的空白
    • 属性定义的外部初始化
    • 缺少文档字符串
    • 缩进错误
    • 糟糕的继续
    • 广泛除外
    • 未使用的参数
    • 未使用的导入
    • 未使用的变量
    • 不可自用
    • 没有会员
    • 修复我
    • 不必要的通行证
    • 多语句
    • 陈述过多
    • 重复代码
    • 本地人太多
    • 缺少最终换行符
    • 实例属性过多
    • 分行太多
    • 重新定义内置
    • 公共方法太多
    • 语法错误
    • 相对导入
    • 也许没有会员
    • 导入错误
    • 超级老班
    • 除此之外
    • 未定义循环变量
    • 太多的退货声明
    • 参数过多
    • 公共方法太少
    • 星形参数
    • 重新导入
    • 索引异常
    • 无法访问
    • 行数过多
    • 重新定义的外部名称
    • 旧类属性
    • 无意义的字符串语句
    • 毫无意义的陈述
    • 老式类
    • 模块中没有名称
    • 全局变量未定义
    • 表达式未赋值
    • 除顺序错误
    • 无任务分配

    有趣且对我的问题的直接回答是,在 pylint 结果中会有具有这种布局的行:

    ************** 模块 module_name.sub_module.class_name.method_nameR:line_no,列:问题描述some_name"(问题类型)C:line_no,column:问题描述'some_name'(问题类型)W:line_no,列:问题描述some_name"(问题类型)E: line_no, column: 问题描述'some_name'(问题类型)F:line_no,column:问题描述'some_name'(问题类型)************* 模块 module_name.sub_module.class_name.method_nameR:line_no,列:问题描述some_name"(问题类型)C:line_no,column:问题描述'some_name'(问题类型)W:line_no,列:问题描述some_name"(问题类型)E: line_no, column: 问题描述'some_name'(问题类型)F:line_no,column:问题描述'some_name'(问题类型)

    • [R]违反良好做法"指标的因素
    • [C]违反编码标准的约定
    • [W]针对风格问题或轻微编程问题的警告
    • [E]重要编程问题的错误(即很可能是错误)
    • [F]atal 表示阻止进一步处理的错误

    因此,在我的大多数情况下,问题类型(未定义变量)表示尚未导入的模块.pylint -E/path/to/module 将只返回未定义变量的错误.

    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)
    

    Outputs:

    .[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.

    After reading 'The Pragmatic Programmer' based on suggestions from my initial post on codereview:

    • https://codereview.stackexchange.com/questions/46223/wxpython-item-information-scraper

    I have been digging around in the source code of:

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

    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.

    Solution:

    pylint -E /path/to/module

    解决方案

    pip install pylint

    Point pylint to the folder/module in question:

    pylint /path/to/module > pylint_output

    Where /path/to/module is the location of the python module you're interested in looking at.

    For example:

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

    • ./my_project/moduleA
    • ./my_project/moduleB

    This will create a file with Global Evaluations on:

    • 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

    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]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

    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 Rope:如何在所有子模块重构中查找所有缺失的导入和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐