python coding style guide 的高速落地实践

python coding style guide 的高速落地实践


机器和人各有所长,如coding style检查这样的可自己主动化的工作理应交给机器去完毕,故发此文帮助你在几分钟内实现coding style的自己主动检查。


1.有哪些著名的Python Coding Style Guide

  • PEP8

https://www.python.org/dev/peps/pep-0008/

发明Python语言丰碑人物Guido van Rossum的亲自写的Coding Style, 知名度5颗星,可操作性5颗星。

  • Google Python Coding Style Guide

http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

Google内部广泛使用Python作为开发语言,此Coding Style 在坊间流传非常广。知名度5颗星,可操作性5颗星。

值得一提的是Guido也以前在Google工作过一段时间。

2.Flake8 - Coding Style检查自己主动化的利器

你可能听说过pep8。这是一个依据PEP8规范检查python代码style的自己主动化工具。

flake8是对pep8进行了包装,充分发挥了插件化的优势。添加了如代码复杂度,函数、变量命名习惯,import顺序等检查。

2.1 安装Flake8

安装flake8。同一时候安装一些实用的插件。

  • pep8-nameing

https://github.com/PyCQA/pep8-naming

命名检查

  • flake8-import-order

https://github.com/public/flake8-import-order

import 顺序检查。能够有两种风格顺序检查cryptography, google。如google的意思是import顺序是(1)标准库(2)第三方库(3)本地项目库。代码检查时能够通过--import-order-style=google来指定。

  • flake8-todo

https://github.com/schlamar/flake8-todo

检查代码中的todo。

  • flake8-quotes

https://github.com/zheller/flake8-quotes/

检查单双引號的使用是否正确。

详细安装命令例如以下:

$ pip install flake8
$ pip install pep8-naming
$ pip install flake8-import-order
$ pip install flake8-todo
$ pip install flake8-quotes

检查安装了哪些插件:

$ flake8 --version
# 输出例如以下内容,显示了已安装的插件:
2.5.1 (pep8: 1.5.7, import-order: 0.6.1, naming: 0.3.3, pyflakes: 1.0.0, mccabe: 0.3.1, flake8-todo: 0.4, flake8_quotes: 0.1.1) CPython 2.6.6 on Linux

2.2 用Flake8检查Python Codes

比如例如以下代码:

# test.py

from order import place_order
import os, sys

class website_api(object):
    def __init__(self):
        self.user_name = ‘‘
        self.Gender = ‘male‘
        #comment in wrong ident
        self.active =False

    def login(self, Person):
        self.user_name=Person.user_name
        not_used_var = 0
        return True

    def Logout(self):
        self.active =False

    def place_order(self):
        place_order()

def action():
    Client_a = website_api()
    Client_a.login()
    Client_a.place_order()
    Client_a.Logout()

运行检查命令:

$ flake8 --first --import-order-style=google test.py

输出结果例如以下,你能够依据错误码来修正代码,如当中的N802的意思是function name不应该包括大写英文字母。

# flake8 output
test.py:2:1: F401 ‘sys‘ imported but unused
test.py:2:1: I100 Imports statements are in the wrong order. from os, sys should be before from order
test.py:2:1: I201 Missing newline before sections or imports.
test.py:2:10: E401 multiple imports on one line
test.py:4:7: N801 class names should use CapWords convention
test.py:8:9: E265 block comment should start with ‘# ‘
test.py:9:22: E225 missing whitespace around operator
test.py:11:9: N803 argument name should be lowercase
test.py:13:9: F841 local variable ‘not_used_var‘ is assigned to but never used
test.py:16:9: N802 function name should be lowercase
test.py:23:5: N806 variable in function should be lowercase

除此之外。flake8也能够递归得检查某个文件夹中的代码:

$ flake8 your_project_dir

flake8经常使用的options有:

  • –show-source

show source code for each error

  • –first

show first occurrence of each error

  • –import-order-style=google

import order style to follow

  • –count

print total number of errors and warnings to standard error and set exit code to 1 if total is not null

  • –help

get help

2.3 Flake8 Warning / Error codes 列表

Codes Notes Link
E***/W*** pep8 errors and warnings http://pep8.readthedocs.org/en/latest/intro.html#error-codes
F*** PyFlakes codes (see below) https://flake8.readthedocs.org/en/latest/warnings.html
C9** McCabe complexity, 眼下仅仅有C901 https://github.com/PyCQA/mccabe
N8** PEP-8 naming conventions https://github.com/PyCQA/pep8-naming#plugin-for-flake8
I*** checks the ordering of your imports https://github.com/public/flake8-import-order#warnings
T*** 眼下仅仅有T000检查代码中是否包括TODO, FIXME https://github.com/schlamar/flake8-todo
Q*** 眼下有Q000代表单双引號使用错误 https://github.com/zheller/flake8-quotes/

随着新的flake8 plugin的集成,还可能有其它的codes,假设你的项目有特殊的代码检查需求。也可开发自己的plugin。

2.4 Flake8的个性化配置

依据须要,flake8的配置能够是全局的(对全部project有效)。也能够是分project的。这里仅举例说明全局配置方法。分project配置请见flake8 Per Project Configuration

编辑 ~/.config/flake8

[flake8]
ignore = E201,E202,E302
exclude = .tox,*.egg
max-line-length = 120

以上配置的意思是flake8不检查E201, E202, E302这三个错误,不检查.tox,*.egg文件。同意的最长单行代码长度为120个字符。

2.5 Flake8高级使用方法 - Git Commit Hook

flake8可结合Git实现commit时检查coding style的目的,假设flake8报错,则无法commit

在python project的根文件夹下运行例如以下命令安装git pre-commit hook。

$ flake8 --install-hook
$ git config flake8.strict true

References

  1. PEP8

    https://www.python.org/dev/peps/pep-0008/

  2. Google Python Coding Style

    http://google-styleguide.googlecode.com/svn/trunk/pyguide.html

  3. pep8工具

    https://github.com/PyCQA/pep8

  4. flake8

    https://flake8.readthedocs.org

  5. Warning / Error codes of flake8

    https://flake8.readthedocs.org/en/latest/warnings.html

时间: 2024-08-09 13:24:19

python coding style guide 的高速落地实践的相关文章

python coding style / python编程风格

#from <Python 2.7.9 documentation> Use 4-space indentation, and no tabs. #使用4空格缩进,不要使用tab缩进 Wrap lines so that they don’t exceed 79 characters. 拆行确保每行不超过79个字符 Use blank lines to separate functions and classes, and larger blocks of code inside functi

Python&#39;s Coding Style

一个程序员良好的素养可以从他的代码风格里看出. Python官方的开发者指南,PEP8中,列举了Style Guide for Python Code. 摘录The Python's Tutorial中的依依列出. 1.用4空格缩进,而不是tab键. 2.使每行不超过79个字符,目的是小屏幕用户也能很好的阅读. 3.用空一行的方式去把函数,类,和函数中大的代码块分开. 4.如果可能的话,尽量把注释写在一行里. 5.Use docstrings.使用文档字符串.(Ps.这个可得利用好了) 6.在操

Python Style Guide

Python代码风格规范. @1:参数缩进:(2种形式) <1> foo = long_function_name(var1, var2, var3, var4) #第1行有参数, 第2行参数与第1行对齐 <2> foo = long_function_name( var1, var2, var3, var4) #第1行须没有参数, 第2行前空4个空格, 第3行与第2行对齐 @2:顶级定义之间空2行, 方法定义之间空1行 类中第1个方法前总是应该空1行, 某些函数内部也可以适当的加

Google Python Style Guide

http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Comments http://zh-google-styleguide.readthedocs.org/en/latest/google-python-styleguide/python_language_rules/ Google Python Style Guide

Google Style Guides-Shell Style Guide

作者声明 这篇翻译文章对我来说是有点小挑战的.由于我英语实在非常烂,勉强能够看懂一些技术文档,能够猜出大概的含义.可是翻译对我来说算是一个挑战,看英文文档已经不是一天两天的事了,可是这个篇文章却是我的处女作,通读了这篇翻译后的文章,已经发现部分语句翻译的非常不好,可是我没有能力纠正好,加上时间上不同意,最后我还是厚着脸皮放到了博客上,假设有幸某位读者读到我这蹩脚的翻译.希望你能够在唾骂我的同一时候也给出正确的翻译让我能够修正.原文參见附录. 背景 使用什么shell? Bash是同意的可运行文件

Google&#39;s C++ coding style

v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成     头文件        #define用法        前向声明        内联函数        -inl.h文件        函数参数顺序        include的命名和顺序    作用域        命名空间            未命名空间            命名空间       

Shell Style Guide

Shell Style Guide Revision 1.26 Paul ArmstrongToo many more to mention Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: ▽. You may toggle all summaries with th

Google C++ Style Guide的哲学

Google C++ Style Guide并不是一个百科全书,也不是一个C++使用指南,但它描述适用于Google及其开源项目的编码指南,并不追求全面和绝对正确,也有许多人置疑它的一些规则.但作为一个最具影响力的编码规范,它里面有许多内容值得我们研究学习. 以下主要摘自GSG负责人Titus Winters在CppCon 2014上的演讲. 制订Google C++ Style Guide的目的 引导开发去做对的事,同时不易犯错. 哲学总结 关注于读者,而非作者 (Optimize for t

[CPP] Coding Style

C++ Coding Style C++很多强大的语言特性导致它的复杂,其复杂性会使得代码更容易出现bug.难于阅读和维护. 本Coding Style用来约束C++编程,使得代码在有效使用C++语言特性的同时易于管理[代码的一致性高于一切]. 参考:<Google C++ Style Guide> 分类 标题 规则 备注(示例) 头文件 每个.cc文件都应对应一个.h文件 #define保护 1.  #define PROJECT_PATH_FILE_H_ 防止.h文件被多重包含: 1. P