# 如何使用Sphinx记录Python代码【Programming（Python）】

Python代码可以在其源代码中包含文档。 这样做的默认方式依赖于docstrings ，它们以三引号格式定义。 虽然文档的价值是有据可查的，但似乎似乎很普遍，没有足够的文档代码。 让我们来看一个有关强大文档功能的场景。

```# fib.py
import math

_SQRT_5 = math.sqrt(5)
_PHI = (1 + _SQRT_5) / 2

def approx_fib(n):
return round(_PHI**(n+1) / _SQRT_5)```

（斐波那契数列是四舍五入到最接近的整数的几何序列，这是我最喜欢的鲜为人知的数学事实之一。）

```import setuptools

setuptools.setup(
name='fib',
version='2019.1.0',
description='Fibonacci',
py_modules=["fib"],
)```

```def approx_fib(n):
"""
Approximate Fibonacci sequence

Args:
n (int): The place in Fibonacci sequence to approximate

Returns:
float: The approximate value in Fibonacci sequence
"""
# ...```

```Fibonacci
=========

Are you annoyed at tech interviewers asking you to implement
the Fibonacci sequence?
Do you want to have some fun with them?
A simple
:code:`pip install fib`
is all it takes to tell them to,
um,
fib off.

.. automodule:: fib
:members:```

# 使Python文档更漂亮

• sphinx.ext.autodoc ：从模块内部获取文档
• sphinx.ext.viewcode ：将ReStructured Text源与生成的文档打包在一起

```extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
]
# The name of the entry point, without the ".rst" extension.
# By convention this will be "index"
master_doc = "index"
# This values are all used in the generated documentation.
# Usually, the release and version are the same,
# but sometimes we want to have the release have an "rc" tag.
project = "Fib"
version = release = "2019.1.0"```

```[tox]
# By default, .tox is the directory.
# Putting it in a non-dot file allows opening the generated
# documentation from file managers or browser open dialogs
# that will sometimes hide dot files.
toxworkdir = {toxinidir}/build/tox

[testenv:docs]
# Running sphinx from inside the "docs" directory
# ensures it will not pick up any stray files that might
# get into a virtual environment under the top-level directory
# or other artifacts under build/
changedir = docs
# The only dependency is sphinx
# If we were using extensions packaged separately,
# we would specify them here.
# A better practice is to specify a specific version of sphinx.
deps =
sphinx
# This is the sphinx command to generate HTML.
# In other circumstances, we might want to generate a PDF or an ebook
commands =
sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html
# We use Python 3.7. Tox sometimes tries to autodetect it based on the name of
# the testenv, but "docs" does not give useful clues so we have to be explicit.
basepython = python3.7```

# Python文档非常出色

0 条评论

• ### 约束编程示例【Programming】

通过示例应用程序了解约束编程，该示例应用程序可以转换字符的大小写和ASCII代码。

• ### 如何使用Protobuf进行数据交换【Programming（Go）】

在用不同语言编写的应用程序之间以及在不同平台上运行的应用程序之间交换数据时，Protobuf 编码提高了效率。

• ### Percona Server 5.7 并行doublewrite 特性

在这篇文章中，我们将由里及外讨论Percona Server 5.7的并行doublewrite。

• ### VBA小技巧04：使用VBA获取能够打开指定文件的EXE程序

这是一个很有趣的技巧！可以通过你给定的文件名来获取计算机中可以打开该文件的EXE程序，即可执行程序。有时候，我们可能真的需要找到可以打开指定文件名的EXE程序，...

• ### restful api设计最佳实践

考虑api的安全性，建议使用https访问。 证书可使用let’s encrypt的免费证书。

• ### [PHP] PHP5中的写时复制change on write

但是当新的变量值变更时 , 值从新赋予新的值时 , 就会减掉刚才的引用计数,并且从新创建内存空间.

• ### 解决java compiler level does not match the version of the installed java project facet

java compiler level does not match the version of the installed java project fa...

• ### Excel应用实践20：使用Excel中的数据自动填写Word表格

我想将这些数据逐行自动输入到Word文档的表格中并分别自动保存，Word文档表格如下图2所示，文档名为“datafromexcel.docx”。

• ### Sketch67来了！可嵌入字体，性能大幅提升，你更新了吗？

静电说：sketch现在更新的速度就像之前的Firefox浏览器一样迅速，经过几年的发展，现在几乎一个月就一个大版本，非常给力。而目前sketch67版本还算是...