如何使用 Pylint 来规范 Python 代码风格

有没有人在啊,想请问一下,如何使用 Pylint 来规范 Python 代码风格
最新回答
江心薄雾起

2025-03-01 16:29:34

Pylint具体介绍Pylint的安装Pylint可以用于所有高于或者等于2.2的Python版本兼容。需要logilab-astng(version>=0.14)和logilab-common(version>=0.13)的包(具体信息,请参阅参考资料),如果是Python版本低于2.3,那么它还需要optik包(本文接下来的示例暂不考虑这种情况)。Pylint所用到的所有的包的下载地址logilab-astng的最新包下载:等等。在Pylint的输出中有如下两个部分:
源代码
分析部分和报告部分。源代码分析部分:对于每一个Python模块,Pylint的结果中首先显示一些"*"字符,后面紧跟模块的名字,然后是一系列的message,message的格式如下:MESSAGE_TYPE:LINE_NUM:[OBJECT:]MESSAGEMESSAGE_TYPE有如下几种:(C)惯例。违反了编码风格标准(R)重构。写得非常糟糕的代码。(W)警告。某些Python特定的问题。(E)错误。很可能是代码中的错误。(F)致命错误。阻止Pylint进一步运行的错误。清单2.Pylint中的utils模块的输出结果*************ModuleutilsC:88:Message:MissingdocstringR:88:Message:Toofewpublicmethods(0/2)C:183:MessagesHandlerMixIn._cat_ids:MissingdocstringR:183:MessagesHandlerMixIn._cat_ids:MethodcouldbeafunctionR:282:MessagesHandlerMixIn.list_messages:Toomanybranches(14/12)报告部分:在源代码分析结束后面,会有一系列的报告,每个报告关注于项目的某些方面,如每种类别的message的数目,模块的依赖关系等等。具体来说,报告中会包含如下的方面:检查的module的个数。对于每个module,错误和警告在其中所占的百分比。比如有两个moduleA和B,如果一共检查出来4个错误,1个错误是在A中,3个错误是在B中,那么A的错误的百分比是25%,B的错误的百分比是75%。错误,警告的总数量。回页首使用Pylint分析Python代码的具体示例下面是一个从xml文件中读取一些值并显示出来的一段Python代码dw.py,代码如下:清单3.源码importstring#!/usr/bin/envpythonimportxml.dom.minidomxmlDom=xml.dom.minidom.parse("identity.xml")organizations=xmlDom.getElementsByTagName('DW')fororginorganizations:products=org.getElementsByTagName('linux')forproductinproducts:print'ID:'+product.getAttribute('id')print'Name:'+product.getAttribute('name')print'WordCount:'+product.getAttribute('count')清单4.identity.xml的内容这时候使用Pylint的结果(这是从html格式的输出中拷贝的)为:清单5.Pylint的分析结果*************ModuledwC:1:MissingdocstringC:5:OperatornotprecededbyaspacexmlDom=xml.dom.minidom.parse("identity.xml")^C:5:Invalidname"xmlDom"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)C:6:Invalidname"organizations"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)Report部分省略输出中第一部分是源代码分析,第二部分是报告。输出结果中有这么多信息,从哪里开始分析呢?首先使用如下的步骤来分析代码:1.因为输出结果太长,所以可以先不让它输出报告部分,先根据源代码分析部分来找出代码中的问题。使用选项"--reports=n"。2.使用选项"--include-ids=y"。可以获取到源代码分析部分每条信息的ID。清单6.使用pylint--reports=n--include-ids=ydw.py的结果*************ModuledwC0111:1:MissingdocstringC0322:5:OperatornotprecededbyaspacexmlDom=xml.dom.minidom.parse("identity.xml")^C0103:5:Invalidname"xmlDom"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)C0103:6:Invalidname"organizations"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)每个信息前面都会加上一个id,如果不理解这个信息的意思,可以通过pylint--help-msg=id来查看。清单7.使用pylint--help-msg=C0111的结果C0111:*Missingdocstring*Usedwhenamodule,function,classormethodhasnodocstring.Somespecialmethodslike__init__doesn'tnecessaryrequireadocstring.Thismessagebelongstothebasicchecker.3.开始分析每个源代码中的问题。从上面知道,第一个问题的原因是缺少docstring,在代码中增加docstring,修改后的代码如下:清单8.增加docstring修改后的源码#!/usr/bin/envpython"""Thisscriptparsethecontentofaxmlfile"""importxml.dom.minidomxmlDom=xml.dom.minidom.parse("identity.xml")organizations=xmlDom.getElementsByTagName('DW')fororginorganizations:products=org.getElementsByTagName('linux')forproductinproducts:print'ID:'+product.getAttribute('id')print'Name:'+product.getAttribute('name')print'WordCount:'+product.getAttribute('count')重新运行pylint--reports=n--include-ids=ydw.py,结果为:清单9.运行结果*************ModuledwC0322:7:OperatornotprecededbyaspacexmlDom=xml.dom.minidom.parse("identity.xml")^C0103:7:Invalidname"xmlDom"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)C0103:8:Invalidname"organizations"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)可以看到源代码中的第一个问题已被解决。4.关于第二个C0322的问题,这里的分析结果说明得比较清楚,是代码第七行中的等号
运算符
两边没有空格。我们在这里加上空格,重新运行pylint--reports=n--include-ids=ydw.py,结果为:清单10.运行结果*************ModuledwC0103:7:Invalidname"xmlDom"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)C0103:8:Invalidname"organizations"(shouldmatch(([A-Z_][A-Z0-9_]*)|(__.*__))$)5.可以看到现在问题只剩下C0103了。这里的意思是变量命名规则应该符合后面正则表达式的规定。Pylint定义了一系列针对变量,函数,类等的名字的命名规则。实际中我们不一定要使用这样的命名规则,我们可以定义
使用正则表达式
定义自己的命名规则,比如使用选项--const-rgx='[a-z_][a-z0-9_]{2,30}$',我们将变量xmlDom改为xmldom,代码如下:清单11.将变量xmlDom改为xmldom后的源码#!/usr/bin/envpython"""Thisscriptparsethecontentofaxmlfile"""importxml.dom.minidomxmldom=xml.dom.minidom.parse("identity.xml")organizations=xmldom.getElementsByTagName('DW')fororginorganizations:products=org.getElementsByTagName('linux')forproductinproducts:print'ID:'+product.getAttribute('id')print'Name:'+product.getAttribute('name')print'WordCount:'+product.getAttribute('count')运行pylint--reports=n--include-ids=y--const-rgx='[a-z_][a-z0-9_]{2,30}$'dw.py,结果中就没有任何问题了。6.如果希望一个组里的人都使用这些统一的规则,来规范一个部门的代码风格。比如说大家都使用--const-rgx='[a-z_][a-z0-9_]{2,30}$'作为命名规则,那么一个比较便捷的方法是使用
配置文件
。使用pylint--generate-rcfile>pylint.conf来生成一个示例配置文件,然后编辑其中的--const-rgx选项。或者也可以直接pylint--const-rgx='[a-z_][a-z0-9_]{2,30}$'--generate-rcfile>pylint.conf,这样生成的配置文件中--const-rgx选项直接就是'[a-z_][a-z0-9_]{2,30}$'了。以后运行Pylint的时候指定配置文件:pylint--rcfile=pylint.confdw.py这样Pylint就会按照配置文件pylint.conf中的选项来指定参数。在一个部门中,大家可以共同使用同一个配置文件,这样就可以保持一致的代码风格。7.如果把report部分加上,即不使用--reports=n,可以看到报告部分的内容。