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后结果PASSEDzhongyaqi@010A1612190049 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以外,还有其他选项可以设置,具体如下

9fec41d135da702df2f641aeffb1ad34.png

参考文档

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

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

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

猜你喜欢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值