Python文檔生成工具pydoc

在Python中有很多很好的工具來(lái)生成字符串文檔(docstring),比如說(shuō): epydoc、doxygen、sphinx,但始終覺(jué)得pydoc還是不錯(cuò)的工具,用法非常簡(jiǎn)單,功能也算不錯(cuò),本文主要介紹pydoc.

pydoc是Python自帶的模塊,主要用于從python模塊中自動(dòng)生成文檔,這些文檔可以基于文本呈現(xiàn)的、也可以生成WEB 頁(yè)面的,還可以在服務(wù)器上以瀏覽器的方式呈現(xiàn)!

【用法】

Windows下:

D:\>python -m pydoc <modulename>   # 比如說(shuō): python -m pydoc math

-m參數(shù):Python以腳本的方法運(yùn)行模塊

Linux/Unix下:

 $ pydoc <modulename>               # 比如說(shuō): pydoc 

【幫助】

$ pydoc -h
pydoc - the Python documentation tool 
 
pydoc <name> ...
    Show text documentation on something.  <name> 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 <name> 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 <keyword>
    Search for a keyword in the synopsis lines of all available modules.
 
 
pydoc -p <port>
    Start an HTTP server on the given port on the local machine.
 
 
pydoc -w <name> ...
    Write out the HTML documentation for a module to a file in the current
    directory.  If <name> contains a '/', it is treated as a filename; if
    it names a directory, documentation is written for all the contents.

【參數(shù) -p】在本地機(jī)器上,按照給定的端口啟動(dòng)HTTP,

D:>python -m pydoc -p 1234 #比如說(shuō): 端口為1234

pydoc server ready at http://localhost:1234/

pydoc server stopped

在IE中輸入:http://localhost:1234/,效果如圖:
image.png
【參數(shù) -k】在所有可用的模塊中按關(guān)鍵字搜索
$ 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
【參數(shù) -w】將指定模塊的文本字符串生成HTML格式

比如說(shuō),在Window下面,執(zhí)行下面命令:

D:\Learn\Python>python -m pydoc math -w math.html  # math是模塊名,-w:寫(xiě)

那么在D:\Learn\Python目錄下會(huì)生成math.html文件,顯示如下:


image.png

因?yàn)槭亲詭У哪K,所以右上角顯示(built-in)字樣

【例子】自寫(xiě)的模塊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 <x>
    '''
    return x * x
執(zhí)行命令:
D:\Learn\Python> python -m pydoc my_doc
執(zhí)行結(jié)果:
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 <x>
        
DATA
    __authors__ = 'Alice & Fred'
    __license__ = 'Copyright...'
    __version__ = 'version 1.10'
 
VERSION
    version 1.10
執(zhí)行命令:
d:\Learn\Python>python -m pydoc -w my_doc my_doc.html
wrote my_doc.html
no Python documentation found for 'my_doc.html'
執(zhí)行結(jié)果:
image.png

官方文檔:

https://docs.python.org/2/library/pydoc.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,226評(píng)論 0 3
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,934評(píng)論 0 13
  • 今晚在上班,天氣真的好熱啊,晚上吃完飯女兒給我打電話,問(wèn)我媽媽你今天忙嗎?累不累???聽(tīng)到女兒?jiǎn)栁?,心里特感?dòng),我告...
    海浪花_2642閱讀 156評(píng)論 0 1
  • 深夜兩點(diǎn)無(wú)眠。 應(yīng)該說(shuō)是昨天了,因小兒的百日宴,家里來(lái)了好多的客人,我媽媽也過(guò)來(lái)了,和婆婆聊天,...
    海河小學(xué)三三班張逍遙閱讀 248評(píng)論 0 0

友情鏈接更多精彩內(nèi)容