使用Pydoc生成文檔

Python中自己帶有不少實用的工具,如pydoc。pydoc模塊主要用來從Python模塊中提取信息並生成文檔。html

使用方法

在Windows和Linux下的使用方法有些區別。python

Windows

python -m pydoc <modulename>linux

如:json

C:\>python -m pydoc module_test

NB:module_test是自定義的模塊,不要添加文件後綴。windows

Linux

pydoc <modulename>瀏覽器

如:ruby

$ pydoc module_test

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.

$

無參數 <name>

顯示文檔,<name>能夠是相查詢的任何東西。如關鍵字、函數、模塊、包等等。markdown

C:\>python -m pydoc print
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end: string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

C:\>python -m pydoc random
Help on module random:

NAME
    random - Random variable generators.

DESCRIPTION
        integers
        --------
               uniform within range

        sequences
        ---------
               pick random element
               pick random sample
               generate random permutation

        distributions on the real line:
        ------------------------------
               uniform
               triangular
               normal (Gaussian)
               lognormal
               negative exponential
               gamma
...

參數-k <keyword>

在可用模塊中按關鍵字搜索。app

C:\>python -m pydoc -k print
calendar - Calendar printing functions
email.quoprimime - Quoted-printable content transfer encoding per RFCs 2045-2047
.
encodings.quopri_codec - Codec for quoted-printable encoding.
json.tool - Command-line tool to validate and pretty-print JSON
lib2to3.fixes.fix_print - Fixer for print.
pprint - Support to pretty-print lists, tuples, & dictionaries recursively.
pstats - Class for printing reports on profiled python code.
quopri - Conversions to/from quoted-printable transport encoding as per RFC 1521
.
test.test_pprint
test.test_print
traceback - Extract, format and print information about Python stack traces.

參數-p <port>

在當前主機啓用HTTP服務,使用指定的端口。dom

C:\>python -m pydoc -p 53241
Server ready at http://localhost:53241/
Server commands: [b]rowser, [q]uit
server> b
server>

如:

pydoc

這時能夠點擊來跟蹤連接進行查看相應的文檔。

參數-w <name>

生成HTML文檔。

首先編寫一個模塊,此外爲class_test.py

""" A simple test. """

if __name__ == "__main__":
    print("hello")

而後執行以下:

C:\>python -m pydoc -w class_test
wrote class_test.html

此時在C盤根目錄下生成了class_test.html文件,就能夠在瀏覽器中進行查看了。

更多請參考Python的pydoc模塊

相關文章
相關標籤/搜索