什么样的 python 可以可谓专业 PyPI 项目?刚刚学到三个概念:pep8、Sphinx、pytest与GitHub Action的集成

前言: 最近在读很火的 tianshou (基于 pytorch 提供深度强化学习算法的简易接口),两个清华本科生做的。很规范、很优秀的项目。 做出来的项目,想要让别人使用、维护、建立良性可持续社区,项目结构清晰、写注释、生成文档是必不可少的。 我在 GitHub 上 watch 了该项目,现在项目文档工作正如火如荼,我的邮件提示也常常响起:“你看看,人家清华巨佬们今天又在努力工作学习,刚刚又提交了一个 commit ,而你写的幼儿园级别算法甚至都不收敛至最优解!”嗯… watch 了人家的项目,时刻提醒自己:已经很渣了,再不努力就被落下太远了。说正题,通过阅读这个正在生长的项目, 我学到了一个健康项目该有的文档与测试规范pep8SphinxpytestGitHub Action 的集成。

强化学习算法接口集成的必要性

强化学习与传统的“监督学习”、“非监督学习”不同,强化学习要时刻与环境/模型交互,以传输数据。这就不能简单地将数据输入,而要整理算法与数据的接口,将二者连接起来。

这个问题我在 https://blog.csdn.net/weixin_42815609/article/details/105318763 中简单探讨过。

所以可以看出,清华大佬做的东西不但牛逼、好用,还有意义,将帮助很多 DRL 入门者及很多离入门都很远的渣渣(比如我)更好地学习并尝试实践 DRL

pep8

“PEP 是 Python Enhancement Proposal 的缩写,翻译过来就是 Python风格建议书”。PEP8 应该其第8章,标题为 Style Guide for Python Code ,给出了代码样式(标准化、统一)的意见。

pep8 的大名我早就听说过,起初我对其理解是这样的:pep8 告诉我们缩进要有 4 个空格!

学了一些 python 后,我的理解是这样的: pep8 告诉我们类名首字母大写!

到现在,我才明白,pep8 大概是这样的:

  • pep8 确实对代码风格、样式做出了规范,使代码标准化,更易读
  • 但 pep8 真正牛逼之处在于,规范后的项目,其注释、结构可以被 Sphinx 、pydoc 解析,也就意味着,如果你只写代码文件,并且遵从 pep8 的规范在代码之间写注释,那么你的项目的说明文档也就自动生成了

Sphinx

清华巨佬应该是用 Sphinx 生成的 .rst文件,之后通过 make html 生成网页文件再投到网上。整个过程全部自动化,符合程序员“懒惰”的美德。

你看上图,清华大佬只要在代码中加一些注释(右),然后他在本地就能自动生成 html ;他上传到服务器后,官网上就有了相应的说明文档(左)!

下面是大佬写的 Contributing guidelines ,我就是通过阅读这个 .md 文件才知道检索、并学习到上述技术的。

Contributing

To install Tianshou in an “editable” mode, run

pip install -e .

in the main directory. This installation is removable by

python setup.py develop --uninstall

Additional dependencies for developments can be installed by

pip install ".[dev]"

Tests

This command will run automatic tests in the main directory

pytest test --cov tianshou -s

To run on your own GitHub Repo, enable the GitHub Action and it will automatically run the test.

PEP8 Code Style Check

We follow PEP8 python code style. To check, in the main directory, run:

flake8 . --count --show-source --statistics

Documents

Documents are written under the docs/ directory as RestructuredText (.rst) files. index.rst is the main page. A Tutorial on RestructuredText can be found here.

API References are automatically generated by Sphinx according to the outlines under
doc/api/ and should be modified when any code changes.

To compile docs into webpages, run

make html

under the docs/ directory. The generated webpages are in docs/_build and
can be viewed with browsers.

引用自:https://github.com/thu-ml/tianshou/blob/master/CONTRIBUTING.md

pytest与GitHub Action的集成

我一直不太理解什么是 CI/CD 和 GitHub 的 workflow 是做什么的,现在大概明白一些了:

  • 在我看来,持续集成服务就是将合格的、写完的新版代码自动推出;
  • 这其中可能涉及到一些动作(命令),而 GitHub Action 将其打包,不需要每次发布新版本时都输入这些命令;
  • 现在算法工程师们将其用在算法测试上,比如上午提到的 pytest test --cov tianshou -s :To run on your own GitHub Repo, enable the GitHub Action and it will automatically run the test.

Piper Liu
2020-4-6 00:15:11

本来想零点前写完的。不管怎么说,还是应该早睡早起,晚安。

原创文章 163 获赞 177 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_42815609/article/details/105336408
今日推荐