资料: https://github.com/spulec/pep8ify

安装

$ pip install pep8ify

用法

Usage: 2to3 [options] file|dir ...

Options:
-h, --help show this help message and exit
-d, --doctests_only Fix up doctests only
-f FIX, --fix=FIX Each FIX specifies a transformation; default: all
-j PROCESSES, --processes=PROCESSES
Run 2to3 concurrently
-x NOFIX, --nofix=NOFIX
Prevent a transformation from being run
-l, --list-fixes List available transformations
-p, --print-function Modify the grammar so that print() is a function
-v, --verbose More verbose logging
--no-diffs Don't show diffs of the refactoring
-w, --write Write back modified files
-n, --nobackups Don't write backups for modified files
-o OUTPUT_DIR, --output-dir=OUTPUT_DIR
Put output files in this directory instead of
overwriting the input files. Requires -n.
-W, --write-unchanged-files
Also write files even if no changes were required
(useful with --output-dir); implies -w.
--add-suffix=ADD_SUFFIX
Append this string to all output filenames. Requires
-n if non-empty. ex: --add-suffix='' will generate
.py3 files.

修复的问题

Available transformations for the -f/--fix option:
blank_lines
compound_statements
extraneous_whitespace
imports_on_separate_lines
indentation
maximum_line_length
missing_newline
missing_whitespace
tabs
trailing_blank_lines
trailing_whitespace
whitespace_around_operator
whitespace_before_inline_comment
whitespace_before_parameters

具体使用方法

假设当前路径下有一个文件夹a,a目录下有一个源文件a.py,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)

1. 查看需要更改的diff信息。

备注:该操作执行之后,不会有任何真正意义上的变化,只是在终端打印出一些源文件和更改之后的文件的diff信息,源文件并没有变化。

D:\Python3\Scripts\pep8ify.exe a

2. 格式化文件/路径

备注: 该操作会在终端打印出源文件和更改之后的文件的diff信息,同时更新源文件,并且在路径下生成一个“文件名.bak”文件。

执行如下命令:

D:\Python3\Scripts\pep8ify.exe -w a

可以看到a.py内容更新,a路径下有一个a.py.bak文件,a.py.bak的内容为源文件内容。

3. 不生成bak文件

-n 选项不会生成备份。

如果没有使用版本控制系统,不要使用这个选项。

D:\Python3\Scripts\pep8ify.exe -w -n a

4. 应用指定的修复

-f 可以指定应用的特定修复

默认是all,但是all不包括“maximum_line_length”。

如果想应用 all + maximum_line_length:

$ pep8ify -f all -f maximum_line_length example.py

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

  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. python 代码格式化工具:autopep8

    学习资料: https://github.com/hhatto/autopep8 背景 autopep8 会根据 PEP 8 样式文档来格式化 python 代码.它使用 pep8 来决定代码的哪部分 ...

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

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

  6. python代码规范 自动优化工具Black

    自动优化工具Black 在众多代码格式化工具中,Black算是比较新的一个,它***的特点是可配置项比较少,个人认为这对于新手来说是件好事,因为我们不必过多考虑如何设置Black,让 Black 自己 ...

  7. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  8. if __name__== "__main__" 的意思(作用)python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

  9. Python 代码风格

    1 原则 在开始讨论Python社区所采用的具体标准或是由其他人推荐的建议之前,考虑一些总体原则非常重要. 请记住可读性标准的目标是提升可读性.这些规则存在的目的就是为了帮助人读写代码,而不是相反. ...

随机推荐

  1. Sqlserver通过链接服务器访问Oracle

    工作中遇到的情况,win 7 64位操作系统的就安装64位的oracle 客户端,然后重启sql service服务后再创建sql 链接服务器.关于如何创建链接服务器请查看下面的大神的链接: http ...

  2. TreeView checkbox

    C# TreeView checkbox 联动打勾 #region 将树的checkbox选中 private void setNodeTrue(Node selNode) { Node node = ...

  3. Nginx和Apache共存环境下apache获得真实IP

    自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...

  4. C#winform程序自定义鼠标样式

    public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...

  5. String、StringBuffer、StringBuilder

    也说String. String:不可变字符序列. StringBuffer:线程安全的可变字符序列. StringBuilder:StringBuffer的非线程安全实现,JDK1.5+. publ ...

  6. opencv安装及学习资料

    第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...

  7. 明解C语言,练习13-3,从文件中读入个人信息,按身高排序后显示

    #include <stdio.h> #define NUMBER 6 #define F_PATH "D:\\C_C++\\ec13-3\\hw.dat" typed ...

  8. hdu 4502吉哥系列故事——临时工计划 (简单DP)

    Problem Description 俗话说一分钱难倒英雄汉,高中几年下来,吉哥已经深深明白了这个道理,因此,新年开始存储一年的个人资金已经成了习惯,不过自从大学之后他不好意思再向大人要压岁钱了,只 ...

  9. Codeforces 543D Road Improvement(DP)

    题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...

  10. zTree异步生成数据时无法获取到子节点的选中状态

    最近在项目中遇到一个问题,需求如下: 根据选中不同的人员(ID)向后台发送ajax请求,通过返回的数据来生成该人员的权限访问树,该树目录最少为3级目录,在生成的时候会自动勾选上次保存过的选中状态,点击 ...