鱼C论坛

 找回密码
 立即注册
查看: 2095|回复: 2

[技术交流] Python 小技巧 071:Python 编码风格检查

[复制链接]
发表于 2020-3-5 19:39:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 编码风格检查


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

安装

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

  1. pip install pycodestyle
复制代码


使用

检测单个文件

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

  1. # -*- coding: utf-8 -*-
  2. a=2
  3. print(     4 ,a)
复制代码


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

  1. python -m pycodestyle --first 程序名.py
复制代码


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

  1. python -m pycodestyle --first demo.py
复制代码


执行结果:

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


其中这一部分:

  1. demo.py:2:2
  2. demo.py:3:7
  3. demo.py:3:13
  4. demo.py:3:14
  5. demo.py:3:17
复制代码


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

稍稍翻译一下:

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


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

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

显示详细信息

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

  1. python -m pycodestyle --show-source --show-pep8 程序名.py
复制代码


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

  1. python -m pycodestyle --show-source --show-pep8 demo.py
复制代码


执行结果:

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

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

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

  11.     Okay: i = i + 1
  12.     Okay: submitted += 1
  13.     Okay: x = x * 2 - 1
  14.     Okay: hypot2 = x * x + y * y
  15.     Okay: c = (a + b) * (a - b)
  16.     Okay: foo(bar, key='word', *args, **kwargs)
  17.     Okay: alpha[:-i]

  18.     E225: i=i+1
  19.     E225: submitted +=1
  20.     E225: x = x /2 - 1
  21.     E225: z = x **y
  22.     E226: c = (a+b) * (a-b)
  23.     E226: hypot2 = x*x + y*y
  24.     E227: c = a|b
  25.     E228: msg = fmt%(errno, errmsg)
  26. demo.py:3:7: E201 whitespace after '('
  27. print(     4 ,a)
  28.       ^
  29.     Avoid extraneous whitespace.

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

  33.     Okay: spam(ham[1], {eggs: 2})
  34.     E201: spam( ham[1], {eggs: 2})
  35.     E201: spam(ham[ 1], {eggs: 2})
  36.     E201: spam(ham[1], { eggs: 2})
  37.     E202: spam(ham[1], {eggs: 2} )
  38.     E202: spam(ham[1 ], {eggs: 2})
  39.     E202: spam(ham[1], {eggs: 2 })

  40.     E203: if x == 4: print x, y; x, y = y , x
  41.     E203: if x == 4: print x, y ; x, y = y, x
  42.     E203: if x == 4 : print x, y; x, y = y, x
  43. demo.py:3:13: E203 whitespace before ','
  44. print(     4 ,a)
  45.             ^
  46.     Avoid extraneous whitespace.

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

  50.     Okay: spam(ham[1], {eggs: 2})
  51.     E201: spam( ham[1], {eggs: 2})
  52.     E201: spam(ham[ 1], {eggs: 2})
  53.     E201: spam(ham[1], { eggs: 2})
  54.     E202: spam(ham[1], {eggs: 2} )
  55.     E202: spam(ham[1 ], {eggs: 2})
  56.     E202: spam(ham[1], {eggs: 2 })

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

  64.     Okay: [a, b]
  65.     Okay: (3,)
  66.     Okay: a[1:4]
  67.     Okay: a[:4]
  68.     Okay: a[1:]
  69.     Okay: a[1:4:2]
  70.     E231: ['a','b']
  71.     E231: foo(bar,baz)
  72.     E231: [{'a':'b'}]
  73. demo.py:3:17: W292 no newline at end of file
  74. print(     4 ,a)
  75.                 ^
  76.     Trailing blank lines are superfluous.

  77.     Okay: spam(1)
  78.     W391: spam(1)\n

  79.     However the last line should end with a new line (warning W292).
复制代码


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

评分

参与人数 1鱼币 +1 收起 理由
lijiachen + 1 能不能保存成文档?

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-5 21:37:59 | 显示全部楼层
vscode和pycharm自带的code formatting感觉就很爽
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-28 14:30:39 | 显示全部楼层
  1. python -m pycodestyle --first demo.py | clip
复制代码

然后输出的信息就到了你剪切板里了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 20:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表