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
Google Python Coding Style
http://google-styleguide.googlecode.com/svn/trunk/pyguide.htmlWarning / Error codes of flake8
https://flake8.readthedocs.org/en/latest/warnings.html
python coding style guide 的高速落地实践的更多相关文章
- python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准
python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...
- Google coding Style Guide : Google 编码风格/代码风格 手册/指南
1 1 1 https://github.com/google/styleguide Google 编码风格/代码风格 手册/指南 Style guides for Google-originated ...
- PSR-2 Coding Style Guide
本文主要是对PSR-2 的简单翻译. 英文源址 http://www.php-fig.org/psr/psr-2/ PSR2继承和扩展PSR1--基本编码规范 本手册的目的是使用一系列共同遵守的编码格 ...
- The OpenCV Coding Style Guide
https://github.com/opencv/opencv/wiki/Coding_Style_Guide
- PEP 8 – Style Guide for Python Code
原文:PEP 8 – Style Guide for Python Code PEP:8 题目:Python代码风格指南 作者:Guido van Rossum, www.yszx11.cnBarry ...
- [Guide]Google Python Style Guide
扉页 项目主页 Google Style Guide Google 开源项目风格指南 - 中文版 背景 Python 是Google主要的脚本语言.这本风格指南主要包含的是针对python的编程准则. ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- Google's C++ coding style
v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成 头文件 ...
- Google Shell Style Guide
转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...
随机推荐
- (二) Spring项目的搭建
传统的项目搭建,是将所依赖的第三方jar包复制到项目的类路径下.但是,这样带来的问题是,无法更好的对这些jar包进行动态管理. 目前主流的构建工具有:Ant.Maven.Gradle.以Maven为例 ...
- 控制台或Winform程序中如何编码或解码Server.URLEncode
原文发布时间为:2010-07-10 -- 来源于本人的百度文章 [由搬家工具导入] 在Asp.net中可以使用Server.HTMLEncode和Server.URLEncode 将文本或URL的特 ...
- 【Visual Studio - Dependency Walker】查找程序依赖的动态链接库文件(转)
原文转自 http://163n.blog.163.com/blog/static/5603555220113151113287/ 有时我们需要知道一个程序依赖哪些动态链接库(DLL)文件.实际上,有 ...
- 转 C++拷贝构造函数详解
C++拷贝构造函数详解 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一 ...
- C# .NET4.0 改为 到.NET2.0 时 TypedTableBase 报错解决方法
.NET 4.0 降版本 到.NET 2.0.不出意外,问题必然来了. 编译错误一: 错误 1 命名空间“System”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?)解决: 删掉该引用 ...
- AC日记——[Sdoi2016]数字配对 bzoj 4514
4514 思路: 很受伤现在,,测了那么多次不过的原因就是因为INF不够大: 解法有两种: 解法1: 把n个点按照质因数个数为奇或偶分为两个点集(很容易就可以想到): 然后,按照题目连边跑最大费用流: ...
- my.ini配置详解
Mysql my.ini 配置文件详解 #BEGIN CONFIG INFO #DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大 #TYPE: SYSTEM ...
- mfoc安装编译
系统环境 UBUNTU16.04,要安装好各种基础的编译环境,这就不说了,如果不知道需要什么,那就编译时出错时看到什么安装什么吧 下载源码包libnfc, mfoc, mfcuk,都是github上, ...
- delphi如何把一个整数转化为4个十六进制字节
var s:string; len:Integer; AData:TBytes; begin s:=IntToHex(149259,6);//返回6位字符串 len ...
- ios开发小结之app发布升级
在近两个月的开发中,遇到了挺多问题的,几天加班加点,最后还是在年前发布并更新了一个版本,欢迎下载无觅下载. 最头疼的问题是提交app审核,之前的工程不太规范,导致一些文件icon没有设置好,直接val ...