首页 » InfoTech »

Flake8初体验

2018年10月1日 / 15次阅读
Python

打开支付宝首页,搜索“529018372”,即可领取红包!可重复领。

Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强。

Flake8 是“将 PEP 8、Pyflakes(类似 Pylint)、McCabe(代码复杂性检查器)和第三方插件整合到一起,以检查 Python 代码风格和质量的一个 Python 工具”。与pyLint的功能一样,用于对代码的静态检测。

执行 pip install flake8 安装 flake8 ,然后执行 flake8 [options] path/to/dir 或者 flake8 [options] path/to/module.py 可以查看报出的错误和警告。

和 Pylint 类似,Flake8 允许通过配置文件来自定义检查的内容。它有非常清晰的文档,包括一些有用的提交钩子,可以将自动检查代码纳入到开发工作流程之中。

Flake8 也可以集成到一些流行的编辑器和 IDE 当中,但在文档中并没有详细说明。要将 Flake8 集成到喜欢的编辑器或 IDE 中,可以搜索插件(例如 Sublime Text 的 Flake8 插件)。

 

(BlogChecker) D:\pyve\BlogChecker>pip install flake8
Collecting flake8
Downloading https://files.pythonhosted.org/packages/b9/dc/14e9d94c770b8c4ef584
e906c7583e74864786a58d47de101f2767d50c0b/flake8-3.5.0-py2.py3-none-any.whl (69kB
)
43% |██████████████ | 30kB 7.5kB/s eta 0:00:0
58% |██████████████████▊ | 40kB 8.5kB/s eta 0
73% |███████████████████████▍ | 51kB 8.0kB/s
87% |████████████████████████████▏ | 61kB 9.6
100% |████████████████████████████████| 71kB
10kB/s
Collecting pyflakes<1.7.0,>=1.5.0 (from flake8)
Downloading https://files.pythonhosted.org/packages/d7/40/733bcc64da3161ae4122
c11e88269f276358ca29335468005cb0ee538665/pyflakes-1.6.0-py2.py3-none-any.whl (22
7kB)
45% |██████████████▍ | 102kB 10kB/s eta 0:00:
49% |███████████████▉ | 112kB 10kB/s eta 0:00
54% |█████████████████▎ | 122kB 9.1kB/s eta 0
58% |██████████████████▊ | 133kB 9.0kB/s eta
63% |████████████████████▏ | 143kB 8.9kB/s et
67% |█████████████████████▋ | 153kB 8.3kB/s e
72% |███████████████████████ | 163kB 8.0kB/s
76% |████████████████████████▌ | 174kB 9.2kB/
81% |██████████████████████████ | 184kB 9.1kB
85% |███████████████████████████▍ | 194kB 9.0
90% |████████████████████████████▉ | 204kB 9.
94% |██████████████████████████████▎ | 215kB
99% |███████████████████████████████▊| 225kB
100% |████████████████████████████████| 235k
B 11kB/s
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in d:\pyve\blogchecker\lib\s
ite-packages (from flake8) (0.6.1)
Collecting pycodestyle<2.4.0,>=2.0.0 (from flake8)
Downloading https://files.pythonhosted.org/packages/e4/81/78fe51eb4038d1388b72
17dd63770b0f428370207125047312886c923b26/pycodestyle-2.3.1-py2.py3-none-any.whl
(45kB)
45% |██████████████▌ | 20kB 9.7kB/s eta 0:00:
68% |█████████████████████▊ | 30kB 8.2kB/s et
90% |█████████████████████████████ | 40kB 8.2
100% |████████████████████████████████| 51kB
10kB/s
Installing collected packages: pyflakes, pycodestyle, flake8
Successfully installed flake8-3.5.0 pycodestyle-2.3.1 pyflakes-1.6.0

(BlogChecker) D:\pyve\BlogChecker>flake8 LinkCheck.py
LinkCheck.py:8:1: F401 'datetime as dt' imported but unused
LinkCheck.py:50:36: W291 trailing whitespace
LinkCheck.py:52:28: W291 trailing whitespace
LinkCheck.py:77:59: W291 trailing whitespace
LinkCheck.py:97:1: W293 blank line contains whitespace
LinkCheck.py:111:67: E712 comparison to False should be 'if cond is False:' or '
if not cond:'
LinkCheck.py:117:18: E712 comparison to False should be 'if cond is False:' or '
if not cond:'
LinkCheck.py:117:27: W291 trailing whitespace
LinkCheck.py:121:1: W293 blank line contains whitespace
LinkCheck.py:138:52: W291 trailing whitespace
LinkCheck.py:139:46: E231 missing whitespace after ':'
LinkCheck.py:154:9: E265 block comment should start with '# '
LinkCheck.py:155:9: E722 do not use bare except'
LinkCheck.py:162:53: W291 trailing whitespace
LinkCheck.py:169:44: W291 trailing whitespace
LinkCheck.py:174:60: W291 trailing whitespace
LinkCheck.py:175:77: W291 trailing whitespace
LinkCheck.py:176:76: W291 trailing whitespace
LinkCheck.py:193:74: W291 trailing whitespace
LinkCheck.py:209:13: W291 trailing whitespace
LinkCheck.py:210:51: W291 trailing whitespace
LinkCheck.py:211:25: E127 continuation line over-indented for visual indent
LinkCheck.py:211:47: W291 trailing whitespace
LinkCheck.py:212:25: E127 continuation line over-indented for visual indent
LinkCheck.py:212:46: E231 missing whitespace after ':'
LinkCheck.py:213:9: E722 do not use bare except'
LinkCheck.py:218:1: W293 blank line contains whitespace
LinkCheck.py:225:59: W291 trailing whitespace
LinkCheck.py:233:54: W291 trailing whitespace
LinkCheck.py:234:33: E128 continuation line under-indented for visual indent
LinkCheck.py:234:56: W291 trailing whitespace
LinkCheck.py:235:33: E128 continuation line under-indented for visual indent
LinkCheck.py:238:51: W291 trailing whitespace
LinkCheck.py:239:33: E128 continuation line under-indented for visual indent
LinkCheck.py:239:56: W291 trailing whitespace
LinkCheck.py:240:33: E128 continuation line under-indented for visual indent
LinkCheck.py:244:52: W291 trailing whitespace
LinkCheck.py:245:33: E128 continuation line under-indented for visual indent
LinkCheck.py:245:56: W291 trailing whitespace
LinkCheck.py:246:33: E128 continuation line under-indented for visual indent
LinkCheck.py:248:17: E265 block comment should start with '# '
LinkCheck.py:249:61: W291 trailing whitespace
LinkCheck.py:253:55: W291 trailing whitespace
LinkCheck.py:254:33: E128 continuation line under-indented for visual indent
LinkCheck.py:254:56: W291 trailing whitespace
LinkCheck.py:255:33: E128 continuation line under-indented for visual indent
LinkCheck.py:257:13: E265 block comment should start with '# '
LinkCheck.py:266:1: W293 blank line contains whitespace
LinkCheck.py:274:1: W293 blank line contains whitespace
LinkCheck.py:286:1: W293 blank line contains whitespace
LinkCheck.py:290:1: W293 blank line contains whitespace
LinkCheck.py:306:1: W293 blank line contains whitespace
LinkCheck.py:310:1: W293 blank line contains whitespace
LinkCheck.py:322:80: E501 line too long (144 > 79 characters)
LinkCheck.py:323:28: W291 trailing whitespace
LinkCheck.py:324:1: W293 blank line contains whitespace
LinkCheck.py:325:80: E501 line too long (87 > 79 characters)
LinkCheck.py:327:8: W291 trailing whitespace
LinkCheck.py:334:34: W291 trailing whitespace
LinkCheck.py:336:1: W293 blank line contains whitespace
LinkCheck.py:352:9: E265 block comment should start with '# '
LinkCheck.py:353:9: E265 block comment should start with '# '
LinkCheck.py:357:16: W292 no newline at end of file

不管是pyLint还是Flake8,我觉得都需要做适当的配置,才能适用特定的项目。

pyLink和Flake8检查出来的结果会稍有不同,pyLink默认太在意函数和变量以及类的命名,给我的代码爆出一大堆warning。但是,通过阅读PEP-0008,我的理解是,有好几种不同的命名规则供选择适用,并没有强制哪一种必须用在什么地方。这一点Flake8做的比较好,默认情况下,不会对我的命名“提出异议”。

建议使用这个flake8命令,忽略掉两个蛋疼的warning:flake8 --ignore W291,W293 yourPython.py

Flake8的说明文档:https://flake8.readthedocs.io

除了单独检查一个py文件,Flake8还可以检查一个文件夹里面所有的文件:

flake8 path/to/code/to/check.py
# or
flake8 path/to/code/

本文链接:http://www.maixj.net/ict/flake8-19061
云上小悟 麦新杰(QQ:1093023102)

相关文章

评论是美德

无力满足评论实名制,评论对非实名注册用户关闭,有事QQ:1093023102.


前一篇:
后一篇:

栏目精选

云上小悟,麦新杰的独立博客

Ctrl+D 收藏本页

栏目

AD

ppdai

©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。

网站二维码
go to top