zoukankan      html  css  js  c++  java
  • Pylint在项目中的使用

    需求背景:

    Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。

    Pylint 是一个 Python 工具,除了平常代码分析工具的作用之外,它提供了更多的功能:如检查一行代码的长度,变量名是否符合命名标准,一个声明过的接口是否被真正实现等等。

    Pylint 的一个很大的好处是它的高可配置性,高可定制性,并且可以很容易写小插件来添加功能。

    项目中需要做代码规范检查,所以研究一下pylint的使用。

    pylint使用:

    • 安装pylint:

    >pip install pylint
    

    确认pylint安装成功:

    >pylint --version
    No config file found, using default configuration
    pylint 1.6.5,
    astroid 1.4.9
    Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
    
    • 生成默认配置文件:

    >pylint --persistent=n --generate-rcfile > pylint.conf
    No config file found, using default configuration
    

    查看当前目录下,已经生成了名为pylint.conf的文件,该文件中的配置项都是pylint的默认配置,比较大400多行。

    • check单个文件:

    >pylint --rcfile=pylint.conf manage.py
    
    • check结果总览:
    ************* Module manage
    C:  1, 0: Missing module docstring (missing-docstring)
    W: 14,12: Unused import django (unused-import)
    

    报告中上述格式为检查结果总览:C表示convention,规范;W表示warning,告警;pylint结果总共有四个级别:error,warning,refactor,convention,可以根据首字母确定相应的级别。1, 0表示告警所在文件中的行号和列号。

    • 4种级别告警汇总:
    Messages by category
    +-----------+-------+---------+-----------+
    |type       |number |previous |difference |
    +===========+=======+=========+===========+
    |convention |1      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |refactor   |0      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |warning    |1      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |error      |0      |NC       |NC         |
    +-----------+-------+---------+-----------+
    
    • 消息类型统计:
    Messages
    +------------------+------------+
    |message id        |occurrences |
    +==================+============+
    |mixed-indentation |8           |
    +------------------+------------+
    |unused-import     |2           |
    +------------------+------------+
    |invalid-name      |2           |
    +------------------+------------+
    |redefined-builtin |1           |
    +------------------+------------+
    
    • check整个工程:

      目前看只能一个module一个module的pylint,但是如果在工程根目录下添加init.py文件,即把工程当做一个python包的话,可以对整个工程进行pylint。
    pylint api(api为包名称)
    

    重命名pylint.conf为.pylintrc,即不需要每次执行都带上--rcfile参数了:

    pylintrc in the current working directory
    .pylintrc in the current working directory
    If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.
    The file named by environment variable PYLINTRC
    if you have a home directory which isn’t /root:
    .pylintrc in your home directory
    .config/pylintrc in your home directory
    /etc/pylintrc
    

    pylint集成到PyCharm:

    打开File->设置对话框:

    settings.png

    单击ToolsExternal Tools,添加External Tool:
    Create Tool.png

    这样修改完代码后,Tools菜单下就会多出pylint工具了:

    如果想要pylint当前文件,只要运行上图的pylint工具即可。

  • 相关阅读:
    《自动化测试工程师进阶之路》系列课程
    开发测试工程师系列课程
    AQA新加入手机自动化测试版块
    持续集成体系搭建服务
    开源自动化测试White与UIA
    TIB自动化测试快讯 自动化测试空间一周精选(201119)
    周六广州软件测试俱乐部圆桌会议3期
    Java白盒测试训练
    TIB自动化测试快讯 自动化测试空间一周精选(2012220)
    TIB自动化测试快讯 自动化测试空间一周精选(201226)
  • 原文地址:https://www.cnblogs.com/zhangningyang/p/8652941.html
Copyright © 2011-2022 走看看