引言

遇到的问题

在项目开发过程中,缩进的配置、代码的最大长度、空格换行的使用不统一导致代码风格不一致,尤其是在一个团队中多人协作时,每个人有自己的编码风格,最终导致代码可读性差,难以维护。

是否有一种方案可以在提交代码时能检查代码是否符合代码风格标准且给出重构建议,同时控制代码提交,那么就可以在一定程度上避免这些问题了。

解决思路

考虑借用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以外,还有其他选项可以设置,具体如下

参考文档

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

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

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

猜你喜欢

c语言git代码注释风格,git代码格式化上传相关推荐

  1. 如来佛代码注释,保佑代码无BUG

    如来佛代码注释,保佑代码无BUG         /*                            _ooOoo_                           o8888888o   ...

  2. Eclipse配置代码注释模板 Eclipse代码注释模板 Eclipse设置方法注释模板

    Eclipse配置代码注释模板 Eclipse代码注释模板 Eclipse设置方法注释模板 一.前言 1.在日常的团队开发中,都会有相应的的代码开发规范模板:在不同的IDE中配置方式又是不一样的,本文 ...

  3. 如何通过Git GUI将自己本地的项目上传至Github

    ithud是一个程序员以后成长都会使用到的,先不说很多优秀的开源框架都在这上面发布,光是用来管理自己的demo都已经让人感到很方便,用得也很顺畅.而真正让我下定决心使用github的原因是因为两次误操 ...

  4. Git安装及密钥的生成并上传本地文件到GitHub上

    之前用的GitHub,不太熟练,一直在上传的过程中遇到了一些问题,看了网上诸多教程,总觉得很乱,特参考一些资料,总结了一篇完整的操作步骤,从下载安装到上传文件,亲测有效 1.下载Git软件:https ...

  5. git仓库本地文件与coding远程上传

    ①首先得注册一个coding账号 官方网址为:https://coding.net/ ②点击克隆 复制仓库的链接 使用git clone <复制的链接> 输入git status 发现 红 ...

  6. Hadoop环境下用java代码实现hdfs远程文件的上传和下载

    Hadoop环境下用java代码实现hdfs远程文件的上传和下载 文章目录 Hadoop环境下用java代码实现hdfs远程文件的上传和下载 一.新建maven工程 二.文件的上传 三.文件的下载 四 ...

  7. Go语言中的注释类型和代码风格

    Go语言中的注释类型 Go的代码风格

  8. IntelliJ IDEA绑定GitHub实现代码版本控制实例演示,IDEA上传、更新、同步项目到GitHub演示,Git的下载与安装

    IDEA 绑定 GitHub 实现代码版本控制 第一章:IDEA 配置 Git 并绑定 GitHub ① 下载 Git ① 安装 Git ③ 设置 Git 的用户名和用户邮箱 ④ IEDA 配置 Gi ...

  9. PHP代码注释方法,PHP代码注释

    写在前面 注释可以理解成代码中的解释和说明.使用注释不仅能提高程序的可读性,而且有利于程序的后期维护工作,注释内容不会被程序所执行. 个人项目 个人站点:LN电影网个人博客:L&N博客 PHP ...

最新文章

  1. python 断点调试 pdb
  2. 使用Amalgamate将C/C++项目合并成一个.h/.c[pp]文件
  3. android studio发布项目到github
  4. 深入掌握JMS(二):一个JMS例子
  5. java array 元素的位置_JAVA集合类,有这一篇就够了
  6. POJ 2135 Farm Tour (最小费用最大流)
  7. linux dns函数,Linux DNS (1)的基本概念
  8. Impala与Hive的关系
  9. Netsuite Foreign Currency Revaluation 外币评估
  10. Http Core学习(Http Components 翻译和学习)
  11. iOS开发Assertion failure in -[AFJSONRequestSerializer requestWithMethod:URLString:parameters:error:]
  12. 西门子s7-200解密软件下载_高邮哪里有西门子三菱PLC编程学习班?多久能学会?...
  13. 计算机硬盘隐,终极电脑磁盘隐藏方法大全
  14. iOS 2020 开发者账号 身份验证步骤
  15. 计算机表格常用根式,常用平方根表.doc
  16. 如何在IDEA中导入eclipse项目
  17. 插入排序(InsertingSort)
  18. 关于 三相可控硅触发板 晶闸管整流调压控制板
  19. 买车容易行路难-买车
  20. PID控制------伯德图原理

热门文章

  1. Windows本地连接正常,上不去网的解决办法
  2. 胧月初音未来计算机,胧月---初音未来(调教用)
  3. 【图像超分辨率】Accurate Image Super-Resolution Using Very Deep Convolutional Networks
  4. RTL8189ES/ETV/FTV系列模块定频软件操作手册
  5. SEO搜索引擎优化(总结学习)
  6. 今晚直播 |不诉离殇,图像分割打卡营正式毕业啦!
  7. Oracle中Clob类型处理解析
  8. ORACLE中CLOB介绍及使用
  9. 工程师如何拥抱数字化转型?
  10. 算法-大作业-圆排列问题