git+pylint实现python提交代码格式校验

环境:win10(64),python3.7.1,git2.7.2,pylint-2.3.1,git_pylint_commit_hook-2.5.1

以上为当期搭建所用到的版本,有异常时方便查找问题。

安装pylint,pylint是一个单独可以对python文件进行格式校验的模块,Pylint 2.14.0-b1 documentation 官网地址有各个电脑环境的安装说明,Windows下,使用:

pip install pylint

安装完成之后,就可以直接使用pylint对python文件进行格式的检查了,要检查的文件如下:

 
  1. print("啦啦啦")

  2. def func():

  3. print("do something special")

  4. def func1():

  5. print("sdfsd")

执行结果如下: 

 
  1. $ pylint test1.py

  2. ************* Module test1

  3. test1.py:1:0: C0111: Missing module docstring (missing-docstring)

  4. test1.py:2:0: C0111: Missing function docstring (missing-docstring)

  5. test1.py:5:0: C0111: Missing function docstring (missing-docstring)

  6. ------------------------------------------------------------------

  7. Your code has been rated at 4.00/10 (previous run: 4.00/10, +0.00)

修改后满分代码:

 
  1. """模块说明"""

  2. print("啦啦啦")

  3. def func():

  4. """func函数说明"""

  5. print("do something special")

  6. def func1():

  7. """func1函数说明"""

  8. print("sdfsd")

看最后的输出rated at 4.00/10。就是所有代码满分是10分,当前代码得分为4分,以上会说明缺少那些操作,把相应的操作补上,分数就会涨上去,这章就不具体解释缺少操作的含义。

previous run:4.00/10,+0.00。上次得分和相对上次得分的涨幅或扣分,没有就和当前得分一样。

以上的最低分可以通过配置进行设置,下面会讲到如何设置。

但是,这样操作的话,需要开发人员自觉去遵守执行,确保代码全都符合条件了再提交上去,但是人无完人,项目任务繁重的时候难免会忘记,而且这种做法本身也比较low。本着科技为第一生成力,我们希望在git commit的时候,就进行代码检查,通过的代码将会提交成功,进而才能push到服务端。没通过的代码,将打印出得分、修改的相关信息、位置直到开发人员完毕通过检查为止。

接下来的配置将满足以上需求。

有幸找到一遍软文,介绍如何操作 https://kirankoduru.github.io/python/pylint-git-hooks.html ,但是其中有些坑,由于该文章没有透露它的环境相关信息,我照着操作了一遍,并不好使,花了一些时间去排除,所以还是以本篇文章为准。

安装 git-pylint-commit-hook,如果使用的是python版本和我一致,就别指定版本为2.0.7

 
  1. #pip install git-pylint-commit-hook==2.0.7 第一个坑,不使用该版本

  2. pip install git-pylint-commit-hook

配置git钩子,注意配置是在git客户端操作的。

进到git项目的根目录,以根目录为$root$,

 
  1. #进到hooks目录

  2. cd .git/hooks

  3. #配置pre-commit文件

  4. mv pre-commit.sample pre-commit

注意:将pre-commit中除了#!/bin/sh 以外的内容全部删除,如果不删除的话,提交的代码检查不通过,也会被提交!(第二个坑)

这个其实文章里有说明,当时操作的时候没注意,如果以后有需求的话,可以先做个备份。

往pre-commit添加内容,最后其中的所有内容为

 
  1. #!/bin/sh

  2. git-pylint-commit-hook

到现在,上面的完整的需求就满足了,赶紧拿一个python项目进行测试看看。

最后说一些额外的配置

最低分设置:--limit,下面将最低分设置为9分

 
  1. #!/bin/sh

  2. git-pylint-commit-hook --limit=9.0

其他很多设置:可以通过设置配置文件,设置其他的参数,留给大家去探索。

 
  1. #!/bin/sh

  2. git-pylint-commit-hook --limit=9.0 --pylintrc=.pylintrc

.pylintrc和pre-commit同一级目录即可,.pylintrc的内容如下,参考链接:

 
  1. # PyLint configuration file for the project pymvpa.

  2. #

  3. # Agreed formatting (per yoh+michael voice dialog) is camel.

  4. #

  5. # This pylintrc file will use the default settings except for the

  6. # naming conventions, which will allow for camel case naming as found

  7. # in Java code or several libraries such as PyQt, etc.

  8. #

  9. # At some moment it was modified by yoh from the original one

  10. # which can be found on debian systems at

  11. # /usr/share/doc/pylint/examples/pylintrc_camelcase

  12. #

  13. # Just place it in ~/.pylintrc for user-wide installation or simply

  14. # use within a call to pylint or export environment variable

  15. # export PYLINTRC=$PWD/doc/misc/pylintrc

  16. [BASIC]

  17. # Regular expression which should only match correct module names

  18. module-rgx=([a-z][a-z0-9_]*)$

  19. attr-rgx=[a-z_][a-z0-9_]{,30}

  20. # Regular expression which should only match correct class names

  21. class-rgx=[A-Z_]+[a-zA-Z0-9]+$

  22. # Regular expression which should only match correct function names

  23. function-rgx=[a-z_]+[a-z0-9_][a-z0-9]*$

  24. # Regular expression which should only match correct method names

  25. # Allow upper cases in testFeatureSelection where FeatureSelection

  26. # is a class name

  27. method-rgx=(([a-z_]|__)[a-z0-9_]*(__)?|test[a-zA-Z0-9_]*)$

  28. # Regular expression which should only match correct argument names

  29. argument-rgx=[a-z][a-z0-9]*_*[a-z0-9]*_*[a-z0-9]*_?$

  30. # Regular expression which should only match correct variable names

  31. variable-rgx=([a-z_]+[a-z0-9]*_*[a-z0-9]*_*[a-z0-9]*_?||(__[a-zA-Z0-9_]*__))$||[A-Z]+

  32. # Regular expression which should only match correct module level names

  33. # Default: (([A-Z_][A-Z1-9_]*)|(__.*__))$

  34. const-rgx=([a-z_]+[a-z0-9]*_*[a-z0-9]*_*[a-z0-9]*_?|__[a-zA-Z0-9_]*__)$||[A-Z]+

  35. [FORMAT]

  36. indent-string=' '

  37. [DESIGN]

  38. # We are capable to follow that many, yes!

  39. max-branchs = 20

  40. # some base class constructors have quite a few arguments

  41. max-args = 14

  42. # and due to ClassWithCollections and conditional attributes classes by default have lots

  43. # of attributes

  44. max-attributes = 14

  45. # some sci computation can't be handled efficiently without having

  46. #lots of locals

  47. max-locals = 35

  48. [MESSAGES CONTROL]

  49. # Disable the following PyLint messages:

  50. # R0903 - Not enough public methods

  51. # W0105 - String statement has no effect # often used for after-line doc

  52. # W0142 - Used * or ** magic

  53. # W0232 - Class has no __init__ method

  54. # W0212 - Access to a protected member ... of a client class

  55. # W0613 - Unused argument

  56. # E1101 - Has no member (countless false-positives)

  57. # R0904 - Too many public methods

  58. disable-msg=R0903,W0142,W0105,W0212,W0613,E1101,R0904

  59. [REPORTS]

  60. # set the output format. Available formats are text, parseable, colorized and

  61. # html

  62. output-format=parseable

  63. # Include message's id in output

  64. include-ids=yes

  65. # Tells wether to display a full report or only the messages

  66. # reports=no

  67. [MISCELLANEOUS]

  68. # List of note tags to take in consideration, separated by a comma.

  69. # FIXME -- something which needs fixing

  70. # TODO -- future plan

  71. # XXX -- some concern

  72. # YYY -- comment/answer to above mentioned concern

  73. notes=FIXME,TODO,XXX,YYY

  74. [MASTER]

  75. ignore=tests

  76. disable-msg=R0904,R0903,E1101,R21

任何程序错误,以及技术疑问或需要解答的,请添加

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546955.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenCV添加中文(五)

OpenCV添加文字的方法putText(…),添加英文是没有问题的,但如果你要添加中文就会出现“???”的乱码,需要特殊处理一下。 下文提供封装好的(代码)方法,供OpenCV添加中文使…

UpdatePanel的内容中出现自定义多语言运行异常

2019独角兽企业重金招聘Python工程师标准>>> 我们项目有双语要求&#xff0c;采用的是自定义符号控制。在页面加载时进行翻译。 在xml文件中有如下格式的配置 <items> <resource page"~/SalesAppeal/SalesAppealCO.aspx"> <item key"…

ELK开机自启动脚本

elasticsearch服务配置文件 cd /etc/init.dtouch elasticsearchchmod x elasticsearchvi elasticsearch并输入以下内容: #!bin/bash# chkconfig: 2345 21 89 # description: elasticsearch# JAVA_HOME/usr/lib/jvm/jdk1.8.0_91 ES_HOME/usr/local/elasticsearch-6.7.2 case…

QT5获取QPlainTextEdit 某行内容

#include <QTextBlock> #include <QMessageBox>QString str;//获取总行数 str QString::number(ui->plainTextEdit->document()->lineCount()); QMessageBox::information(this, "information", str);//输出某行内容 str ui->plainTextEdi…

pip/pip3更换国内源

pip/pip3更换国内源 用途&#xff1a;pip更换为国内源&#xff0c;可以大大的提高安装成功率和速度。 Windows更换pip/pip3源 打开目录&#xff1a;%appdata%新增pip文件夹&#xff0c;新建pip.ini文件给pip.ini添加内容 [global] timeout 6000 index-url https://pypi.t…

使用Bazel编译报错ERROR: Unrecognized option: --experimental_repo_remote_exec解决方法

ERROR: Unrecognized option: --experimental_repo_remote_exec 一、问题&#xff1a; INFO: Options provided by the client:Inherited common options: --isatty1 --terminal_columns80 INFO: Reading rc options for version from /home/emadboctor/tensorflow/.bazelrc:I…

QT5获取CPU编号和硬盘序列号

windows下执行命令除了用cmd之外&#xff0c;还有个东西叫WMIC&#xff0c;非常强大&#xff0c;可以通过他获取很多信息&#xff0c;包括硬件信息。 #include <QProcess>QString frmMain::getWMIC(const QString &cmd) {//获取cpu名称&#xff1a;wmic cpu get Nam…

视频人脸检测——Dlib版(六)

往期目录 视频人脸检测——Dlib版&#xff08;六&#xff09; OpenCV添加中文&#xff08;五&#xff09; 图片人脸检测——Dlib版&#xff08;四&#xff09; 视频人脸检测——OpenCV版&#xff08;三&#xff09; 图片人脸检测——OpenCV版&#xff08;二&#xff09; …

解决 wamp网站访问慢的问题

最近使用wamp进行调试。 一个简单页面发现wamp的响应达10s。 经过研究发现&#xff0c;是wamp的sql部分拖慢了访问速度&#xff0c;追深后发现是&#xff0c;mysql进行了dns解析而导致反问速度超慢的情况&#xff1b; 所以给mysql 减减压吧&#xff1a; my.ini(Linux下为 my.co…

Ubuntu apt-get和pip源更换

Ubuntu apt-get和pip源更换 更新数据源为国内&#xff0c;是为了加速安装包的增加速度。 更换apt-get数据源 输入&#xff1a;sudo -s切换为root超级管理员&#xff1b;执行命令&#xff1a;vim /etc/apt/sources.list&#xff1b;使用命令&#xff1a;%d 清空所有内容&…

OpenPose+win10安装

openpose在win10上的环境配置和安装&#xff0c;主要参考&#xff1a;OpenPose_1.3.0 vs2017 cuda_9.2.148_win10 cudnn9.2-windows10-x64-v7.2.1.38 windows 配置教程_无情时尚的博客-CSDN博客_openpose vs 另一篇博客后半部分有讲如何使用代码实现相应的功能&#xff1a;…

QT5获取运行程序的工作目录与程序所在的目录

在qt-creator中debug的时候&#xff0c;发现程序不能读写所在目录的配置文件&#xff0c;资源文件。 Google了一下&#xff0c;原来 运行程序的 工作目录 与 程序所在的目录是不同的概念。 跑跑这段代码就知道了&#xff1a; #include <QDebug> #include <QDir>Q…

超简单的视频对象提取程序

视频对象提取 与其说是视频对象提取&#xff0c;不如说是视频颜色提取&#xff0c;因为其本质还是使用了OpenCV的HSV颜色物体检测。 HSV介绍 HSV分别代表&#xff0c;色调&#xff08;H&#xff1a;hue&#xff09;&#xff0c;饱和度&#xff08;S&#xff1a;saturation&a…

python2.7 与 go1.2简单性能比较

过完年刚上班&#xff0c;项目还没有开始&#xff0c;对于即将到来的项目&#xff0c;想尝试是否可以找到一个开发效率接近python&#xff0c;运行效率接近静态语言的编程语言&#xff0c;选择基本就是scala和go&#xff0c;公司的技术组成基本都是c派的&#xff0c;scala暂不考…

Win10安装 WSL Ubuntu Linux系统,非双系统,完美兼容超详细版本

Windows SubSystem for Linux(WSL) 适用于Linux的Windows子系统 WSL团队的blog:Windows Subsystem for Linux | Microsoft Docs WSL的官方文档&#xff1a;What is Windows Subsystem for Linux | Microsoft Docs 最近需要Linux的shell环境进行学习&#xff0c;之前一直是在虚…

QT5 exec()模态显示子对话框,父子对话框都最小化后显示桌面

QT5 exec()模态显示子对话框&#xff0c;在任务栏上单击鼠标右键&#xff0c;出现‘’显示桌面”选项&#xff0c;选中后&#xff0c;QT5父对话框最小化&#xff0c;但是子对话框一直显示在最上层&#xff0c;却不随父对话框一起最小化。后来发现是添加了Qt::WindowStaysOnTopH…

基于python的图片修复程序-可用于水印去除

图片修复程序-可用于水印去除 在现实的生活中&#xff0c;我们可能会遇到一些美好的或是珍贵的图片被噪声干扰&#xff0c;比如旧照片的折痕&#xff0c;比如镜头上的灰尘或污渍&#xff0c;更或者是某些我们想为我所用但有讨厌水印&#xff0c;那么有没有一种办法可以消除这些…

Promethus搭建 K8S 集群节点资源监控系统

对于集群的监控一般我们需要考虑以下几个方面&#xff1a; Kubernetes 节点的监控&#xff1a;比如节点的 cpu、load、disk、memory 等指标 内部系统组件的状态&#xff1a;比如 kube-scheduler、kube-controller-manager、kubedns/coredns 等组件的详细运行状态 编排级的 me…

MySQL运维常用系统命令

1.取出连接3306端口连接数前10的ip及连接数netstat -antlp|grep :3306 |awk {print $4}|sort |uniq -c|sort -k 1 -r|head -10#先sort后uniq -c 统计每个ip出现数量&#xff0c;再按第一列逆序排序(sort -k 1第一列)2.cut 与 awk 区别cut默认是以制表符分割&#xff0c;awk默认…

ubuntu搭建nodejs生产环境——快速部署手册

为什么不用CentOS而用Ubuntu作为生产环境的运行平台&#xff1f;这个我也比较好奇&#xff0c;公司订的只能沿用传统&#xff0c;从使用成本的角度来说&#xff0c;此举也是值得肯定的。 测试环境 腾讯云 Ubuntu 16.04 阿里云 Ubuntu 16.04 开启Root账号ssh登录 1.修改配置…