python coding style guide 的快速落地實踐——業內python 編碼風格就pep8和谷歌能夠認做標準

python coding style guide 的快速落地實踐


機器和人各有所長,如coding style檢查這種可自動化的工做理應交給機器去完成,故發此文幫助你在幾分鐘內實現coding style的自動檢查。html


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

  • PEP8

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

發明Python語言豐碑人物Guido van Rossum的親自寫的Coding Style, 知名度5顆星,可操做性5顆星。git

  • Google Python Coding Style Guide

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

Google內部普遍使用Python做爲開發語言,此Coding Style 在坊間流傳很廣,知名度5顆星,可操做性5顆星。值得一提的是Guido也曾經在Google工做過一段時間。web

2.Flake8 - Coding Style檢查自動化的利器

你可能據說過pep8,這是一個根據PEP8規範檢查python代碼style的自動化工具。flake8是對pep8進行了包裝,充分發揮了插件化的優點,增長了如代碼複雜度,函數、變量命名習慣,import順序等檢查。api

2.1 安裝Flake8

安裝flake8,同時安裝一些有用的插件。ide

  • pep8-nameing

https://github.com/PyCQA/pep8-naming 命名檢查svn

  • 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


Written with StackEdit.

相關文章
相關標籤/搜索