Introduction
Category
现有的 python 代码格式化工具(其中: autopep8, pep8ify)可以移除代码中的 lint 错误.
其中lint
是最著名的 C 语言工具之一, 用于对程序进行更加广泛的错误分析, 是一种
更加严密的编译工具, 用于扫描源文件并对符合语法但是错误的特性进行检查.
新的格式化工具, 见 google 开源的yapf
PEP8–Python Enhancement Proposal, 即 python 增强建议书, 官方样式指导, 规定了较好的编码方式.
Install
安装 autopep8:
1
| pip install --upgrade autopep8
|
详细信息见autopep8.
Command
修复问题
autopep8 会修复如下问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| E101 - Reindent all lines. E121 - Fix indentation to be a multiple of four. E122 - Add absent indentation for hanging indentation. E123 - Align closing bracket to match opening bracket. E124 - Align closing bracket to match visual indentation. E125 - Indent to distinguish line from next logical line. E126 - Fix over-indented hanging indentation. E127 - Fix visual indentation. E128 - Fix visual indentation. E20 - Remove extraneous whitespace. E211 - Remove extraneous whitespace. E22 - Fix extraneous whitespace around keywords. E224 - Remove extraneous whitespace around operator. E226 - Fix missing whitespace around arithmetic operator. E227 - Fix missing whitespace around bitwise/shift operator. E228 - Fix missing whitespace around modulo operator. E231 - Add missing whitespace. E241 - Fix extraneous whitespace around keywords. E242 - Remove extraneous whitespace around operator. E251 - Remove whitespace around parameter '=' sign. E26 - Fix spacing after comment hash for inline comments. E265 - Fix spacing after comment hash for block comments. E27 - Fix extraneous whitespace around keywords. E301 - Add missing blank line. E302 - Add missing 2 blank lines. E303 - Remove extra blank lines. E304 - Remove blank line following function decorator. E309 - Add missing blank line (after class declaration). E401 - Put imports on separate lines. E501 - Try to make lines fit within --max-line-length characters. E502 - Remove extraneous escape of newline. E701 - Put colon-separated compound statement on separate lines. E70 - Put semicolon-separated compound statement on separate lines. E711 - Fix comparison with None. E712 - Fix comparison with boolean. E721 - Use "isinstance()" instead of comparing types directly. W291 - Remove trailing whitespace. W293 - Remove trailing whitespace on blank line. W391 - Remove trailing blank lines. W601 - Use "in" rather than "has_key()". W602 - Fix deprecated form of raising exception. W603 - Use "!=" instead of "<>" W604 - Use "repr()" instead of backticks. W690 - Fix various deprecated code (via lib2to3).
|
同时, autopep8 会修复一些非 pep8 汇报的问题:
1 2 3 4 5
| O1 - 纠正弃用的以及非惯用的python代码 O2 - 标准化具有多种行结束符的文件 O3 - 在类申明和它的第一个方法申明中间加一个空行 O4 - 在类文档和它的第一个方法申明中间加一个空行 O5 - 移除方法申明和它的文档之间的空行
|
Options
命令格式:
命令参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| -v 使用调试, 详细的显示转换过程
--recursive 递归修复当前目录下的所有文件 --aggressive 默认情况下, autopep8仅仅修复空格问题, 为了应用复杂修复, 需要 增加aggressive, 可以通过增加--aggressive来增加修复的等级. 其中E712就需要等级2来进行修复.
--diff 类似git diff, 打印修复前修复后的文件对比 --in-place 以更改后的文件直接替换源文件 --select 有选择的修复部分规范 --ignore 有选择的忽略某些规范 --exclude globs 忽略指定的文件
--max-line-length 设置最大长度的规范 --line-range line1 line2 仅仅修复指定范围的行的代码
|
Example
修复当前目录下所有文件, 并使用等级1
来完成:
1
| autopep8 --recursive --aggressive --in-place .
|
以等级2
来修复更加复杂的功能:
1
| autopep8 --recursive --aggressive --aggressive --in-place .
|
选择修复部分规范:
1
| autopep8 --select=E1,W1,W6 --in-place --aggressive test.py
|
显示详细的过程:
1
| autopep8 -v --select=E1,W1,W6 --in-place --aggressive test.py
|