zoukankan      html  css  js  c++  java
  • Python

    1-简介

    • 检查语法错误,是否遵守编码风格标准、潜在的问题等;
    • 支持自定义配置:例如显示或隐藏特定的警告,并且可以通过编写插件来添加功能;
    • 使用Pylint检查文件时,需要直接将模块或包名作为参数;
    • 可以在命令行以脚本方式运行(pylint),也可作为模块(pylint.lint)导入,建议作为命令行工具使用;
     

    2-帮助信息

    帮助信息:
    • 在命令行下运行“pylint -h”或“pylint --help”获取帮助信息;
    常用命令行参数:
    --generate-rcfile
    生成一个配置文件示例;
    可以使用重定向把这个配置文件保存下来用做以后使用;
    也可以在前面加上其它选项,使这些选项的值被包含在这个产生的配置文件里;
    如:“pylint --persistent=n --generate-rcfile > pylint.conf”,查看 pylint.conf,可以看到 persistent=no,而不再是其默认值 yes;
    
    
    --rcfile=<file>
    指定一个配置文件;
    把使用的配置放在配置文件中,这样不仅规范了自己代码,也可以方便地和别人共享这些规范;
    
    
    -i <y_or_n>, --include-ids=<y_or_n>
    在输出中包含 message 的 id, 然后通过“pylint --help-msg=<msg-id>”来查看这个错误的详细信息,这样可以具体地定位错误。
    
    
    -r <y_or_n>, --reports=<y_or_n>
    表示 Pylint 的输出中是否包含报告部分;
    
    
    --files-output=<y_or_n>
    将每个 module /package 的 message 输出到一个以 pylint_module/package. [txt|html] 命名的文件中;
    如果有 report 的话,输出到名为 pylint_global.[txt|html] 的文件中。默认是输出到屏幕上不输出到文件里;
     
    
    -f <format>, --output-format=<format>
    设置输出格式;
    可以选择的格式有 text, parseable, colorized, msvs (visual studio) 和 html, 默认的输出格式是 text;
    
    
    --disable-msg=<msg ids>
    禁止指定 id 的 message;
    例如:输出中包含了 W0402 这个 warning 的 message, 如果不希望它在输出中出现,可以使用“--disable-msg= W0402”;

    3-安装

    $ pip3 show pylint
    Name: pylint
    Version: 2.1.1
    Summary: python code static checker
    Home-page: https://github.com/PyCQA/pylint
    Author: Python Code Quality Authority
    Author-email: code-quality@python.org
    License: GPL
    Location: c:python36libsite-packages
    Requires: colorama, isort, astroid, mccabe
    Required-by:
    
    $ py --version
    Python 3.6.0
    
    $ py -m pylint --version
    __main__.py 2.1.1
    astroid 2.0.4
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]

    生成默认配置文件:将在当前目录生成pylint.conf文件,包含pylint的默认配置项;

    $ py -m pylint --persistent=n --generate-rcfile > pylint.conf
    
    $ ll -h pylint.conf
    -rw-r--r-- 1 guowli 1049089 18K Nov 12 10:23 pylint.conf

    4-检查单个文件(模块)

    $ cat test.py
    import time
    
    
    def shwotime():
        print(time.asctime())
    
    
    shwotime()
    
    $ py -m pylint --rcfile=pylint.conf test.py
    ************* Module test
    test.py:1:0: C0111: Missing module docstring (missing-docstring)
    test.py:4:0: C0111: Missing function docstring (missing-docstring)
    
    -----------------------------------
    Your code has been rated at 5.00/10
    
    $ py -m pylint -ry --rcfile=pylint.conf test.py
    ************* Module test
    test.py:1:0: C0111: Missing module docstring (missing-docstring)
    test.py:4:0: C0111: Missing function docstring (missing-docstring)
    
    
    Report
    ======
    4 statements analysed.
    
    Statistics by type
    ------------------
    
    +---------+-------+-----------+-----------+------------+---------+
    |type     |number |old number |difference |%documented |%badname |
    +=========+=======+===========+===========+============+=========+
    |module   |1      |NC         |NC         |0.00        |0.00     |
    +---------+-------+-----------+-----------+------------+---------+
    |class    |0      |NC         |NC         |0           |0        |
    +---------+-------+-----------+-----------+------------+---------+
    |method   |0      |NC         |NC         |0           |0        |
    +---------+-------+-----------+-----------+------------+---------+
    |function |1      |NC         |NC         |0.00        |0.00     |
    +---------+-------+-----------+-----------+------------+---------+
    
    
    
    Raw metrics
    -----------
    
    +----------+-------+------+---------+-----------+
    |type      |number |%     |previous |difference |
    +==========+=======+======+=========+===========+
    |code      |5      |50.00 |NC       |NC         |
    +----------+-------+------+---------+-----------+
    |docstring |0      |0.00  |NC       |NC         |
    +----------+-------+------+---------+-----------+
    |comment   |0      |0.00  |NC       |NC         |
    +----------+-------+------+---------+-----------+
    |empty     |5      |50.00 |NC       |NC         |
    +----------+-------+------+---------+-----------+
    
    
    
    Duplication
    -----------
    
    +-------------------------+------+---------+-----------+
    |                         |now   |previous |difference |
    +=========================+======+=========+===========+
    |nb duplicated lines      |0     |NC       |NC         |
    +-------------------------+------+---------+-----------+
    |percent duplicated lines |0.000 |NC       |NC         |
    +-------------------------+------+---------+-----------+
    
    
    
    Messages by category
    --------------------
    
    +-----------+-------+---------+-----------+
    |type       |number |previous |difference |
    +===========+=======+=========+===========+
    |convention |2      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |refactor   |0      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |warning    |0      |NC       |NC         |
    +-----------+-------+---------+-----------+
    |error      |0      |NC       |NC         |
    +-----------+-------+---------+-----------+
    
    
    
    Messages
    --------
    
    +------------------+------------+
    |message id        |occurrences |
    +==================+============+
    |missing-docstring |2           |
    +------------------+------------+
    
    
    
    
    -----------------------------------
    Your code has been rated at 5.00/10
    结果说明:
    • Pylint结果的级别:error,warning,refactor,convention;
    • 可以根据首字母确定相应的级别,例如,C表示convention(规范)、W表示warning(告警);
    • 级别之后的数字表示告警所在文件中的行号和列号;
    • 参数 “-ry”开启报告,“-rn”关闭报告(只显示警告和错误),默认为关闭报告;

    5-检查整个工程

    在工程根目录下添加init.py文件,即把工程当做一个python包,可以对整个工程进行pylint;
    $ ll -Ri testproject/
    testproject/:
    total 13
     3940649673951194 -rw-r--r-- 1 guowli 1049089   25 Sep 21 09:37 __init__.py
     5348024557504430 -rw-r--r-- 1 guowli 1049089 5241 Sep 21 09:37 Chapter06_Modules.py
     3377699720529875 -rw-r--r-- 1 guowli 1049089 2719 Sep 12 09:17 Chapter06_ModuleTest.py
    17732923532773334 drwxr-xr-x 1 guowli 1049089    0 Nov 12 10:46 TestPackage/
    
    testproject/TestPackage:
    total 8
     1970324836976601 -rw-r--r-- 1 guowli 1049089  818 Sep 21 09:37 __init__.py
    25895697857382360 -rw-r--r-- 1 guowli 1049089 1338 Sep 21 09:37 ModuleTest.py
    
    $ py -m pylint --rcfile=pylint.conf testproject/
    ************* Module testproject.Chapter06_Modules
    testprojectChapter06_Modules.py:1:0: C0103: Module name "Chapter06_Modules" doesn't conform to snake_case naming style (invalid-name)
    testprojectChapter06_Modules.py:1:0: C0111: Missing module docstring (missing-docstring)
    testprojectChapter06_Modules.py:4:0: E0401: Unable to import 'Chapter06_ModuleTest' (import-error)
    testprojectChapter06_Modules.py:5:0: E0401: Unable to import 'TestPackage' (import-error)
    testprojectChapter06_Modules.py:6:0: E0401: Unable to import 'TestPackage.ModuleTest' (import-error)
    testprojectChapter06_Modules.py:22:0: C0103: Constant name "mp" doesn't conform to UPPER_CASE naming style (invalid-name)
    testprojectChapter06_Modules.py:8:0: C0411: standard import "import os" should be placed before "import Chapter06_ModuleTest as cMT" (wrong-import-order)
    testprojectChapter06_Modules.py:9:0: C0411: standard import "import pprint" should be placed before "import Chapter06_ModuleTest as cMT" (wrong-import-order)
    ************* Module testproject.Chapter06_ModuleTest
    testprojectChapter06_ModuleTest.py:1:0: C0103: Module name "Chapter06_ModuleTest" doesn't conform to snake_case naming style (invalid-name)
    testprojectChapter06_ModuleTest.py:1:0: C0111: Missing module docstring (missing-docstring)
    testprojectChapter06_ModuleTest.py:6:0: C0111: Missing function docstring (missing-docstring)
    testprojectChapter06_ModuleTest.py:18:0: C0111: Missing function docstring (missing-docstring)
    testprojectChapter06_ModuleTest.py:19:4: R1705: Unnecessary "else" after "return" (no-else-return)
    ************* Module testproject.TestPackage.ModuleTest
    testprojectTestPackageModuleTest.py:1:0: C0103: Module name "ModuleTest" doesn't conform to snake_case naming style (invalid-name)
    testprojectTestPackageModuleTest.py:1:0: C0111: Missing module docstring (missing-docstring)
    testprojectTestPackageModuleTest.py:5:0: C0111: Missing function docstring (missing-docstring)
    ************* Module testproject.TestPackage
    testprojectTestPackage\__init__.py:1:0: C0103: Module name "TestPackage" doesn't conform to snake_case naming style (invalid-name)
    testprojectTestPackage\__init__.py:1:0: C0111: Missing module docstring (missing-docstring)
    
    -----------------------------------
    Your code has been rated at 3.18/10

    6-集成到PyCharm

    File --》Settings --》Tools --》External Tools
    保存并应用后,Tools菜单下将会显示pylint工具选项;
    如果想要pylint当前文件,只需要点击此选项即可;

    7-其他工具

    pycodestyle

    • 根据PEP8中的某些样式约定来检查Python代码的工具
    • https://pypi.org/project/pycodestyle/
    • 使用pip安装pycodestyle:pip install pycodestyle
    • 从终端运行pycodestyle:pycodestyle code.py


    flake8

  • 相关阅读:
    Python JPype 在 Win7 下安装与使用
    Visual Studio 2010 下 安装RGiesecke.DllExport
    python简单搭建HTTP Web服务器
    C 语言中 malloc、calloc、realloc 和free 函数的使用方法
    Anaconda 安装 ml_metrics package
    第28章 java反射机制
    第26章 java进制操作
    第27章 java I/O输入输出流
    第25章 网络编程
    第23章 java线程通信——生产者/消费者模型案例
  • 原文地址:https://www.cnblogs.com/anliven/p/10004587.html
Copyright © 2011-2022 走看看