Python自动格式化插件autopep8

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

# NOTE: 对文件进行格式化
$ autopep8 --in-place --aggressive --aggressive <filename>

扩展阅读

autopep8 1.3.2
https://pypi.python.org/pypi/autopep8/1.3.2
简介:

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.)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,593评论 6 427
  • WebDriver基础 欢迎阅读WebDriver基础讲义。本篇讲义将会重点介绍Selenium WebDrive...
    厲铆兄阅读 5,536评论 3 22
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • 1.什么是EventBus EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Int...
    symsimmy阅读 1,968评论 0 0
  • 1.不要把事情看得太认真,任何事情都不要较真,(用我们的话说就是较真,自己跟自己较劲)放过自己,好好爱自己。 2....
    徐小蕾阅读 192评论 0 0