python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准
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.html
Warning / Error codes of flake8 https://flake8.readthedocs.org/en/latest/warnings.html
Written with StackEdit.
python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准的更多相关文章
- python coding style guide 的高速落地实践
python coding style guide 的高速落地实践 机器和人各有所长,如coding style检查这样的可自己主动化的工作理应交给机器去完毕,故发此文帮助你在几分钟内实现coding ...
- Google coding Style Guide : Google 编码风格/代码风格 手册/指南
1 1 1 https://github.com/google/styleguide Google 编码风格/代码风格 手册/指南 Style guides for Google-originated ...
- Python学习笔记4-如何快速的学会一个Python的模块、方法、关键字
想要快速的学会一个Python的模块和方法,两个函数必须要知道,那就是dir()和help() dir():能够快速的以集合的型式列出该模块下的所有内容(类.常量.方法)例: #--encoding: ...
- 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
- 一张图总结Google C++编程规范(Google C++ Style Guide)
Google C++ Style Guide是一份不错的C++编码指南,我制作了一张比較全面的说明图,能够在短时间内高速掌握规范的重点内容.只是规范毕竟是人定的,记得活学活用.看图前别忘了阅读以下三条 ...
- electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google JavaScript Style Guide代码规范
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- PEP8中文版 -- Python编码风格指南
Python部落组织翻译, 禁止转载 目录 缩进 制表符还是空格? 行的最大长度 空行 源文件编码 导入 无法忍受的 其 ...
- Python: 设计模式 之 工厂模式例(2)(神奇的Python)
#!/usr/bin/env python #coding=utf-8 # # 工厂模式第二例(神奇的Python) # 版权所有 2014 yao_yu (http://blog.csdn.net/ ...
随机推荐
- DIY:从零开始写一个 SQL 构建器
最近在项目中遇到了一个棘手的问题,因为 EF Core 不支持直接生成 Update 语句,所以这个项目就用到了 EFCore.Plus 来实现这个功能,但是 EFCore.Plus 对 SQLite ...
- China.NETConf2019 - 用ASP.NETCore构建可检测的高可用服务
一.前言 2019 中国 .NET 开发者峰会(.NET Conf China 2019)于2019年11月10日完美谢幕,校宝在线作为星牌赞助给予了峰会大力支持,我和项斌等一行十位同事以讲师.志愿者 ...
- 【C/C++开发】C++之enum枚举量声明、定义、使用与枚举类详解与枚举类前置类型声明
众所周知,C/C++语言可以使用#define和const创建符号常量,而使用enum工具不仅能够创建符号常量,还能定义新的数据类型,但是必须按照一定的规则进行,下面我们一起看下enum的使用方法. ...
- 1 RAID技术入门
序 RAID一页通整理所有RAID技术.原理并配合相应RAID图解,给所有存储新人提供一个迅速学习.理解RAID技术的网上资源库,本文将持续更新,欢迎大家补充及投稿.中国存储网一如既往为广大存储界 ...
- python抓取不得姐动图(报错 urllib.error.HTTPError: HTTP Error 403: Forbidden)
抓取不得姐动图(报错) # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/7/23 17:01 imp ...
- IDEA更改JavaScript版本
最好改两个地方 File -> File -> -- --
- Django-09-cookie和session
1. 简介 <1> cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie的工作原 ...
- 【C语言】崩溃的双重指针
指针的指针? 前言: 指针的初识点击移步 双重指针: 指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链.通常,一个指针包含一个变量的地址.当我们定义一个指向指针的指针时,第一个指针包含了第二 ...
- .net 生成非托管代码
最近在一个老外的程序中看到一段代码,其中使用了System.Runtime.InteropServices.MarshalAs方法进行托管代码与非托管代码之间封装数据,感觉很新颖.特意记录下来,供大家 ...
- python 2种创建多线程的方法
多个线程是可以操作同一个全局变量的,因此,可以通过这种方式来判断所有线程的执行进度 # 第一种方法:将要执行的方法作为参数传给Thread的构造方法 import threading import t ...