清单 1: 附带典型文档的模块mymod.py
#!/usr/bin/python
"""Show off features of [pydoc] module
This is a silly module to
demonstrate docstrings
"""
__author__ = 'David Mertz'
__version__= '1.0'
__nonsense__ = 'jabberwocky'
class MyClass:
"""Demonstrate class docstrings"""
def __init__ (self, spam=1, eggs=2):
"""Set default attribute values only
Keyword arguments:
spam ― a processed meat product
eggs ― a fine breakfast for lumberjacks
"""
self.spam = spam
self.eggs = eggs
pydoc 模块利用了 Python文档的约定,又使用了一些有关 Python导入、继承和其它类似的实用知识。此外, pydoc 有绝对的天赋可以使自己在不同的操作模式下被使用(马上就能看到更多有关这个论点的资料)。让我们用一些时间,看看通过OS 命令行调用的 manpage 风格的用法。
假设您已将上述模块 mymod 安装在您的系统上,但不知道它有什么用处(在示例中并不多)。您可以阅读源代码,不过更简单的方法可能是:
清单2:获取‘manpage’风格的文档
% pydoc.py mymod
Python Library Documentation: module mymod
NAME
mymod - Show off features of [pydoc] module
FILE
/articles/scratch/cp18/mymod.py
DESCRIPTION
This is a silly module to
demonstrate docstrings
CLASSES
MyClass
class MyClass
| Demonstrate class docstrings
|
| __init__(self, spam=1, eggs=2)
| Set default attribute values only
|
| Keyword arguments:
| spam ― a processed meat product
| eggs ― a fine breakfast for lumberjacks
DATA
__author__ = 'David Mertz'
__file__ = './mymod.pyc'
__name__ = 'mymod'
__nonsense__ = 'jabberwocky'
__version__ = '1.0'
VERSION
1.0
AUTHOR
David Mertz
根据特定的平台和安装过程,上述样本可能会显示在一个允许滚屏、搜索等功能并突出显示某些关键字的文本查看器中。对于像这样简单的示例,只是比纯粹的阅读源代码好一点。但请考虑一下像下面这样简单的示例:
清单 3:检查类的继承结构
% cat mymod2.py
from mymod import MyClass
class MyClass2(MyClass):
"""Child class"""
def foo(self):
pass
% pydoc.py mymod2.MyClass2
Python Library Documentation: class MyClass2 in mymod2
class MyClass2(mymod.MyClass)
| Child class
|
| __init__(self, spam=1, eggs=2) from mymod.MyClass
|
| foo(self)
在这个快速报告中,我们可以知道 MyClass2 有 __init__() 和 foo() 方法(以及相应的参数),哪个方法是类自身实现的以及其它哪些方法是继承而来(以及被继承的类所处的位置)。
另一个美妙的类似于 manpage 的功能是用来在模块中搜索关键字的 -k 选项。例如:
清单 4:为任务定位适当的模块
% pydoc.py -k uuencode
uu - Implementation of the UUencode and UUdecode functions.
% pydoc.py uu
Python Library Documentation: module uu
NAME
uu - Implementation of the UUencode and UUdecode functions.
[...]
pydoc 除了它的命令行用法之外,还有其它四种“模式”可以显示被生成的同样的文档。
#------- Interactive shell with help enhancements ------#
>>> from pydoc import help
>>> import uu
>>> help(uu.test)
Help on function test in module uu:
test()
uuencode/uudecode main program
>>> help
Welcome to Python 2.0! This is the online help utility.
[...introductory message about help shell...]
help>
Web 服务器模式:仅使用 -p 选项, pydoc 就会在 LOCALHOST 上作为一个简单的 Web 服务器自启动。您可以使用任何Web浏览器浏览所有已安装在现有操作系统上的模块。这个服务器的主页是一张模块列表,根据目录(并用浏览器支持的醒目色块)将它们分组。此外,您查看其文档的每个模块也广泛分布着它导入的函数、方法以及指向任何模块的链接。
HTML 生成器模式: -w 选项对于 pydoc 可以归档的任何文档都能生成 HTML 文档页面。这些页面与您在 Web服务器模式下可能会浏览到的页面本质上是一回事,但页面是静态的,可以进行存档、传输等等。
清单 7:最小的 setup.py安装脚本
#!/usr/bin/env python
"""Setup script for the sample #1 module distribution:
single top-level pure Python module, named explicitly
in 'py_modules'."""
from distutils.core import setup
setup (# Distribution meta-data
name = "sample",
version = "1.0",
description = "Distutils sample distribution #1",
# Description of modules and packages in the distribution
py_modules = ['sample'],
)
这里真正的工作是由导入的 distutils 实现,特别是由 setup() 函数来实现。基本上, setup() 函数采用一组包含一列需要安装的东西(除 py_modules 外还可能有 packages 或ext_modules 或其它东西)的已命名的变量。
distutils 的魔力在于 创建模块分发包时利用安装时使用的完全相同的 setup.py 文件。一旦您 ― 模块开发者 ― 创建了一个setup.py 脚本(也可能是‘setup.cfg’或其它扩展名)指定了需要安装的东西,创建分发包所要做的全部事情就是(下面的一步或几步):