开源python-代码规范与检查

python开源系列文章

代码规范

  • PEP8
  • 谷歌代码规范

代码检查

  • flake8
  • pylint
  • mypy
  • pyright

flake操作可以参考这了:https://github.com/tensorflow/addons/blob/master/tools/format.py

# 检查整个项目:
flake8 path/to/your_project/

# 检查单个文件:
flake8 path/to/your_file.py 

其中,可以在命令行上进行配置,也可以使用配置文件,配置文件可以在最高级的用户目录下,或者当前目录下,setup.cfg,tox.ini, 或者.flake8 文件都可以,文件需要以 [flake8] 段落开头:

[flake8]
max-line-length = 120
show-source = true
pre-commit

实际项目可以通过pre-commit ,在执行特定 git 操作的时候运行这些脚本。例如,在 commit或者 push之前运行一下 flake8、 bandit和 safety这些工具。

参考官方文档中的quick start
1 Install pre-commit : pip install pre-commit
2 Add a pre-commit configuration
3 用 pre-commit install安装git hooks到你的 .git/目录, pre-commit install
4. 运行 pre-commit run --all-files

若检测出错,则无法commit,也就无法push,保证了出错代码只在本地,不会把问题提交到远程仓库

  1. 如果不想要commit之前检查,直接把以下文件删除。因为有时候实在跑不通
rm .git/hooks/pre-commit

Mypy: Optional Static Typing for Python

另外,比较新的也可以使用mypy进行检查

参考

  • https://zhuanlan.zhihu.com/p/400870942
  • https://zhuanlan.zhihu.com/p/250493093
  • https://zhuanlan.zhihu.com/p/94008510
  • https://zhuanlan.zhihu.com/p/65820736
  • https://www.zhihu.com/people/hbaaron/posts
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YueTann

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值