zltzlt 发表于 2020-3-5 19:39:08

Python 小技巧 071:Python 编码风格检查

Python 编码风格检查

Pycodestyle 是一个第三方库,它用于对 Python 程序进行代码风格的检查(遵循 PEP8 中定义的规范)。

安装

直接在命令行使用 pip 安装即可:

pip install pycodestyle

使用

检测单个文件

假设现在有 demo.py ,内容如下:

# -*- coding: utf-8 -*-
a=2
print(   4 ,a)

在命令行中运行以下命令:

python -m pycodestyle --first 程序名.py

例如,想检测 demo.py 就运行以下命令:

python -m pycodestyle --first demo.py

执行结果:

demo.py:2:2: E225 missing whitespace around operator
demo.py:3:7: E201 whitespace after '('
demo.py:3:13: E203 whitespace before ','
demo.py:3:14: E231 missing whitespace after ','
demo.py:3:17: W292 no newline at end of file

其中这一部分:

demo.py:2:2
demo.py:3:7
demo.py:3:13
demo.py:3:14
demo.py:3:17

详细地交代了出现不符合规范的地方,用英文冒号分隔。第一个元素是文件名,第二个元素是出问题的行号,第三个元素是出问题的字符在行中的位置。

稍稍翻译一下:

demo.py:2:2: E225 运算符之间应该有空格
demo.py:3:7: E201 '(' 之后不应该有空格
demo.py:3:13: E203 ',' 之前不应该有空格
demo.py:3:14: E231 ',' 之后应该有空格
demo.py:3:17: W292 文件的末尾应该有

可以按照给出的提示更改程序代码,使代码变得更美观。

说明:如果程序代码完全符合规范,则没有输出。

显示详细信息

在命令行中运行以下命令:

python -m pycodestyle --show-source --show-pep8 程序名.py

还是以上面的 demo.py 为例:

python -m pycodestyle --show-source --show-pep8 demo.py

执行结果:

demo.py:2:2: E225 missing whitespace around operator
a=2
^
    Surround operators with a single space on either side.

    - Always surround these binary operators with a single space on
      either side: assignment (=), augmented assignment (+=, -= etc.),
      comparisons (==, <, >, !=, <=, >=, in, not in, is, is not),
      Booleans (and, or, not).

    - If operators with different priorities are used, consider adding
      whitespace around the operators with the lowest priorities.

    Okay: i = i + 1
    Okay: submitted += 1
    Okay: x = x * 2 - 1
    Okay: hypot2 = x * x + y * y
    Okay: c = (a + b) * (a - b)
    Okay: foo(bar, key='word', *args, **kwargs)
    Okay: alpha[:-i]

    E225: i=i+1
    E225: submitted +=1
    E225: x = x /2 - 1
    E225: z = x **y
    E226: c = (a+b) * (a-b)
    E226: hypot2 = x*x + y*y
    E227: c = a|b
    E228: msg = fmt%(errno, errmsg)
demo.py:3:7: E201 whitespace after '('
print(   4 ,a)
      ^
    Avoid extraneous whitespace.

    Avoid extraneous whitespace in these situations:
    - Immediately inside parentheses, brackets or braces.
    - Immediately before a comma, semicolon, or colon.

    Okay: spam(ham, {eggs: 2})
    E201: spam( ham, {eggs: 2})
    E201: spam(ham[ 1], {eggs: 2})
    E201: spam(ham, { eggs: 2})
    E202: spam(ham, {eggs: 2} )
    E202: spam(ham, {eggs: 2})
    E202: spam(ham, {eggs: 2 })

    E203: if x == 4: print x, y; x, y = y , x
    E203: if x == 4: print x, y ; x, y = y, x
    E203: if x == 4 : print x, y; x, y = y, x
demo.py:3:13: E203 whitespace before ','
print(   4 ,a)
            ^
    Avoid extraneous whitespace.

    Avoid extraneous whitespace in these situations:
    - Immediately inside parentheses, brackets or braces.
    - Immediately before a comma, semicolon, or colon.

    Okay: spam(ham, {eggs: 2})
    E201: spam( ham, {eggs: 2})
    E201: spam(ham[ 1], {eggs: 2})
    E201: spam(ham, { eggs: 2})
    E202: spam(ham, {eggs: 2} )
    E202: spam(ham, {eggs: 2})
    E202: spam(ham, {eggs: 2 })

    E203: if x == 4: print x, y; x, y = y , x
    E203: if x == 4: print x, y ; x, y = y, x
    E203: if x == 4 : print x, y; x, y = y, x
demo.py:3:14: E231 missing whitespace after ','
print(   4 ,a)
             ^
    Each comma, semicolon or colon should be followed by whitespace.

    Okay:
    Okay: (3,)
    Okay: a
    Okay: a[:4]
    Okay: a
    Okay: a
    E231: ['a','b']
    E231: foo(bar,baz)
    E231: [{'a':'b'}]
demo.py:3:17: W292 no newline at end of file
print(   4 ,a)
                ^
    Trailing blank lines are superfluous.

    Okay: spam(1)
    W391: spam(1)\n

    However the last line should end with a new line (warning W292).

可以看到 Pycodestyle 给出了详细的解释和建议的做法。

Levin-e 发表于 2020-3-5 21:37:59

vscode和pycharm自带的code formatting感觉就很爽{:10_279:}

_2_ 发表于 2020-3-28 14:30:39

python -m pycodestyle --first demo.py | clip
然后输出的信息就到了你剪切板里了{:10_256:}
页: [1]
查看完整版本: Python 小技巧 071:Python 编码风格检查