zoukankan      html  css  js  c++  java
  • 用Pylint规范化Python代码,附PyCharm配置

    Pylint一个可以检查Python代码错误,执行代码规范的工具。它还可以对代码风格提出建议。

    官网:https://pylint.readthedocs.io 

    pip install pylint

    默认情况,Pylint就已经随着Python安装好。在Python的scripts目录下。


    找一段小程序试验一下Pylint,程序很简单。

    1. #!/usr/bin/env python  
    2. # encoding: utf-8  
    3.   
    4. '''  
    5. Created on 2017年2月4日  
    6.   
    7. @author: Arthur Guo  
    8. '''  
    9.   
    10. N = 10  
    11. YHTriangle = []  
    12. for i in range(N):  # 行  
    13.     YHTriangle.append([])  
    14.     if i == 0:  
    15.         YHTriangle[i].append(1) #第一行只有 1  
    16.     else:  
    17.         YHTriangle[i].append(1) #最左元素永远为 1  
    18.         YHTriangle[i].append(1) #最右元素永远为 1  
    19.         for j in range(1,i):    #中间元素  
    20.             if i <> 0 and i <> 1:  
    21.                 YHTriangle[i].insert(j,YHTriangle[i-1][j-1] + YHTriangle[i-1][j])  
    22. for i in range(N):  
    23.     print YHTriangle[i]  


    命令行运行pylint yanghui.py,结果看起来很冗长

    1. c:>c:Python27Scriptspylint.exe d:workspacePyCommsrcInterviewsyanghui.py  
    2.   
    3. No config file found, using default configuration  
    4. ************* Module Interviews.yanghui  
    5. C: 19, 0: Exactly one space required after comma  
    6.         for j in range(1,i):    #中间元素  
    7.                         ^ (bad-whitespace)  
    8. C: 21, 0: Exactly one space required after comma  
    9.                 YHTriangle[i].insert(j,YHTriangle[i-1][j-1] + YHTriangle[i-1][j]  
    10. )  
    11.                                       ^ (bad-whitespace)  
    12. C: 23, 0: Final newline missing (missing-final-newline)  
    13. C: 11, 0: Invalid constant name "YHTriangle" (invalid-name)  
    14.   
    15.   
    16. Report  
    17. ======  
    18. 13 statements analysed.  
    19.   
    20. Statistics by type  
    21. ------------------  
    22.   
    23. +---------+-------+-----------+-----------+------------+---------+  
    24. |type     |number |old number |difference |%documented |%badname |  
    25. +=========+=======+===========+===========+============+=========+  
    26. |module   |1      |1          |=          |100.00      |0.00     |  
    27. +---------+-------+-----------+-----------+------------+---------+  
    28. |class    |0      |0          |=          |0           |0        |  
    29. +---------+-------+-----------+-----------+------------+---------+  
    30. |method   |0      |0          |=          |0           |0        |  
    31. +---------+-------+-----------+-----------+------------+---------+  
    32. |function |0      |0          |=          |0           |0        |  
    33. +---------+-------+-----------+-----------+------------+---------+  
    34.   
    35.   
    36.   
    37. Raw metrics  
    38. -----------  
    39.   
    40. +----------+-------+------+---------+-----------+  
    41. |type      |number |%     |previous |difference |  
    42. +==========+=======+======+=========+===========+  
    43. |code      |15     |62.50 |15       |=          |  
    44. +----------+-------+------+---------+-----------+  
    45. |docstring |5      |20.83 |5        |=          |  
    46. +----------+-------+------+---------+-----------+  
    47. |comment   |2      |8.33  |17       |-15.00     |  
    48. +----------+-------+------+---------+-----------+  
    49. |empty     |2      |8.33  |3        |-1.00      |  
    50. +----------+-------+------+---------+-----------+  
    51.   
    52.   
    53.   
    54. Duplication  
    55. -----------  
    56.   
    57. +-------------------------+------+---------+-----------+  
    58. |                         |now   |previous |difference |  
    59. +=========================+======+=========+===========+  
    60. |nb duplicated lines      |0     |0        |=          |  
    61. +-------------------------+------+---------+-----------+  
    62. |percent duplicated lines |0.000 |0.000    |=          |  
    63. +-------------------------+------+---------+-----------+  
    64.   
    65.   
    66.   
    67. Messages by category  
    68. --------------------  
    69.   
    70. +-----------+-------+---------+-----------+  
    71. |type       |number |previous |difference |  
    72. +===========+=======+=========+===========+  
    73. |convention |4      |6        |-2.00      |  
    74. +-----------+-------+---------+-----------+  
    75. |refactor   |0      |0        |=          |  
    76. +-----------+-------+---------+-----------+  
    77. |warning    |0      |0        |=          |  
    78. +-----------+-------+---------+-----------+  
    79. |error      |0      |0        |=          |  
    80. +-----------+-------+---------+-----------+  
    81.   
    82.   
    83.   
    84. Messages  
    85. --------  
    86.   
    87. +----------------------+------------+  
    88. |message id            |occurrences |  
    89. +======================+============+  
    90. |bad-whitespace        |2           |  
    91. +----------------------+------------+  
    92. |missing-final-newline |1           |  
    93. +----------------------+------------+  
    94. |invalid-name          |1           |  
    95. +----------------------+------------+  
    96.   
    97.   
    98.   
    99. Global evaluation  
    100. -----------------  
    101. Your code has been rated at 6.92/10 (previous run: 5.38/10, +1.54)  


    ‘’‘’Report

    =======‘’‘’以上是Pylint对程序的建议,以下都是报告内容。多数时候,我们其实并不想看那么冗长的报告。这时候,体贴的Pylint就让我们屏蔽掉它们。

    加上参数 --reports=n 或者更简单写成 -rn 就好了。再看检查结果:

    1. c:>c:Python27Scriptspylint.exe --reports=n d:workspacePyCommsrcInterview  
    2. syanghui.py  
    3. No config file found, using default configuration  
    4. ************* Module Interviews.yanghui  
    5. C: 19, 0: Exactly one space required after comma  
    6.         for j in range(1,i):    #中间元素  
    7.                         ^ (bad-whitespace)  
    8. C: 21, 0: Exactly one space required after comma  
    9.                 YHTriangle[i].insert(j,YHTriangle[i-1][j-1] + YHTriangle[i-1][j]  
    10. )  
    11.                                       ^ (bad-whitespace)  
    12. C: 23, 0: Final newline missing (missing-final-newline)  
    13. C: 11, 0: Invalid constant name "YHTriangle" (invalid-name)  

    建议分三种:“bad-whitespace”, "missing-final-newline", "invalid-name".

    前两个好说,加空格,加空行。但是下面这个怎么破?

    1. C: 11, 0: Invalid constant name "YHTriangle" (invalid-name)  


    命名不规范?认为是个constant值? 其实可以把这类问题忽略掉。

    “Invalid constant name” 错误号是 C0103,所以加上 --disable=C0103即可。

    1. c:>c:Python27Scriptspylint.exe --reports=--disable=c0103 d:workspacePyCo  
    2. mmsrcInterviewsyanghui.py  
    3. No config file found, using default configuration  

    没输出就是说名没问题了。

    当然,还有更多的参数可以供选择。

    =======================华丽丽的分割线===============================

    平时写Python,我们几乎都不直接用命令行,而是用集成的IDE工具。比如猫哥常用的PyCharm。

    Pylint官方文档提了可以支持PyCharm不过太简略了。

    实际操作是这样的:

    进入PyCharm,从菜单栏,依次进入: File -> Settings -> Tools -> External Tools。

    “+”,进行添加。需要填写的部分分别是:“Name”,“Tool Settings -> Programs”、“Tool Settings -> Parameters”、“Tool Settings -> Working directory”。

    注意:

    “Parameters”里其它参数不管怎么写,必须在最后加上$FilePath$,“Working directory”里必须写 $FileDir


    另外,需要再添加“Output Filter”,在上图中间靠右。填写内容的“Regular expression to match output”,必须是:$FILE_PATH$:$LINE$: 最后那个是冒号。


    配置完毕,选择一个Python程序,右键点击,快捷菜单中会有“Extensions Tools -> Pylint”,点击运行即可。输出结果在执行程序结果的窗口(IDE下半部分)。

    如果看到返回值为0,说明程序没问题了。

      1. C:Python27Scriptspylint.exe --reports=--disable=C0103 D:PyCharmSpace est1 est1hello.py  
      2. No config file found, using default configuration  
      3.   
      4. Process finished with exit code 0 
  • 相关阅读:
    两栏自适应布局
    说说bfc 和 HasLayout
    春联式弹动广告
    Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
    2016年新剧《欢乐颂》里面的经典台词
    硬盘怎么保养
    如何保养与维护笔记本硬盘
    仔细看完,你会成为微信高手
    电脑之间用网线直接连接起来快速传送大文件技巧
    阿里云9折优惠码 GQH4IT 使用方法如下:
  • 原文地址:https://www.cnblogs.com/mhc-fly/p/7857620.html
Copyright © 2011-2022 走看看