Skip to content

Latest commit

 

History

History
230 lines (169 loc) · 5.34 KB

232-548470-[专业选修]代码质量检测_pylint.sy.md

File metadata and controls

230 lines (169 loc) · 5.34 KB
show version enable_checker
step
1.0
true

类型提示(type hint)

回忆

  • 上次学习了mypy
  • 可以对python文件进行静态类型检查
    • 说好了是int的
    • 就一定是int的
    • 真的认真做检查
  • 不过遇到未知的模块调用
    • 还是会执行的
    • 毕竟检查和运行是两回事
    • 你也不知道别人怎么用你的这个函数
    • 你也不知道人家传过来什么实参
  • 除了静态类型检查
    • 可以让python文件更加规范一些么?

pylint

图片描述

  • pylint
    • python 的 linter
  • 什么是linter呢?

linter

  • lint
    • 就是绒毛

图片描述

  • 衣服上有很多猫毛、狗猫
  • 不太严肃
  • 用linter给滚下来

图片描述

  • 衣服上的大小毛病(Error)
  • 甚至小瑕疵(Warning)
  • 都能给挑出来
  • 为什么叫做linter呢?

回到1969年

  • Stephen Curtis Johnson

图片描述

  • 5岁开始接触电脑
    • 他的爷爷是统计局的
    • 跟他说过这么句话

Well, we can now send a message around the world in 1/7 of a second, but it still takes 20 years for that same message to go through 1/4 inch of human skull!’

  • 他后来去了真金时代的贝尔实验室
  • 是核心成员
  • 写了三个东西
    • YACC
    • Portable C Compiler
    • Lint(粘毛器)
  • 我们先看看目前的代码

代码

from typing import Set
def get_factor_set(num: int) -> Set[int]:
    s: Set[int] = {1}
    for i in range(2, num + 1):
        if num % i == 0:
            s.add(i)
    return s

def gcd(n1: int, n2: int) -> Set[int]:
    s1: Set[int] = get_factor_set(n1)
    s2: Set[int] = get_factor_set(n2)
    common_devisor_set: Set[int] = s1.intersection(s2)
    return max(common_devisor_set)

print(gcd(3600,4096))
  • 这个代码可以运行
  • 试一下这个linter

pylint

图片描述

  • 有各种问题
  • 分成不同级别

问题级别

  • 层次清晰,从低到高

    • 惯用法
      • C convention related checks for coding standard violation
    • 重构相关
      • R refactoring related checks
    • 警告⚠️
      • W various warnings
    • 错误
      • E errors, for probable bugs in the code
    • 致命
      • F fatal, if an error occurred which prevented pylint from doing further processing
  • warning 是警告

    • 程序中的警告可以忽略
    • 不影响运行效果

warning

  • 生活中 别拿警告
    • 不当回事
    • 那就会很危险

图片描述

  • 程序中的 警告 最好也给
    • 改掉

修改

图片描述

  • 没有模块文档字符串
  • 也没有函数文档字符串

图片描述

  • 修改完成
  • 继续

继续修改

图片描述

  • 都是变量命名法的问题
  • 要求蛇形命名
"""this is gcd module"""
from typing import Set
def get_factor_set(num: int) -> Set[int]:
    """get factor set from num """
    set_temp: Set[int] = {1}
    for i in range(2, num + 1):
        if num % i == 0:
            set_temp.add(i)
    return set_temp

def gcd(num_1: int, num_2: int) -> Set[int]:
    """ get gce from two numbers"""
    set_1: Set[int] = get_factor_set(num_1)
    set_2: Set[int] = get_factor_set(num_2)
    common_devisor_set: Set[int] = set_1.intersection(set_2)
    return max(common_devisor_set)

print(gcd(3600,4096))
  • 修改掉了所有瑕疵
  • 最终得到10分

图片描述

  • 谁要求的呢?

pep0008

  • 这是一个关于编程风格的文档
  • 可以决定代码的味道(smells)

图片描述

  • 从各类标识符
  • 到函数之间中间空几行
  • 规矩很多
  • 而且越来越细
  • 如果通过了pylint
  • 就是符合这个编码规范的

mypy vs pylint

  • mypy是静态类型检查
    • 检查的是数据类型
  • pylint是静态代码分析工具
    • 分析代码错误
    • 分析代码风格
    • 分析得比较严格
    • 类似的还有flake8
      • 也是基于pep8对代码做静态分析
  • 如何理解静态呢?

图片描述

  • 1 + "1" 是动态(dynamic)运行时(runtime)才能发现错误的
  • 静态(static)分析(analyze)发现不了错误
  • 那有没有动态代码分析工具呢?

动态代码分析

图片描述

  • 动态(dynamic)代码分析(analyze)可以分析运行时(runtime)的代码
  • 只有运行的时候才能进行
  • 一般用python运行代码
  • 看python报错
  • 所以动态代码分析工具就是python
  • 囧rz

总结

  • 我们这次研究了pylint
    • lint是粘毛器
    • 可以把很多小毛病粘出来
    • 一个个改
    • 挑毛病的理由是pep8
    • 都改好了就统一了代码规范
  • python还有什么好玩的么?🤔
  • 我们下次再说👋