点这里

现在的大多数 Python 代码格式化工具(比如:autopep8 和 pep8ify)是可以移除代码中的 lint 错误。这显然有些局限性。比如:遵循 PEP 8 指导的代码可能就不会被格式化了,但这并不说明代码看起来就舒服。

译注:lint 是最著名的C语言工具之一,是由贝尔实验室SteveJohnson于1979在PCC(PortableC Compiler)基础上开发的静态代码分析,一般由UNIX系统提供。与大多数C语言编译器相比,lint可以对程序进行更加广泛的错误分析,是一种更加严密的编译工具。最初,lint这个工具用来扫描C源文件并对源程序中不可移植的代码提出警告。但是现在大多数lint实用程序已经变得更加严密,它不但可以检查出可移植性问题,而且可以检查出那些虽然可移植并且完全合乎语法但却很可能是错误的特性。

但 YAPF 独辟蹊径。它脱胎于由 Daniel Jasper 开发的 clang-format。大体上来说,这个算法获取代码,然后把初始代码重新编排,即便初始代码并没有违背规范,也可使其达到遵循代码规范的最佳格式。这个理念和 Go 语言中的 gofmt 工具相似,终结关于格式的各种“圣战”。如果一个项目的代码库,无论何时修改,通过 YAPF 优化后,代码风格可统一,在每次 code review 中,也就没有必要争论风格了。

YAPF 的终极目标是生成和遵循代码规范的程序员写出的一样的代码。可帮你减少维护代码的苦差事。

YAPF 支持 Python 2.7 和 3.4+。

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
usage: yapf [-h] [--style STYLE] [-d | -i] [-l START-END | -r] ...
 
Formatter for Python code.
 
positional arguments:
  files
 
optional arguments:
  -h, --help            show this help message and exit
  --style STYLE         specify formatting style: either a style name (for
                        example "pep8" or "google"), or the name of a file
                        with style settings. pep8 is the default.
  -d, --diff            print the diff for the fixed source
  -i, --in-place        make changes to files in place
  -l START-END, --lines START-END
                        range of lines to reformat, one-based
  -r, --recursive       run recursively over directories

示例

美化前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
= {  'a':37,'b':42,
 
'c':927}
 
= 'hello ''world'
= 'hello '+'world'
= 'hello {}'.format('world')
class foo  (     object  ):
  def f    (self   ):
    return       37*-+2
  def g(self, x,y=42):
      return y
def f  (   a ) :
  return      37+-+a[42-x :  y**3]

美化后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
= {'a'37'b'42'c'927}
 
= 'hello ' 'world'
= 'hello ' + 'world'
= 'hello {}'.format('world')
 
class foo(object):
    def f(self):
        return 37 * -+2
 
    def g(self, x, y=42):
        return y
 
def f(a):
    return 37 + -+a[42 - x:y ** 3]

详情请参考下方的 Github 链接。

GitHub:https://github.com/google/yapf

YAPF:Google开源的Python代码格式化工具的更多相关文章

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

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

  2. Google 开源的 Python 命令行库:初探 fire

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  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. Google 开源的 Python 命令行库:深入 fire(二)

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  7. Google 开源的 Python 命令行库:fire 实现 git 命令

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  8. Google 开源的 Python 命令行库:深入 fire(一)

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

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

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

随机推荐

  1. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  2. 安装Oracle11g时,检测到系统的主 IP 地址是 DHCP 分配的地址

    检查完成.此次检查的总体结果为: 失败 <<<< 问题: 安装检测到系统的主 IP 地址是 DHCP 分配的地址. 建议案: Oracle 支持在具有 DHCP 分配的 IP ...

  3. Git 设置别名[alias]

    工作中我经常设置一下别名... 别名就在[alias]后面,要删除别名,直接把对应的行删掉即可. 而当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中: $ cat .git ...

  4. C#判断字符串为空

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...

  5. hmmer 使用(转载)

    hmmer 使用 » 转载文章请注明,转载自:博耘生物 » <hmmer的安装与使用> » 原文链接:http://boyun.sh.cn/bio/?p=1753   从功能基因研究的角度 ...

  6. 35.在PCB中删除元件

    在PCB Editor里面,如果想进行什么操作,首先得点击这个命令,再点击你要操作的区域/元件,最后右键选择"Done",这样你才能完成一个操作.

  7. C++实现的哈希搜索

    C++实现的哈希搜索 程序内容 Complete a text searching engine using hash table. 完成一个文本搜索引擎,使用哈希表 程序设计 程序流程图 程序代码 ...

  8. AppDelegate中的方法解析

    // 当应用程序启动完毕的时候就会调用(系统自动调用) -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOp ...

  9. ionic:Build mobile apps faster with the web technologies you know and love

    http://ionicframework.com/getting-started/ 5 Ionic Framework App Development Tips and Tricks http:// ...

  10. MyEclipse下直接查看class文件 jadnt158下载

    在没有源文件的情况下,通过倒入两个jar文件就可以实现 文件链接:http://pan.baidu.com/share/link?shareid=372924537&uk=2435113113 ...