[TOC]
一、代码风格指南
PEP 8
- https://www.python.org/dev/peps/pep-0008/
- Python 代码风格指南(Style Guide for Python Code)
- 规则:行长度、缩进、多行表达式、命名约定等
二、静态代码风格检查工具
Pylint
- https://www.pylint.org/
python -m pip install pylint1
2
3
4
5
6
7
8pylint -h
pylint --persistent=n --generate-rcfile > pylint.conf # 生成默认的配置文件
pylint --rcfile=pylint.conf xxx.py # 检查单个文件
pylint --rcfile=pylint.conf package # 检查某个包
--ignore=<file> # 忽视文件
--reports=n 或 -rn # 简单报告
--disable-msg=C0103,C0301,W0312,W0511,W0232,E1101 # 禁止指定id的msg
pylint --help-msg=[id] # 查看错误的详细信息
Flake8
- https://flake8.pycqa.org/en/latest/
- 检查代码逻辑
PyFlakes
、编码风格Pep8
、代码复杂度NedBatchelder’s McCabe
python -m pip install flake8 pep8-naming flake8-import-order flake8-todo flake8-quotes # 风格、命名、导入顺序、todo、引号1
2
3
4flake8 --version # 检查安装了哪些插件
flake8 --first --import-order-style=google test.py
flake8 xxx_dir
flake8 --import-order-style=google --exclude=*test*.py,*.pyc --config=cicd/.flake8
pycodestyle
Google的pytype、Microsoft的pyright、Facebook的Pyre Pysa
三、代码格式化工具
Autopep8
- https://github.com/hhatto/autopep8
- python -m pip install autopep8
Yapf
- https://github.com/google/yapf
- python -m pip install yapf
Black
- https://github.com/psf/black
- python -m pip install black
四、覆盖率测试工具
Coverage
五、单元测试
nose
- https://nose.readthedocs.io/en/latest/
- python -m pip install nose
- nosetests only_test_this.py