{"id":165713,"date":"2019-12-21T10:00:19","date_gmt":"2019-12-21T02:00:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=165713"},"modified":"2019-12-02T13:54:53","modified_gmt":"2019-12-02T05:54:53","slug":"sphinx-python-tox","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sphinx-python-tox.html","title":{"rendered":"\u7528Sphinx\u7ed9Python\u4ee3\u7801\u5199\u6587\u6863\uff0c\u53c8\u5feb\u53c8\u6f02\u4eae\uff01"},"content":{"rendered":"
\u5982\u4f55\u4f7f\u7528Sphinx\u7ed9Python\u4ee3\u7801\u5199\u6587\u6863
\n\u6700\u597d\u5c06\u6587\u6863\u4f5c\u4e3a\u5f00\u53d1\u8fc7\u7a0b\u7684\u4e00\u90e8\u5206\u3002Sphinx \u52a0\u4e0a Tox\uff0c\u8ba9\u6587\u6863\u53ef\u4ee5\u8f7b\u677e\u4e66\u5199\uff0c\u5e76\u4e14\u5916\u89c2\u6f02\u4eae\u3002<\/p>\n
<\/p>\n
Python \u4ee3\u7801\u53ef\u4ee5\u5728\u6e90\u7801\u4e2d\u5305\u542b\u6587\u6863\u3002\u8fd9\u79cd\u65b9\u5f0f\u9ed8\u8ba4\u4f9d\u9760 docstring\uff0c\u5b83\u4ee5\u4e09\u5f15\u53f7\u683c\u5f0f\u5b9a\u4e49\u3002\u867d\u7136\u6587\u6863\u7684\u4ef7\u503c\u662f\u5f88\u5927\u7684\uff0c\u4f46\u662f\u6ca1\u6709\u5145\u8db3\u7684\u6587\u6863\u7684\u4ee3\u7801\u8fd8\u662f\u5f88\u5e38\u89c1\u3002\u8ba9\u6211\u4eec\u6f14\u7ec3\u4e00\u4e2a\u573a\u666f\uff0c\u4e86\u89e3\u51fa\u8272\u7684\u6587\u6863\u7684\u5f3a\u5927\u529f\u80fd\u3002
\n\u7ecf\u5386\u4e86\u592a\u591a\u5728\u767d\u677f\u6280\u672f\u9762\u8bd5\u4e0a\u8981\u6c42\u4f60\u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff0c\u4f60\u5df2\u7ecf\u53d7\u591f\u4e86\u3002\u4f60\u56de\u5bb6\u7528 Python \u5199\u4e86\u4e00\u4e2a\u53ef\u91cd\u7528\u7684\u6590\u6ce2\u90a3\u5951\u8ba1\u7b97\u5668\uff0c\u4f7f\u7528\u6d6e\u70b9\u6280\u5de7\u6765\u5b9e\u73b0 O(1) \u590d\u6742\u5ea6\u3002
\n\u4ee3\u7801\u5f88\u7b80\u5355\uff1a<\/p>\n
# fib.py\r\nimport math\r\n \r\n_SQRT_5 = math.sqrt(5)\r\n_PHI = (1 + _SQRT_5) \/ 2 \r\ndef approx_fib(n):\r\n return round(_PHI**(n+1) \/ _SQRT_5)\r\n<\/pre>\n\uff08\u8be5\u6590\u6ce2\u90a3\u5951\u6570\u5217\u662f\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u7684\u51e0\u4f55\u5e8f\u5217\uff0c\u8fd9\u662f\u6211\u6700\u559c\u6b22\u7684\u9c9c\u4e3a\u4eba\u77e5\u7684\u6570\u5b66\u4e8b\u5b9e\u4e4b\u4e00\u3002\uff09<\/p>\n
\u4f5c\u4e3a\u4e00\u4e2a\u597d\u4eba\uff0c\u4f60\u53ef\u4ee5\u5c06\u4ee3\u7801\u5f00\u6e90\uff0c\u5e76\u5c06\u5b83\u653e\u5728 PyPI \u4e0a\u3002setup.py \u6587\u4ef6\u5f88\u7b80\u5355\uff1a<\/p>\n
import setuptools \r\nsetuptools.setup(\r\n name='fib',\r\n version='2019.1.0',\r\n description='Fibonacci',\r\n py_modules=[\"fib\"],\r\n)\r\n<\/pre>\n\u4f46\u662f\uff0c\u6ca1\u6709\u6587\u6863\u7684\u4ee3\u7801\u662f\u6ca1\u6709\u7528\u7684\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u5411\u51fd\u6570\u6dfb\u52a0 docstring\u3002\u6211\u6700\u559c\u6b22\u7684 docstring \u6837\u5f0f\u4e4b\u4e00\u662f \u201cGoogle\u201d \u6837\u5f0f\u3002\u6807\u8bb0\u5f88\u8f7b\u91cf\uff0c\u5f53\u5b83\u653e\u5728\u6e90\u4ee3\u7801\u4e2d\u65f6\u5f88\u597d\u3002<\/p>\n
def approx_fib(n):\r\n \"\"\"\r\n Approximate Fibonacci sequence\r\n \r\n Args:\r\n n (int): The place in Fibonacci sequence to approximate\r\n \r\n Returns:\r\n float: The approximate value in Fibonacci sequence\r\n \"\"\"\r\n # ...\r\n<\/pre>\n\u4f46\u662f\u51fd\u6570\u7684\u6587\u6863\u53ea\u662f\u6210\u529f\u7684\u4e00\u534a\u3002\u666e\u901a\u6587\u6863\u5bf9\u4e8e\u60c5\u5883\u5316\u4ee3\u7801\u7528\u6cd5\u5f88\u91cd\u8981\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60c5\u666f\u662f\u607c\u4eba\u7684\u6280\u672f\u9762\u8bd5\u3002
\n\u6709\u4e00\u79cd\u6dfb\u52a0\u66f4\u591a\u6587\u6863\u7684\u65b9\u5f0f\uff0c\u4e13\u4e1a Python \u4eba\u7684\u65b9\u5f0f\u901a\u5e38\u662f\u5728 docs\/ \u6dfb\u52a0 rst \u6587\u4ef6\uff08 reStructuredText \u7684\u7f29\u5199\uff09\u3002\u56e0\u6b64 docs\/index.rst \u6587\u4ef6\u6700\u7ec8\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\nFibonacci\r\n=========\r\nAre you annoyed at tech interviewers asking you to implement\r\nthe Fibonacci sequence?\r\nDo you want to have some fun with them?\r\nA simple\r\n:code:`pip install fib`\r\nis all it takes to tell them to,\r\num,\r\nfib off.\r\n.. automodule:: fib\r\n :members:\r\n<\/pre>\n\u6211\u4eec\u5b8c\u6210\u4e86\uff0c\u5bf9\u5427\uff1f\u6211\u4eec\u5df2\u7ecf\u5c06\u6587\u672c\u653e\u5728\u4e86\u6587\u4ef6\u4e2d\u3002\u4eba\u4eec\u5e94\u8be5\u4f1a\u770b\u7684\u3002<\/p>\n
\u4f7f Python \u6587\u6863\u66f4\u6f02\u4eae
\n\u4e3a\u4e86\u4f7f\u4f60\u7684\u6587\u6863\u770b\u8d77\u6765\u66f4\u6f02\u4eae\uff0c\u4f60\u53ef\u4ee5\u5229\u7528 Sphinx\uff0c\u5b83\u65e8\u5728\u5236\u4f5c\u6f02\u4eae\u7684 Python \u6587\u6863\u3002\u8fd9\u4e09\u4e2a Sphinx \u6269\u5c55\u7279\u522b\u6709\u7528\uff1a
\nsphinx.ext.autodoc\uff1a\u4ece\u6a21\u5757\u5185\u90e8\u83b7\u53d6\u6587\u6863
\nsphinx.ext.napoleon\uff1a\u652f\u6301 Google \u6837\u5f0f\u7684 docstring
\nsphinx.ext.viewcode\uff1a\u5c06 ReStructured Text \u6e90\u7801\u4e0e\u751f\u6210\u7684\u6587\u6863\u6253\u5305\u5728\u4e00\u8d77
\n\u4e3a\u4e86\u544a\u8bc9 Sphinx \u8be5\u751f\u6210\u4ec0\u4e48\u4ee5\u53ca\u5982\u4f55\u751f\u6210\uff0c\u6211\u4eec\u5728 docs\/conf.py \u4e2d\u914d\u7f6e\u4e00\u4e2a\u8f85\u52a9\u6587\u4ef6\uff1a<\/p>\nextensions = [\r\n 'sphinx.ext.autodoc',\r\n 'sphinx.ext.napoleon',\r\n 'sphinx.ext.viewcode',\r\n]\r\n# \u8be5\u5165\u53e3\u70b9\u7684\u540d\u79f0\uff0c\u6ca1\u6709 .rst \u6269\u5c55\u540d\u3002\r\n# \u60ef\u4f8b\u8be5\u540d\u79f0\u662f index\r\nmaster_doc = \"index\"\r\n# \u8fd9\u4e9b\u503c\u5168\u90e8\u7528\u5728\u751f\u6210\u7684\u6587\u6863\u5f53\u4e2d\u3002\r\n# \u901a\u5e38\uff0c\u53d1\u5e03\uff08release\uff09\u4e0e\u7248\u672c\uff08version\uff09\u662f\u4e00\u6837\u7684\uff0c\r\n# \u4f46\u662f\u6709\u65f6\u5019\u6211\u4eec\u4f1a\u6709\u5e26\u6709 rc \u6807\u7b7e\u7684\u53d1\u5e03\u3002\r\nproject = \"Fib\"\r\ncopyright = \"2019, Moshe Zadka\"\r\nauthor = \"Moshe Zadka\"\r\nversion = release = \"2019.1.0\"\r\n<\/pre>\n\u6b64\u6587\u4ef6\u4f7f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6240\u9700\u7684\u6240\u6709\u5143\u6570\u636e\u6765\u53d1\u5e03\u4ee3\u7801\uff0c\u5e76\u6ce8\u610f\u6269\u5c55\u540d\uff08\u4e0a\u9762\u7684\u6ce8\u91ca\u8bf4\u660e\u4e86\u65b9\u5f0f\uff09\u3002\u6700\u540e\uff0c\u8981\u786e\u4fdd\u751f\u6210\u6211\u4eec\u60f3\u8981\u7684\u6587\u6863\uff0c\u8bf7\u4f7f\u7528 Tox \u7ba1\u7406\u865a\u62df\u73af\u5883\u4ee5\u786e\u4fdd\u6211\u4eec\u987a\u5229\u751f\u6210\u6587\u6863\uff1a<\/p>\n
[tox]\r\n# \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c`.tox` \u662f\u8be5\u76ee\u5f55\u3002\r\n# \u5c06\u5176\u653e\u5728\u975e\u70b9\u6587\u4ef6\u4e2d\u53ef\u4ee5\u4ece\r\n# \u6587\u4ef6\u7ba1\u7406\u5668\u6216\u6d4f\u89c8\u5668\u7684\r\n# \u6253\u5f00\u5bf9\u8bdd\u6846\u4e2d\u6253\u5f00\u751f\u6210\u7684\u6587\u6863\uff0c\r\n# \u8fd9\u4e9b\u5bf9\u8bdd\u6846\u6709\u65f6\u4f1a\u9690\u85cf\u70b9\u6587\u4ef6\u3002\r\ntoxworkdir = {toxinidir}\/build\/tox\r\n \r\n[testenv:docs]\r\n# \u4ece `docs` \u76ee\u5f55\u5185\u8fd0\u884c `sphinx`\uff0c\r\n# \u4ee5\u786e\u4fdd\u5b83\u4e0d\u4f1a\u62fe\u53d6\u4efb\u4f55\u53ef\u80fd\u8fdb\u5165\u9876\u5c42\u76ee\u5f55\u4e0b\u7684\r\n# \u865a\u62df\u73af\u5883\u6216 `build\/` \u76ee\u5f55\u4e0b\u7684\u5176\u4ed6\u5de5\u4ef6\u7684\u6742\u6563\u6587\u4ef6\u3002\r\nchangedir = docs\r\n# \u552f\u4e00\u7684\u4f9d\u8d56\u5173\u7cfb\u662f `sphinx`\u3002\r\n# \u5982\u679c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u5355\u72ec\u6253\u5305\u7684\u6269\u5c55\u7a0b\u5e8f\uff0c\r\n# \u6211\u4eec\u5c06\u5728\u6b64\u5904\u6307\u5b9a\u5b83\u4eec\u3002\r\n# \u66f4\u597d\u7684\u505a\u6cd5\u662f\u6307\u5b9a\u7279\u5b9a\u7248\u672c\u7684 sphinx\u3002\r\ndeps =\r\n sphinx\r\n# \u8fd9\u662f\u7528\u4e8e\u751f\u6210 HTML \u7684 `sphinx` \u547d\u4ee4\u3002\r\n# \u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u751f\u6210 PDF \u6216\u7535\u5b50\u4e66\u3002\r\ncommands =\r\n sphinx-build -W -b html -d {envtmpdir}\/doctrees . {envtmpdir}\/html\r\n# \u6211\u4eec\u4f7f\u7528 Python 3.7\u3002\r\n# Tox \u6709\u65f6\u4f1a\u6839\u636e testenv \u7684\u540d\u79f0\u5c1d\u8bd5\u81ea\u52a8\u68c0\u6d4b\u5b83\uff0c\r\n# \u4f46\u662f `docs` \u6ca1\u6709\u7ed9\u51fa\u6709\u7528\u7684\u7ebf\u7d22\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u660e\u786e\u5b83\u3002\r\nbasepython = python3.7\r\n<\/pre>\n\u73b0\u5728\uff0c\u65e0\u8bba\u4f55\u65f6\u8fd0\u884c Tox\uff0c\u5b83\u90fd\u4f1a\u4e3a\u4f60\u7684 Python \u4ee3\u7801\u751f\u6210\u6f02\u4eae\u7684\u6587\u6863\u3002
\n\u5728 Python \u4e2d\u5199\u6587\u6863\u5f88\u597d
\n\u4f5c\u4e3a Python \u5f00\u53d1\u4eba\u5458\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7684\u5de5\u5177\u94fe\u5f88\u68d2\u3002\u6211\u4eec\u53ef\u4ee5\u4ece docstring \u5f00\u59cb\uff0c\u6dfb\u52a0 .rst \u6587\u4ef6\uff0c\u7136\u540e\u6dfb\u52a0 Sphinx \u548c Tox \u6765\u4e3a\u7528\u6237\u7f8e\u5316\u7ed3\u679c\u3002
\nvia: https:\/\/opensource.com\/article\/19\/11\/document-python-sphinx
\n\u4f5c\u8005\uff1aMoshe Zadka \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy
\n\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4f7f\u7528Sphinx\u7ed9Python\u4ee3\u7801\u5199\u6587\u6863 \u6700\u597d\u5c06\u6587\u6863\u4f5c\u4e3a\u5f00\u53d1\u8fc7\u7a0b\u7684\u4e00\u90e8\u5206\u3002Sphinx \u52a0\u4e0a Tox\uff0c\u8ba9\u6587 […]<\/p>\n","protected":false},"author":1898,"featured_media":165716,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[472],"class_list":["post-165713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-sphinx"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=165713"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165713\/revisions"}],"predecessor-version":[{"id":165717,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165713\/revisions\/165717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/165716"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=165713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=165713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=165713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}