重要python工具-代码质量篇

作为一名开发者,无论是不是在IDE或者命令行,你应该使用那些有用的工具库武装自己。如果你没有考虑代码干净、可读和可维护性,你就很可能看不到一些工具的好处。

python禅道

% python -m this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
...

你经常会看到其他作者或者演讲者应用上述的python禅道,因为,它们真的是你应该遵照的代码规范。

PEP8标准

python 增强提议叫做 PEP8 是我们的好帮手。

值得一提的是有一个python包就叫做pep8,可以检查代码规范。安装方法:

% pip install pep8

使用例子:

% pep8 code.py
code.py:4:14: E231 missing whitespace after ','
code.py:6:18: E231 missing whitespace after ','
code.py:7:80: E501 line too long (81 > 79 characters)

pep8可以报告多数的违反PEP8规则的代码,然后你可以人工解决。

你可以在 这里 找到官方地址。

Pyflakes工具:

现在我们有了规范检查方法,我们还需要错误检查方法。在代码运行之前就知道一些错误,是不是很棒?一个很赞的工具就是Pyflakes ,你可以这样安装它:

% pip install pyflakes

简单例子:

% pyflakes code.py
code.py:14: undefined name 'operate'

你可以在 这里  找到官方地址。

Flake8工具:

Flake8 包含了上面提到的两个工具,既有规范检查又有运行前代码报错。 安装方法:

% pip install flake8

简单例子:

% flake8 code.py
code.py:4:14: E231 missing whitespace after ','
code.py:4:14: undefined name 'operate'
code.py:6:18: E231 missing whitespace after ','
code.py:7:80: E501 line too long (81 > 79 characters)

你可以在  这里 找到官方地址。

Hacking工具:

hacking 提供了检查OpenStack 代码指导样式 ,改进了 Flake8。安装方法:

% pip install hacking

你可以在 这里 找到官方地址。

Pylint工具:

Pylint 是你的个人代码质量检查工具 。它检查了许多 代码规则 ,并且给你需要的总结。安装方法:

% pip install pylint

使用例子:

% pylint code.py
************* Module code
C:  4, 0: Exactly one space required after comma
        for j,row in enumerate(array):
             ^ (bad-whitespace)
C:  6, 0: Exactly one space required after comma
            for i,column in enumerate(row):
                 ^ (bad-whitespace)
C: 19, 0: No space allowed before bracket
            sum(array[lmi(j-1)   ][lmi(i-1):lma(i+2, w)]),
                                 ^ (bad-whitespace)
C: 20, 0: No space allowed before bracket
            sum(array[j          ][lmi(i-1):lma(i+2, w)]),
                                 ^ (bad-whitespace)
C: 23, 0: Exactly one space required after comma
        return 1 if (alive and living_cells in (2,3)) or \
                                                 ^ (bad-whitespace)
C: 27, 0: Trailing whitespace (trailing-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Missing class docstring (missing-docstring)
C:  2, 4: Missing method docstring (missing-docstring)
W:  6,18: Unused variable 'column' (unused-variable)
C: 13, 4: Invalid argument name "h" (invalid-name)
C: 13, 4: Invalid argument name "w" (invalid-name)
C: 13, 4: Missing method docstring (missing-docstring)
R: 13, 4: Too many arguments (6/5) (too-many-arguments)
W: 14, 8: Statement seems to have no effect (pointless-statement)
E: 14, 8: Undefined variable 'a' (undefined-variable)
R: 13, 4: Method could be a function (no-self-use

你可以在 这里 找到官方地址。

Radon工具:

Radon 是一个可以检查源代码和一系列复杂度和指标的工具。你可以在 radon官方文档网站 上阅读细节。安装方法:

% pip install radon

使用例子:

% radon cc code.py
code.py
    M 13:4 Operation.get_successor - B
    C 1:0 Operation - A
    M 2:4 Operation.next_gen - A
    M 8:10 Process.next - C
    C 20:0 Process- C

B+的评分已经是不错了。

你可以在 这里 找到官方地址。

转自:http://aboumrad.info/essential-python-tools-quality.html

 

发布者

David 9

邮箱:yanchao727@gmail.com 微信: david9ml

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注