python文档生成工具pydoc使用介绍

在python中有很多很好的工具来生成字符串文档(docstring),比如说: epydoc、doxygen、sphinx,但始终觉得pydoc还是不错的工具,用法非常简单,功能也算不错,本文主要介绍pydoc.
pydoc是python自带的模块,主要用于从python模块中自动生成文档,这些文档可以基于文本呈现的、也可以生成web 页面的,还可以在服务器上以浏览器的方式呈现!
【用法】

windows下:

代码如下:

d:\>python -m pydoc # 比如说: python -m pydoc math
-m参数:python以脚本的方法运行模块

linux/unix下:

代码如下:

$ pydoc # 比如说: pydoc

【帮助】

代码如下:

$ pydoc -h
pydoc – the python documentation tool

pydoc …
show text documentation on something. may be the name of a
python keyword, topic, function, module, or package, or a dotted
reference to a class or function within a module or module in a
package. if contains a ‘/’, it is used as the path to a
python source file to document. if name is ‘keywords’, ‘topics’,
or ‘modules’, a listing of these things is displayed.

pydoc -k
search for a keyword in the synopsis lines of all available modules.

pydoc -p
start an http server on the given port on the local machine.

pydoc -w …
write out the html documentation for a module to a file in the current
directory. if contains a ‘/’, it is treated as a filename; if
it names a directory, documentation is written for all the contents.

【参数 -p】在本地机器上,按照给定的端口启动http,

代码如下:

d:\>python -m pydoc -p 1234 #比如说: 端口为1234
pydoc server ready at http://localhost:1234/
pydoc server stopped

在ie中输入:http://localhost:1234/,效果如图:

【参数 -k】在所有可用的模块中按关键字搜索

代码如下:

$ pydoc -k xml.sax
xml.sax (package) – simple api for xml (sax) implementation for python.
xml.sax._exceptions – different kinds of sax exceptions
xml.sax.expatreader – sax driver for the pyexpat c module. this driver works with
xml.sax.handler – this module contains the core classes of version 2.0 of sax for python.
xml.sax.saxutils – a library of useful helper classes to the sax classes, for the
xml.sax.xmlreader – an xml reader is the sax 2 name for an xml parser. xml parsers

【参数 -w】将指定模块的文本字符串生成html格式
比如说,在window下面,执行下面命令:

代码如下:

d:\learn\python>python -m pydoc math -w math.html # math是模块名,-w:写

那么在d:\learn\python目录下会生成math.html文件,显示如下:

因为是自带的模块,所以右上角显示(built-in)字样
【例子】自写的模块my_doc.py

代码如下:

””’
showoff features of pydoc module
this is easy module to demonstrate docstrings
”’
__authors__ = ‘alice & fred’
__version__ = ‘version 1.10’
__license__ = ‘copyright…’

class myclass:
””’
demonstrate class docstrings

”’
def __init__(self, spam=1, eggs=2):
””’
set the default attributevalues only
keyword arguments:
spam – a processed meat product
eggs – a fine breakfast for lumberjacks
”’
self.spam = spam
self.eggs = eggs

def square(x):
””’
square of the param
”’
return x * x

执行命令:

代码如下:

d:\learn\python> python -m pydoc my_doc

执行结果:

代码如下:

help on module my_doc:

name
my_doc

file
d:\learn\python\my_doc.py

description
showoff features of pydoc module
this is easy module to demonstrate docstrings

classes
myclass

class myclass
| demonstrate class docstrings
|
| methods defined here:
|
| __init__(self, spam=1, eggs=2)
| set the default attributevalues only
| keyword arguments:
| spam – a processed meat product
| eggs – a fine breakfast for lumberjacks

functions
square(x)
square of the param

data
__authors__ = ‘alice & fred’
__license__ = ‘copyright…’
__version__ = ‘version 1.10’

version
version 1.10

执行命令:

代码如下:

d:\learn\python>python -m pydoc -w my_doc my_doc.html
wrote my_doc.html
no python documentation found for ‘my_doc.html’

执行结果:

Posted in 未分类