Python自動(dòng)格式化插件autopep8

# NOTE: 安裝autopep8
$ pip install --upgrade autopep8

# NOTE: 對(duì)文件進(jìn)行格式化
$ autopep8 --in-place --aggressive --aggressive <filename>

擴(kuò)展閱讀

autopep8 1.3.2
https://pypi.python.org/pypi/autopep8/1.3.2
簡(jiǎn)介:

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pycodestyle.

autopep8 fixes the following issues reported by pycodestyle:

E101 - Reindent all lines.
E11  - Fix indentation. (not include E112 and E113)
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.
E306 - Expected 1 blank line before a nested definition
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.
E722 - Fix bare except.
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.
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 also fixes some issues not found by pycodestyle.

Correct deprecated or non-idiomatic Python code (via lib2to3). Use this for making Python 2.6 and 2.7 code more compatible with Python 3. (This is triggered if W690 is enabled.)
Normalize files with mixed line endings.
Put a blank line between a class docstring and its first method declaration. (Enabled with E301.)
Remove blank lines between a function declaration and its docstring. (Enabled with E303.)
autopep8 avoids fixing some issues found by pycodestyle.

E112/E113 for non comments are reports of bad indentation that break syntax rules. These should not be modified at all.
E265, which refers to spacing after comment hash, is ignored if the comment looks like code. autopep8 avoids modifying these since they are not real comments. If you really want to get rid of the pycodestyle warning, consider just removing the commented-out code. (This can be automated via eradicate.)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,835評(píng)論 6 427
  • WebDriver基礎(chǔ) 歡迎閱讀WebDriver基礎(chǔ)講義。本篇講義將會(huì)重點(diǎn)介紹Selenium WebDrive...
    厲鉚兄閱讀 5,672評(píng)論 3 22
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評(píng)論 19 139
  • 1.什么是EventBus EventBus是一款針對(duì)Android優(yōu)化的發(fā)布/訂閱事件總線(xiàn).主要功能是替代Int...
    symsimmy閱讀 2,033評(píng)論 0 0
  • 1.不要把事情看得太認(rèn)真,任何事情都不要較真,(用我們的話(huà)說(shuō)就是較真,自己跟自己較勁)放過(guò)自己,好好愛(ài)自己。 2....
    徐小蕾閱讀 213評(píng)論 0 0

友情鏈接更多精彩內(nèi)容