Python文檔產生工具pydoc使用介紹

來源:互聯網
上載者:User
在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'


執行結果:

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.