资料: 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. rdlc报表

    也是第一次接触报表这个东西.现在在我理解,报表无非就是两个内容,格式和数据. 格式没有多少了解,就记录了,以后再续.数据的绑定和结果的显示是怎么实现的呢? 今天的主角就是rdlc这个文件和Report ...

  2. CouchBase 遇到问题笔记(一)

    刚开始看CouchBase,按照官网给出的示例,边敲边理解,遇到了一个很奇怪的问题,如下代码: IView<IViewRow> view = client.GetView("be ...

  3. NRPE: Unable to read output 问题处理总结

    自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...

  4. objective-c相关知识点

    1,objective-c中实现线程同步: Mutexlock (互斥锁).NSCondition lock (条件锁)消息传送 2,UDP和TCP: TCP :传输控制协议,可以提供面向连接的.可靠 ...

  5. 解决ld: warning: directory not found for option警告

    去掉警告的办法如下: 1选择工程, 编译的 (targets) 2选择 Build Settings 菜单 3查找 Library Search Paths 和 Framework Search Pa ...

  6. Python:标准数据类型6种

    #!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行有多个语句,用分号分割(;) print("aaa") ;print(" ...

  7. javascript——浅谈javascript模版(自定义)

    /** * Created by Administrator on 15-1-19. */ function functionUtil() { } functionUtil = { //某个DOM节点 ...

  8. iOS 相机手动对焦

    AVCaptureDevice的方法,之前查了网上和stackoverflow上,没有,于是自己试着做了下,成功了,分享下. //实例化 AVCaptureDevice *captureDevice ...

  9. 我和CPP的第二次约会

    1.变量之间的运算形式依赖于变量的数据类型,如i = i + j;当 i 和 j 是整型或者浮点型,则代表两个数的相加,如果是第一章所说的Sales_item类型,那么就是这两个变量的成分相加(如果书 ...

  10. Connect mysql on Linux from Windows

    ON LINUX: 1 sudo apt-get install mysql-server 2 sudo apt-get install python-dev 3 sudo apt-get insta ...