python——autopep8模块

这是一个可以让python代码自动规范化的开源库,写好的python看着比较混乱,可以使用该库直接改善代码布局,提升可读性。也可以在IDE内(比如PyCharm)中预先配置该库的插件。

PEP8

python编程规范,是一种一致性的风格倡议,可以让代码看起来更整洁(当然,如果规范的规则破坏你原有整齐的风格,可以忽略它)。其中规则有:

  • 行限制的最大字符数为79
  • 每一级缩进使用4个空格
  • 导入通常在分开的行
  • 优先修改注释
  • ……

PEP8 规范原文链接:官网 中文版

autopep8

首先记着安装该库:

pip/pip3 install autopep8   #python2 或 python3

对于需要改的python程序,比如test.py,在命令行输入如下命令:

autopep8 -i -a test.py 或 autopep8 --in-place --aggressive test.py
# -i,--in-place 在原文件修改
# -a,--aggressive  允许非空格的改变
autopep8 -v test.py
# -v 表示打印出修改内容
autopep8 -i -a -a test.py
# 多个 -a 提升修改级别,一个-a会忽略一些问题

你可以通过--help参数来获取帮助,比如其他参数的使用和含义。
autopep8修改的问题有如下通过--select errors参数可以指定前面的序号来修改指定问题:

E101 - Reindent all lines.
E11  - Fix indentation.
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.
E129 - Fix visual indentation.
E131 - Fix hanging indent for unaligned continuation line.
E133 - Fix missing indentation for closing bracket.
E20  - Remove extraneous whitespace.
E211 - Remove extraneous whitespace.
E22  - Fix extraneous whitespace around keywords.
E224 - Remove extraneous whitespace around operator.
E225 - Fix missing 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.
E252 - Missing whitespace around parameter equals.
E26  - Fix spacing after comment hash for inline comments.
E265 - Fix spacing after comment hash for block comments.
E266 - Fix too many leading '#' 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.
E305 - Expected 2 blank lines after end of function or class.
E306 - Expected 1 blank line before a nested definition.
E401 - Put imports on separate lines.
E402 - Fix module level import not at top of file
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.
E713 - Use 'not in' for test for membership.
E714 - Use 'is not' test for object identity.
E721 - Use "isinstance()" instead of comparing types directly.
E722 - Fix bare except.
E731 - Use a def when use do not assign a lambda expression.
W291 - Remove trailing whitespace.
W292 - Add a single newline at the end of the file.
W293 - Remove trailing whitespace on blank line.
W391 - Remove trailing blank lines.
W503 - Fix line break before binary operator.
W504 - Fix line break after binary operator.
W601 - Use "in" rather than "has_key()".
W602 - Fix deprecated form of raising exception.
W603 - Use "!=" instead of "<>"
W604 - Use "repr()" instead of backticks.
W605 - Fix invalid escape sequence 'x'.
W690 - Fix various deprecated code (via lib2to3).
示例

原始test.py文件:
这是一个乱糟糟的代码图~
自动化修改后:
这是一个整齐一些的代码图~
当然,这个例子展示的只是一小部分,感觉没有显示出该库的便利之处——让混乱的代码变得整洁易读,读者不妨自行试一试~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章