首頁 後端開發 Python教學 Python文档生成工具pydoc使用介绍

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

Jun 06, 2016 am 11:18 AM
python 文檔產生工具

在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' 


执行结果:


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

模板化的優點和缺點有哪些? 模板化的優點和缺點有哪些? May 08, 2024 pm 03:51 PM

模板化的優點和缺點有哪些?

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

怎麼下載deepseek 小米 怎麼下載deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎麼下載deepseek 小米

分享幾個.NET開源的AI和LLM相關專案框架 分享幾個.NET開源的AI和LLM相關專案框架 May 06, 2024 pm 04:43 PM

分享幾個.NET開源的AI和LLM相關專案框架

deepseek怎麼問他 deepseek怎麼問他 Feb 19, 2025 pm 04:42 PM

deepseek怎麼問他

evaluate函數怎麼保存 evaluate函數怎麼保存 May 07, 2024 am 01:09 AM

evaluate函數怎麼保存

NET40是什麼軟體 NET40是什麼軟體 May 10, 2024 am 01:12 AM

NET40是什麼軟體

deepseek該怎麼搜索 deepseek該怎麼搜索 Feb 19, 2025 pm 05:18 PM

deepseek該怎麼搜索

See all articles