学习资料: https://github.com/hhatto/autopep8

背景

autopep8 会根据 PEP 8 样式文档来格式化 python 代码。它使用 pep8 来决定代码的哪部分需要被格式化。 autopep8 可以修复 pep8汇报的大部分格式问题。

PEP8 是 Python Enhancement Proposal 的缩写,翻译过来就是 python 增强建议书, 是python的一个官方样式指导。它规定了一些比较好的编码方式,比如用4个空格代替缩进等等。

PEP 0008 -- Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/

安装

$ pip install --upgrade autopep8

用法

usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename]
[--ignore-local-config] [-r] [-j n] [-p n] [-a]
[--experimental] [--exclude globs] [--list-fixes]
[--ignore errors] [--select errors] [--max-line-length n]
[--line-range line line]
[files [files ...]] Automatically formats Python code to conform to the PEP style guide. positional arguments:
files files to format or '-' for standard in optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-v, --verbose print verbose messages; multiple -v result in more
verbose messages
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
--global-config filename
path to a global pep8 config file; if this file does
not exist then this is ignored (default:
~/.config/pep8)
--ignore-local-config
don't look for and apply local config files; if not
passed, defaults are updated with any config files in
the project's root directory
-r, --recursive run recursively over directories; must be used with
--in-place or --diff
-j n, --jobs n number of parallel jobs; match CPU count if value is
less than
-p n, --pep8-passes n
maximum number of additional pep8 passes (default:
infinite)
-a, --aggressive enable non-whitespace changes; multiple -a result in
more aggressive changes
--experimental enable experimental fixes
--exclude globs exclude file/directory names that match these comma-
separated globs
--list-fixes list codes for fixes; used by --ignore and --select
--ignore errors do not fix these errors/warnings (default: E24)
--select errors fix only these errors/warnings (e.g. E4,W)
--max-line-length n set maximum allowed line length (default: )
--line-range line line, --range line line
only fix errors found within this inclusive range of
line numbers (e.g. ); line numbers are indexed at

特性

autopep8会修复pep8汇报的下列问题:

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 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. E112/E113

2. E265

autopep8 也会修复一个非pep8汇报的问题:

1. 纠正弃用的以及非惯用的python代码(通过 lib2to3)。这会让python 2.6 以及 python 2.7 的代码更加与 python3 兼容。(如果 W690 是enable的,这一项修复会被触发)

2. 标准化具有多种行结束符的文件。

3. 在类申明和它的第一个方法申明中间加一个空行。(由 E309 enable)

4. 在类文档和它的第一个方法申明中间加一个空行。(由 E301 enable)

5. 移除方法申明和它的文档之间的空行。 (由 E303 enable )

具体用法

1. 使用 autopep8 更改一个文件。

假设文件名是 a.py,文件内容如下:

import math, sys;

def example1():
####This is a long comment. This should be wrapped to fit within 72 characters.
some_tuple=( 1,2, 3,'a' );
some_variable={'long':'Long code lines should be wrapped within 79 characters.',
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
20,300,40000,500000000,60000000000000000]}}
return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3( object ):
def __init__ ( self, bar ):
#Comments should have a space after the hash.
if bar : bar+=1; bar=bar* bar ; return bar
else:
some_string = """
Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
return (sys.path, some_string)

转到 Python3\Scripts 路径,可以看到 autopep8.exe 文件。

在a.py路径下,打开cmd,执行下列命令:

D:\Python3\Scripts\autopep8 --in-place --aggressive --aggressive a.py

备注:

--in-place: 以更改后的文件直接替代源文件。

--aggressive --aggressive : 以 aggressive 等级2 来更改源文件。

更改之后,a.py内容如下:

import math
import sys def example1():
# This is a long comment. This should be wrapped to fit within 72
# characters.
some_tuple = (1, 2, 3, 'a')
some_variable = {
'long': 'Long code lines should be wrapped within 79 characters.',
'other': [
math.pi,
100,
200,
300,
9876543210,
'This is a long string that goes on'],
'more': {
'inner': 'This whole logical line should be wrapped.',
some_tuple: [
1,
20,
300,
40000,
500000000,
60000000000000000]}}
return (some_tuple, some_variable) def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True} class Example3(object): def __init__(self, bar):
# Comments should have a space after the hash.
if bar:
bar += 1
bar = bar * bar
return bar
else:
some_string = """
Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
return (sys.path, some_string)

2. aggressive 等级

默认情况下,autopep8只修复空格问题。所以,默认情况下,autopep8不会修复 E711 和 E712 ,也不会修复弃用的代码 W6。

为了应用这些更加 aggressive 的修复,使用 --aggressive 选项:

autopep8 --aggressive a.py

使用多个 --aggressive 可以增加 aggressiveness 等级。

比如, E712 需要 aggressiveness 等级2.

autopep8 --aggressive  --aggressive a.py

3. 选择修复部分规范

只修复一部分规范,使用 --select 选项。

比如,修复多种缩进问题:

$ autopep8 --select=E1,W1 a.py

仅仅修复弃用代码问题:

$ autopep8 --aggressive --select=W6 a.py

4. 显示详细的过程信息

$ autopep8 -v a.py

5. 以模块的方式使用

>>> import autopep8
>>> autopep8.fix_code('x= 123\n')
'x = 123\n'

6. 以模块的方式使用,且带有条件

>>> import autopep8
>>> autopep8.fix_code('x.has_key(y)\n', options={'aggressive': 1})
'y in x\n'
>>> autopep8.fix_code('print( 123 )\n', options={'ignore': ['E']})
'print( 123 )\n'

python 代码格式化工具:autopep8的更多相关文章

  1. YAPF:Google开源的Python代码格式化工具

    点这里 现在的大多数 Python 代码格式化工具(比如:autopep8 和 pep8ify)是可以移除代码中的 lint 错误.这显然有些局限性.比如:遵循 PEP 8 指导的代码可能就不会被格式 ...

  2. python 代码格式化工具:YAPF

    学习资料: https://github.com/google/yapf 背景 现在的大多数 Python 代码格式化工具(比如:autopep8 和 pep8ify)是可以移除代码中的 lint 错 ...

  3. 推荐一个小而美的Python代码格式化工具

    代码可读性是评判代码质量的标准之一,有一个衡量代码质量的标准是 Martin 提出的 “WFT” 定律,即每分钟爆出 “WTF” 的次数.你在读别人代码或者做 Code Review 的时候有没有 “ ...

  4. Google的Python代码格式化工具YAPF详解

    平时习惯了杂乱无章地编写代码,而最后的代码勘定,却依赖于PyCharm自带的格式化工具,以及其自带的提示功能来规范代码.而pycharm里的格式化工具,不支持对多文件进行代码批量格式化,曾经尝试些解决 ...

  5. python 代码格式化工具:pep8ify

    资料: https://github.com/spulec/pep8ify 安装 $ pip install pep8ify 用法 Usage: 2to3 [options] file|dir ... ...

  6. PyCharm配置autopep8,自动格式化Python代码

    1. 关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:h ...

  7. 让 Python 代码更易维护的七种武器——代码风格(pylint、Flake8、Isort、Autopep8、Yapf、Black)测试覆盖率(Coverage)CI(JK)

    让 Python 代码更易维护的七种武器 2018/09/29 · 基础知识 · 武器 原文出处: Jeff Triplett   译文出处:linux中国-Hank Chow    检查你的代码的质 ...

  8. python代码风格指南:pep8 中文翻译

    摘要 本文给出主Python版本标准库的编码约定.CPython的C代码风格参见​PEP7.本文和​PEP 257 文档字符串标准改编自Guido最初的<Python Style Guide&g ...

  9. 我在 Ubuntu 下使用 Sublime 编写 python 代码时遇到并解决的问题

    Ubuntu 下 Sublime 无法输入中文 解决方法如下: sudo apt-get update && sudo apt-get upgrade 克隆项目到本地 : git cl ...

随机推荐

  1. SQL Server 表字段值转换成字段名称(二)

    上次写了个比较简单的只有两个字段的例子,经要求在写个  3 个字段的示例 ,贴上来与大家共勉一下   如果你们有更好的方法,提供一下, 感激不尽. 示例如下: /*--drop table temp_ ...

  2. 打造属于前端的Uri解析器

    今天和大家一起讨论一下如何打造一个属于前端的url参数解析器.如果你是一个Web开发工程师,如果你了解过后端开发语言,譬如:PHP,Java等,那么你对下面的代码应该不会陌生: $kw = $_GET ...

  3. C# div、css

    目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...

  4. Java----多线程知识点归纳(概念)

    一.线程与进程的区别: 多个进程的内部数据和状态都是完全独立的,而多线程是共享一块内存空间和一组系统资源,有可能互相影响. ?线程本身的数据通常只有寄存器数据,以及一个 程序执行时使用的堆栈,所以线程 ...

  5. 通过移位与或非运算获取整形最大值,最小值,以及获取输入的int类型整数的二进制表示

    以上是最终效果 实现类: package com.corejava.chap02; public class IntBin { private int value; public IntBin(int ...

  6. SGU 117.Counting

    时间限制: 0.25 sec. 空间限制: 4096 KB 题目大意: 给你n,m,k(都小于10001),和 n 个数,求这n个数中有多少个数的m次幂能够整除k.(即 n i^m % k==0). ...

  7. 【HDU2815】【拓展BSGS】Mod Tree

    Problem Description   The picture indicates a tree, every node has 2 children.  The depth of the nod ...

  8. 【POJ3468】【zkw线段树】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  9. PowerDesigner 的7种建模文件

    1.  概念数据模型 (CDM) 对数据和信息进行建模,利用实体-关系图(E-R图)的形式组织数据,检验数据设计的有效性和合理性. 2.  逻辑数据模型 (LDM) PowerDesigner 15 ...

  10. BeanUtils的日期问题

    //注册日期类型转换器 //第一种  自定义方法            ConvertUtils.register(new Converter(){                //第一个参数是目标 ...