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文件:
這是一個亂糟糟的代碼圖~
自動化修改後:
這是一個整齊一些的代碼圖~
當然,這個例子展示的只是一小部分,感覺沒有顯示出該庫的便利之處——讓混亂的代碼變得整潔易讀,讀者不妨自行試一試~

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