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

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

安装pylint,pylint是一个单独可以对python文件进行格式校验的模块,https://www.pylint.org/ 官网地址有各个电脑环境的安装说明,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

git+pylint实现python提交代码格式校验相关推荐

  1. (26)ESLint一JS代码格式校验

    一.什么是代码格式 代码格式即为代码风格,每个程序员再开发的时候,书写代码的风格都是不一样的,比如说,有的人喜欢书写字符串时用双引号,有的喜欢用单引号,有的再书写标签代码缩进时,喜欢用2个空格,有的喜 ...

  2. vue关闭代码格式校验

    eslint eslint是一个JavaScript的校验插件 通常用来校验语法或代码的书写风格 有了eslint的检查 代码中的缩进 空格 空白行之类的都会被按照规范检查 但有时我们不希望开启代码校 ...

  3. 如何将git服务器同步到本地文件夹,使用git在服务器上部署git仓库并实现提交代码时同步代码到生产环境...

    最近由于需要对正在运行的系统进行新功能添加,本来是可以通过github进行代码维护,但是由于这个项目涉及一些问题,目前还不能开源,所以只能是手动覆盖bug文件,生产环境上的代码反而是最新的了. 之前有 ...

  4. 【Git】IntelliJ IDEA 提交代码到 GitCode 远程仓库 ( GitCode 创建远程仓库 | 将本地工程推送到 GitCode 远程仓库 | 验证权限 | 生成个人访问令牌 )

    文章目录 前言 一.GitCode 创建远程仓库 二.将本地工程推送到 GitCode 远程仓库 三.验证权限 前言 GitHub 又挂了 , 国内不太好用 , 现在开始使用 gitcode , 地址 ...

  5. eclipse中git的配置、提交代码、从远程导入代码

    一.设置Git提交时的用户名和邮箱地址 效果如下: 步骤如下: 设置用户名,key:user.name 设置邮箱.key:user.email 二.提交代码 创建本地仓库->加入暂存区-> ...

  6. Python 组织机构代码证校验

    全国组织机构代码由八位数字(或大写拉丁字母)本体代码和一位数字(或大写拉丁字母)校验码组成. 校验码按照以下公式计算: C9=11−MOD(∑i=18Ci×Wi,11) C_9=11-MOD(\sum ...

  7. 4.7 Python设置代码格式

    随着你编写的程序越来越长,有必要了解一些代码格式设置约定.请花时让你的代码尽可能易于阅读:让代码易于阅读有助于你掌握程序是做什么的,也可以帮助他人理解你编写的代码. 为确保所有人编写的代码的结构都大致 ...

  8. 用小乌龟拉取代码_如何使用git拉取代码及提交代码(详细)

    分享给刚进入公司的小伙伴们鸭! 第一步:首先在本地安装git和TorToiseGit小乌龟,svn同理,也可以安装下TorToiseGit中文语言包,前期可减少出错,后期熟悉了可直接用命令行pull代 ...

  9. git新建分支及提交代码到分支

    项目场景: git仓库中只有一个分支,且已经有上传过代码,新的代码不允许直接把你的代码覆盖上去.所以需要你掌握新建分支的方法 问题描述: 如果你在没有新建分支的情况下上传代码,将会报错,如下: To ...

最新文章

  1. 元宇宙大热后将陷低潮, 虚实互联网更准确, 2030前后才可能全面热启
  2. python中if elif语句优化_python – 最有效的方式做一个if-elif-elif-else语句当else做的最多?...
  3. 4 系统的 CPU 使用率很高,但为啥却找不到高 CPU的应用?
  4. 用CSS3来添加项目编号
  5. 蜗杆参数法设计_齿轮几何尺寸设计,很实用的Excel表格(附自动计算表格)
  6. 前端学习(977):本地存储导读
  7. 流量专家为114搜索提供权威流量访问统计
  8. 复习Java异常处理_异常分类_自定义异常_线程初步了解
  9. python nonetype报错_python 查询数据库数据 NoneType报错
  10. Linux系统磁盘阵列创建教程----------(better late than never. 只要开始,虽晚不迟。)...
  11. 实现网页布局的自适应 利用@media screen
  12. keras ImageDataGenerator数据增强
  13. Qt安卓开发环境搭建
  14. GD32f103介绍第二章
  15. 【统计学习方法】EM算法原理
  16. Unity url编码转换
  17. 一个命令,删除电脑上顽固的文件和文件夹|干货
  18. 人体颈椎神经分布图高清,颈椎部神经分布图高清
  19. 基于SSM框架的网上购物送货系统
  20. 跳一跳辅助源码学习(主流分辨率全适配)

热门文章

  1. Git 2.19 对Diff、Branch和Grep等做了改进
  2. OpenCV探索之路(二十五):制作简易的图像标注小工具
  3. Oracle Linux 6.5 RPM安装Mysql 5.7.11
  4. http中指定使用worker/prefork
  5. 西南大学校园GIS平台
  6. android 类ios actionsheet效果
  7. Hitv8 文件批量重命名工具
  8. 详解:设计模式之-单例设计模式
  9. oracle 经纬度算距离,根据经纬度诀别用java和Oracle存储过程计算两点距离
  10. cesium广告牌_公路广告牌