python coding style guide 的高速落地实践

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

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

PEP8

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

Google Python Coding Style Guide

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

flake8-import-order

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

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

flake8-todo

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 列表

随着新的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

python coding style_python coding style guide 的高速落地实践相关推荐

  1. python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准...

    python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...

  2. python coding style guide 的快速落地实践

    python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...

  3. python coding style why_python coding style guide 的快速落地实践——业内python 编码风格就pep8和谷歌可以认作标准...

    python coding style guide 的快速落地实践 机器和人各有所长,如coding style检查这种可自动化的工作理应交给机器去完成,故发此文帮助你在几分钟内实现coding st ...

  4. iOS Coding Style Guide 代码规范

    前言 代码规范可以说是老生常谈的话题了, 也是程序员自我修养的一种体现, 虽然一套好的代码规范不能使程序运行的更加流畅, 不能使程序直接的影响到程序的功能执行,但是如果能再发开之前就能明确定义一套代码 ...

  5. PEP 8 – Style Guide for Python Code

    PEP 8 – Style Guide for Python Code PEP 8–Python编码规范 原文地址:https://www.python.org/dev/peps/pep-0008/ ...

  6. PEP 8 -- Style Guide for Python Code。Python 代码规范。

    PEP 8 – Style Guide for Python Code 文章目录 PEP 8 -- Style Guide for Python Code 代码规范. PEP8 (pycodestyl ...

  7. 在翻译PEP8中学习 -- Style Guide for Python Code

    翻译了好久, 终于把这篇文档翻完了, 学到很多. 自从考研结束后就没有翻译过文章了, 一开始还以为考研英语78分的我翻译能力还可以, 结果打脸. 凡是得练习啊! 官方原文: PEP8 Style Gu ...

  8. PEP8-python代码样式指南(Style Guide for Python Code)

    文章目录 介绍(Introduction) 尽信书不如无书(A Foolish Consistency is the Hobgoblin of Little Minds) 代码布局(Code Lay- ...

  9. Google Python Style Guide(谷歌python规范指南)

    来自:Google Python Style Guide 1. 背景 Python是谷歌内部使用的主要动态语言(脚本语言).这份指导手册列出了使用Python的编程人员应该做的和不应该做的. 为了帮助 ...

最新文章

  1. Java并发编程71道面试题及答案 1
  2. 用 C# 来守护 Python 进程
  3. 关于SPARK_WORKER_MEMORY和SPARK_MEM
  4. 地推HTTP3和QUIC
  5. StringUtils测试
  6. Windows聚焦壁纸保存方法
  7. JAVASE第5天笔记
  8. Python数据分析与可视化案例解析
  9. extern 用法小结
  10. 2021-09-11职场规则
  11. Codeforces 1089D Eels (看题解)
  12. 考研英语阅读理解错8个,我今年会不会凉?
  13. Ambari2.7.4 + HDP3.1.4 离线安装(1)
  14. 传说中的王八蛋~~~
  15. Nodejs+Express项目使用JWT
  16. Oracle实现判断功能三种方式总结
  17. C# 8.0核心技术指南
  18. 一分钟:XM文件格式转换MP3
  19. 【slam十四讲第二版】【课本例题代码向】【第七讲~视觉里程计Ⅱ】【使用LK光流(cv)】【高斯牛顿法实现单层光流和多层光流】【实现单层直接法和多层直接法】
  20. Spring之XML解析

热门文章

  1. MSP430之BR0、BR1计算
  2. 李宏毅自然语言处理——NLP任务概述
  3. 决胜网总裁王雷:AI在教育领域应用之我见
  4. print 中文输出乱码
  5. jQuery前端开发学习指南(18)——利用jQuery实现元素的隐藏、显示和切换及其动画效果
  6. iterm上安装oh-my-zsh连接失败
  7. OpenGov(一):什么是Polkadot Gov2
  8. PNETCDF 和NETCDF安装分享
  9. php 自动下载apk,Android 下载apk 自动 安装
  10. Python中 axis=0、axis=1是行还是列?