需求背景:

Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。

Pylint 是一个 Python 工具,除了平常代码分析工具的作用之外,它提供了更多的功能:如检查一行代码的长度,变量名是否符合命名标准,一个声明过的接口是否被真正实现等等。

Pylint 的一个很大的好处是它的高可配置性,高可定制性,并且可以很容易写小插件来添加功能。

项目中需要做代码规范检查,所以研究一下pylint的使用。

pylint使用:

  • 安装pylint:

>pip install pylint

确认pylint安装成功:

>pylint --version
No config file found, using default configuration
pylint 1.6.5,
astroid 1.4.9
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
  • 生成默认配置文件:

>pylint --persistent=n --generate-rcfile > pylint.conf
No config file found, using default configuration

查看当前目录下,已经生成了名为pylint.conf的文件,该文件中的配置项都是pylint的默认配置,比较大400多行。

  • check单个文件:

>pylint --rcfile=pylint.conf manage.py
  • check结果总览:
************* Module manage
C: 1, 0: Missing module docstring (missing-docstring)
W: 14,12: Unused import django (unused-import)

报告中上述格式为检查结果总览:C表示convention,规范;W表示warning,告警;pylint结果总共有四个级别:error,warning,refactor,convention,可以根据首字母确定相应的级别。1, 0表示告警所在文件中的行号和列号。

  • 4种级别告警汇总:
Messages by category
+-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention |1 |NC |NC |
+-----------+-------+---------+-----------+
|refactor |0 |NC |NC |
+-----------+-------+---------+-----------+
|warning |1 |NC |NC |
+-----------+-------+---------+-----------+
|error |0 |NC |NC |
+-----------+-------+---------+-----------+
  • 消息类型统计:
Messages
+------------------+------------+
|message id |occurrences |
+==================+============+
|mixed-indentation |8 |
+------------------+------------+
|unused-import |2 |
+------------------+------------+
|invalid-name |2 |
+------------------+------------+
|redefined-builtin |1 |
+------------------+------------+
  • check整个工程:

    目前看只能一个module一个module的pylint,但是如果在工程根目录下添加init.py文件,即把工程当做一个python包的话,可以对整个工程进行pylint。

pylint api(api为包名称)

重命名pylint.conf为.pylintrc,即不需要每次执行都带上--rcfile参数了:

pylintrc in the current working directory
.pylintrc in the current working directory
If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.
The file named by environment variable PYLINTRC
if you have a home directory which isn’t /root:
.pylintrc in your home directory
.config/pylintrc in your home directory
/etc/pylintrc

pylint集成到PyCharm:

打开File->设置对话框:

settings.png

单击ToolsExternal Tools,添加External Tool:

Create Tool.png

这样修改完代码后,Tools菜单下就会多出pylint工具了:

如果想要pylint当前文件,只要运行上图的pylint工具即可。

Pylint在项目中的使用的更多相关文章

  1. [git]安装git-pylint-commit-hook提高python项目中的代码质量

    什么是'git-pylint-commit-hook' 我在工作中,团队为了保证代码和提高代码的质量,要求每个项目都要求安装git-pylint-commit-hook,它是个钩子,会在你提交代码到本 ...

  2. VS项目中使用Nuget还原包后编译生产还一直报错?

    Nuget官网下载Nuget项目包的命令地址:https://www.nuget.org/packages 今天就遇到一个比较奇葩的问题,折腾了很久终于搞定了: 问题是这样的:我的解决方案原本是好好的 ...

  3. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  4. iOS 之项目中遇到的问题总结

    昨天去一家公司面试,面试官问了我在项目开发中遇到过哪些问题,是什么引起的,怎样解决的? 当时由于有点小紧张只说出了一两点,现在就来好好总结一下. 问题: 1.两表联动 所谓的两表联动就是有左右两个表格 ...

  5. My97DatePicker时间控件在项目中的应用

    一.下载My97DatePicker的压缩包My97DatePicker.rar,解压. 注:My97DatePicker最新版本有开发包,项目中使用时删掉,以便节省空间,提高程序的运行效率. 二.在 ...

  6. 在项目中同时使用Objective-C和Swift

    苹果发布的Swift语言可以和之前的Objective-C语言同时存在于一个项目中. 可能有人会认为是同一个类文件中既可以有Objective-C也可以有Swift,这是不对的.同一个类文件或同一个代 ...

  7. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  8. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

  9. Web API项目中使用Area对业务进行分类管理

    在之前开发的很多Web API项目中,为了方便以及快速开发,往往把整个Web API的控制器放在基目录的Controllers目录中,但随着业务越来越复杂,这样Controllers目录中的文件就增加 ...

随机推荐

  1. 在C++程序中开启和禁用Windows设备的无线网卡的方法

    原文链接地址:https://www.jb51.net/article/81340.htm 1.列出当前网卡:SetupDiEnumDeviceInfo 2.找出当前无线网卡的名字(用natvie w ...

  2. Sort Integers

    ) algorithm. 分析 bubble sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Solution ...

  3. AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)

    结论:每次把字符丢到最外面最优,用树状数组统计答案,把字符放到最外边后可以当成消失了,直接在树状数组上删掉就好. 感性理解是把字符丢到中间会增加其他字符的移动次数,但是丢到外面不会,所以是正确的. # ...

  4. pxp

    Time Limit: 2000 ms Memory Limit: 512 MB Description 给定 \(n\), 求\(\sum\limits_{p,q∈primes}[pq≤n]\) ( ...

  5. Codeforces 19.E Fairy

    E. Fairy time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ou ...

  6. 安装好dashboard 登录出现错误

    验证发生错误.请稍后再试一次. While turning SELinux off certainly does the trick, it is somewhat like using a sled ...

  7. 远程调试openstack

    之前一直没有找到方法调试openstack的horizon代码,现在终于找到方法了,特别感谢下面这篇博客,讲解非常清晰: http://blog.csdn.net/tantexian/article/ ...

  8. LeetCode-Insertion Sort List[AC源码]

    package com.lw.leet5; /** * @ClassName:Solution * @Description: * Insertion Sort List * Sort a linke ...

  9. HDU 2157 How many ways?? 临接矩阵+快速幂

    Problem Description 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, ...

  10. Linux改变用户shell的类型

    命令: 改变usr01的类型 # usermod -s /bin/csh usr01