天天看點

c語言git代碼注釋風格,git代碼格式化上傳

引言

遇到的問題

在項目開發過程中,縮進的配置、代碼的最大長度、空格換行的使用不統一導緻代碼風格不一緻,尤其是在一個團隊中多人協作時,每個人有自己的編碼風格,最終導緻代碼可讀性差,難以維護。

是否有一種方案可以在送出代碼時能檢查代碼是否符合代碼風格标準且給出重建構議,同時控制代碼送出,那麼就可以在一定程度上避免這些問題了。

解決思路

考慮借用pylint和git-pylint-commit-hook檢查代碼并控制代碼能否送出。

pylint可以查找程式設計錯誤,幫助實施編碼标準,并提供簡單的重建構議。

git-pylint-commit-hook可以設定滿足送出代碼的limit分數值,送出代碼時若未達到limit,則送出失敗,按照建議完善代碼後重新送出達到limit後方可送出成功。

解決方案

第一步 安裝pylint和git-pylint-commit-hookpip install pylintpip install git-pylint-commit-hook複制代碼

第二步 安裝成功後進入項目.git/hooks路徑下進行配置

1、建立檔案pre-commit,并寫入如下内容cd .git/hooksvim pre-commit#!/bin/shgit-pylint-commit-hook --limit=9.0 --pylintrc=.pylintrc複制代碼

2、建立檔案.pylintrc,并寫入如下内容vim .pylintrcln -sf $(pwd)/.hooks/pre-commit .git/hooks/#關聯到github的commit事件,也就是執行commit指令時,自動運作pre-commit腳本。複制代碼

應用示例

以test_repetition.py為例,将不規範的代碼按照提示完善成可以送出的代碼#! /usr/bin/env python#coding=utf-8__author__ = 'zhongyaqi'from collections import Counter #引入Counterdef deal_repetition(a): # a= ['4175726040','417572604','417675161','417675666','417572604','417572604','417675161','417675666','417572604','417675161','417675666','417572604','417572604'] b = dict(Counter(a)) print(b.items()) print ([key for key,value in b.items()if value > 1]) #隻展示重複元素 print ({key:value for key,value in b.items()if value > 1}) #展現重複元素和重複次數 return ({key:value for key,value in b.items()if value > 1}) #展現重複元素和重複次數if __name__ == '__main__': deal_repetition()複制代碼

git commit 結果FAILED$ git commit -m "test git pylint"Running pylint on src/testpylint/common_method.py (file 1/2).. -11/10.00 FAILED************* Module common_methodsrc\testpylint\common_method.py:7:0: C0301: Line too long (789/100) (line-too-long)src\testpylint\common_method.py:8:0: W0311: Bad indentation. Found 8 spaces, expected 4 (bad-indentation)src\testpylint\common_method.py:9:0: W0311: Bad indentation. Found 8 spaces, expected 4 (bad-indentation)src\testpylint\common_method.py:10:0: W0311: Bad indentation. Found 8 spaces, expected 4 (bad-indentation)src\testpylint\common_method.py:10:14: C0326: No space allowed before bracket print ([key for key,value in b.items()if value > 1]) #ֻչʾ▒ظ▒Ԫ▒▒ ^ (bad-whitespace)src\testpylint\common_method.py:10:27: C0326: Exactly one space required after comma print ([key for key,value in b.items()if value > 1]) #ֻչʾ▒ظ▒Ԫ▒▒ ^ (bad-whitespace)src\testpylint\common_method.py:11:0: W0311: Bad indentation. Found 8 spaces, expected 4 (bad-indentation)src\testpylint\common_method.py:11:14: C0326: No space allowed before bracket print ({key:value for key,value in b.items()if value > 1}) #չ▒▒▒ظ▒Ԫ▒غ▒▒ظ▒▒▒▒▒ ^ (bad-whitespace)src\testpylint\common_method.py:11:33: C0326: Exactly one space required after comma print ({key:value for key,value in b.items()if value > 1}) #չ▒▒▒ظ▒Ԫ▒غ▒▒ظ▒▒▒▒▒ ^ (bad-whitespace)src\testpylint\common_method.py:12:0: W0311: Bad indentation. Found 8 spaces, expected 4 (bad-indentation)src\testpylint\common_method.py:12:34: C0326: Exactly one space required after comma return ({key:value for key,value in b.items()if value > 1}) #չ▒▒▒ظ▒Ԫ▒غ▒▒ظ▒▒▒▒▒ ^ (bad-whitespace)src\testpylint\common_method.py:15:0: C0304: Final newline missing (missing-final-newline)src\testpylint\common_method.py:1:0: C0114: Missing module docstring (missing-module-docstring)src\testpylint\common_method.py:6:0: C0103: Argument name "a" doesn't conform to snake_case naming style (invalid-name)src\testpylint\common_method.py:6:0: C0116: Missing function or method docstring (missing-function-docstring)src\testpylint\common_method.py:8:8: C0103: Variable name "b" doesn't conform to snake_case naming style (invalid-name)src\testpylint\common_method.py:15:4: E1120: No value for argument 'a' in function call (no-value-for-parameter)複制代碼

按照提示修改去掉多餘空格,将超長的換行處理,增加注釋等等,完善代碼如下#! /usr/bin/env python# -*- coding: UTF-8 -*-__author__ = 'zhongyaqi'from collections import Counter #引入Counterdef deal_repetition(idlist): ''' This method checks for duplicate id ''' # IDLIST = ['4175726040','417572604','417675161','417675666','417572604','417572604', # '417675161','417675666','417572604','417675161','417675666','417572604','417572604'] id_dict = dict(Counter(idlist)) print(id_dict.items()) print([key for key, value in id_dict.items()if value > 1]) #隻展示重複元素 print({key:value for key, value in id_dict.items()if value > 1}) #展現重複元素和重複次數 return ({key:value for key, value in id_dict.items()if value > 1}) #展現重複元素和重複次數if __name__ == '__main__': IDLIST = ['4175726040', '417572604', '417675161', '4175726040', '417572604', '417675161', '4175726040', '417572604', '417675161'] deal_repetition(IDLIST)複制代碼

執行git commit後結果[email protected] MINGW64 /e/PythonProject/gitTest/gittest (dev-login)$ git commit -m "test git pylint"Running pylint on src/testpylint/common_method.py (file 1/1).. 9.1/10.00 PASSED[dev-login 4b56be4] test git pylint 2 files changed, 135 insertions(+), 35 deletions(-) rewrite src/testpylint/common_method.py (94%)複制代碼

簡單了解pylint和git-pylint-commit-hook的使用方法後,還是有些疑惑,什麼是pylint,什麼是PEP8,什麼又是git-pylint-commit-hook,接下來參考官網介紹下~

Pylint簡介

Pylint 是一個 Python 代碼分析工具,它分析 Python 代碼中的錯誤,查找不符合代碼風格标準(Pylint 預設使用的代碼風格是 PEP 8,詳細資訊請參閱文章底部參考連結)和有潛在問題的代碼。

官網上是這麼介紹的Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.Pylint是Python靜态代碼分析工具,它可以查找程式設計錯誤,幫助實施編碼标準,嗅探代碼并提供簡單的重建構議It’s highly configurable, having special pragmas to control its errors and warnings from within your code, as well as from an extensive configuration file. It is also possible to write your own plugins for adding your own checks or for extending pylint in one way or another.它是高度可配置的,具有特殊的編譯訓示,可以從您的代碼以及廣泛的配置檔案中控制其錯誤和警告。也可以編寫自己的插件以添加自己的檢查或以一種或另一種方式擴充pylint。

PEP8簡介

這篇文檔說明了Python主要發行版中标準庫代碼所遵守的規範。對于Python中的C語言實作中的編碼規範,請參考實作Python的C代碼風格指南PEP 7。

該樣式指南會随着時間的流逝而發展,因為會确定其他約定,而過去的約定會因語言本身的更改而過時。

如下約定了代碼布局,包含縮進、使用tab或空格、最大長度、及換行符等,空白在表達式和語句中的運用,等具體可參考文章底部附的官網位址Code Lay-outIndentation

Tabs or Spaces?

Maximum Line Length

Should a Line Break Before or After a Binary Operator?

Blank Lines

Source File Encoding

Imports

Module Level Dunder Names

String Quotes

Whitespace in Expressions and StatementsPet Peeves

Other Recommendations

When to Use Trailing Commas

git-pylint-commit-hook簡介

git-pylint-commit-hook是個鈎子,它會在送出代碼(commit)時自動運作,根據配置檔案pylintrc中的配置,去檢測改動過檔案中的代碼,并會對其進行評分,如果未達到設定的分數線,則這次送出到本地版本庫的操作,強制取消。需要修改代碼且評分超過設定的分數時才可以送出。git-pylint-commit-hook will automatically find your pylint configuration files, according to the pylint configuration file default read order. Any configuration found for the project will be used in the git-pylint-commit-hook.将根據pylint配置檔案的預設讀取順序自動找到您的pylint配置檔案。為項目找到的任何配置都将在git-pylint-commit-hook中使用。

使用--pylintrc指令行選項定義自定義pylint配置檔案

pylint配置預設情況下,設定是從存儲庫根目錄中的.pylintrc檔案加載的。.pylintrc檔案内容可參考如下[pre-commit-hook]command=custom_pylintparams=--rcfile=/path/to/another/pylint.rclimit=8.0複制代碼參數解釋command is for the actual command, for instance if pylint is not installed globally, but is in a virtualenv inside the project itself.params lets you pass custom parameters to pylintlimit is the lowest value which you want to allow for a pylint score. Any lower than this, and the script will fail and won’t commit.複制代碼

git-pylint-commit-hook指令行選項

除了--limit以外,還有其他選項可以設定,具體如下

c語言git代碼注釋風格,git代碼格式化上傳

參考文檔

https://pypi.org/project/pylint/

https://git-pylint-commit-hook.readthedocs.io/en/latest/usage.html

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

猜你喜歡