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.pyfrom order import place_order
import os, sysclass website_api(object):def __init__(self):self.user_name = ''self.Gender = 'male'#comment in wrong identself.active =Falsedef login(self, Person):self.user_name=Person.user_namenot_used_var = 0return Truedef Logout(self):self.active =Falsedef 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
PEP8 https://www.python.org/dev/peps/pep-0008/
Google Python Coding Style http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
pep8工具 https://github.com/PyCQA/pep8
flake8 https://flake8.readthedocs.org
Warning / Error codes of flake8 https://flake8.readthedocs.org/en/latest/warnings.html
Written with StackEdit.
转载于:https://www.cnblogs.com/bonelee/p/11243384.html
python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准...相关推荐
- python coding style why_python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准...
python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...
- python coding style guide 的快速落地实践
python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...
- python coding style_python coding style guide 的高速落地实践
python coding style guide 的高速落地实践 机器和人各有所长,如coding style检查这样的可自己主动化的工作理应交给机器去完毕,故发此文帮助你在几分钟内实现coding ...
- iOS Coding Style Guide 代码规范
前言 代码规范可以说是老生常谈的话题了, 也是程序员自我修养的一种体现, 虽然一套好的代码规范不能使程序运行的更加流畅, 不能使程序直接的影响到程序的功能执行,但是如果能再发开之前就能明确定义一套代码 ...
- python 算法教程 pdf 英文_上手实践《Python机器学习第2版》PDF中文+PDF英文+代码+Sebastian...
学习机器学习,推荐学习<Python机器学习(第二版)>. <Python机器学习(第2版)>,图文并茂,代码详实,原理清晰,覆盖面适度,侧重算法实现和应用,作为入门级学习还是 ...
- python coding style why_Python 编码规范(Style Guide)2
8,命名风格,主要有: lowercase,lower_case_with_underscores:通常用于变量,函数名或模块名 UPPERCASE,UPPER_CASE_WITH_UNDERSCOR ...
- [文档].Actel – Actel HDL Coding Style Guide
http://www.actel.com/documents/hdlcode_ug.pdf 转载于:https://www.cnblogs.com/yuphone/archive/2011/04/16 ...
- 随机森林python反欺诈_WePay机器学习反欺诈实践:Python+scikit-learn+随机森林
摘要:在这篇博文中,WePay介绍了支付行业构建机器学习模型应对很难发现的shell selling欺诈的实践心得.WePay采用了流行的Python.scikit-learn开源学习机器学习工具以及 ...
- python开发erp系统odoo_ODOO快速入门与实战:PYTHON开发ERP指南
前言篇 基础篇章 Odoo简介 21.1 Odoo发展历程 21.2 Odoo与主流ERP系统的对比 31.3 总结 4第2章 安装与配置 52.1 在Ubuntu上安装Odo ...
最新文章
- Spring Controller Junit例子
- Android设备音频部分一些概念
- 目前成熟的计算机安全技术,计算机安全危害特点研究(共2389字).doc
- 操作系统实验报告4:Linux 下 x86 汇编语言3
- android陀螺仪测试工具,修改安卓陀螺仪和加速度计的sensor抽象层HAL
- 前端—每天5道面试题(十三)
- JasperReports 5.0.1 发布,Java 报表工具
- catia中尺子没了怎么调出来,【答疑】草图大师sketchup的尺子快捷键是什么呀? - 羽兔网问答...
- C# ToolStrip 图标大小设置
- 河北源达投顾:用专业教学,用智能解决选股烦恼
- 查看Java 版本tls_如何在Java中设置TLS1.2版本
- CSS - 选择器优先级介绍
- Pytorch实现性别识别,男女分类
- java集合方法之TreeSet.floor()和TreeSet.ceiling()
- Centos6 密钥登陆,解决所选的用户密钥未在远程主机上注册
- DOM是什么意思-前端入门
- 飞行棋程序(附源码)
- 去除带下划线的word文档答案
- 微信小程序入门7-微信公众号设置IP白名单
- 蚂蚁金服若IPO 信贷业务或将得到长远发展
热门文章
- Linux驱动中,probe函数何时被调用
- 彻底卸载vscode Linux,ubuntu如何卸载vscode
- python 打包 .app 运行 控制台窗口_Python打包工具PyInstaller的安装与pycharm配置支持PyInstaller详细方法...
- python zipfile_Python 学习入门(16)—— zipfile
- ct读片软件_伦琴影领影像诊断中心:这六大MRI读片技巧,影像医生必须掌握
- 2021广西高考成绩几点可以查询,高考完多久分数能出来广西 2021年广西高考分数查询公布时间...
- 怎么判断网络回路_收藏|电梯安全回路分析说明及故障判断
- vue 全局排序_搞定VUE [ 一 ]
- ateq测漏仪f620说明书_上海壁挂式测漏仪安全使用说明,煤气房报警器安装安全使用说明...
- saxon java_如何将Saxon设置为Java中的Xslt处理器?