pyclbr --- Python 模組瀏覽器支援

原始碼:Lib/pyclbr.py


The pyclbr module provides limited information about the functions, classes, and methods defined in a Python-coded module. The information is sufficient to implement a module browser. The information is extracted from the Python source code rather than by importing the module, so this module is safe to use with untrusted code. This restriction makes it impossible to use this module with modules not implemented in Python, including all standard and optional extension modules.

pyclbr.readmodule(module, path=None)

回傳模組層級的類別名稱與類別描述器對應的字典。如果可能的話,會包含引入基底類別的描述器。參數 module 是包含要讀取的模組名稱的字串;它可以是套件中的模組名稱。如果給定,path 是以 sys.path 為前綴的目錄路徑序列,用來定位模組原始碼。

此函式為原始介面,僅保留用於向後相容性。它回傳的會是 readmodule_ex 回傳結果的篩選後版本。

pyclbr.readmodule_ex(module, path=None)

回傳一個以字典為基礎的樹狀結構,包含在模組中以 defclass 陳述式定義的每個函式和類別的函式或類別描述器。回傳的字典會將模組層級的函式和類別名稱對映到它們的描述器。巢狀物件會輸入其父物件的子字典。與 readmodule 一樣,module 會指定要讀取的模組,而 path 則會預先加入 sys.path。如果要讀取的模組是一個套件,回傳的字典有一個 key '__path__',其值是一個包含套件搜尋路徑的串列。

在 3.7 版被加入: 巢狀定義(nested definitions)的描述器。這些描述器可透過新的子屬性來存取。每個描述器都有一個新的父屬性。

這些函式所回傳的描述器是 Function 和 Class 類別的實例。使用者不需要建立這些類別的實例。

函式物件

class pyclbr.Function

Class Function 實例描述由 def 陳述式定義的函式。它們具有以下屬性:

file

定義函式的檔案名稱。

module

定義所述函式的模組名稱。

name

函式的名稱。

lineno

檔案中定義開始的行號。

parent

對於頂層函式(top-level functions)為 None。對於巢狀函式,則使用父函式。

在 3.7 版被加入.

children

一個 dictionary 將名稱對應到巢狀函式和類別的描述器。

在 3.7 版被加入.

is_async

對於使用 async 前綴定義的函式是 True,否則是 False

在 3.10 版被加入.

類別物件

class pyclbr.Class

Class Class 實例描述由 class 陳述式定義的類別。它們具有與 Functions 相同的屬性,以及另外兩個屬性。

file

定義類別的檔案名稱。

module

定義所述類別的模組名稱。

name

類別的名稱。

lineno

檔案中定義開始的行號。

parent

對於頂層類別為 None。對於巢狀類別,則使用父類別。

在 3.7 版被加入.

children

將名稱對應到巢狀函式和類別的描述器的字典。

在 3.7 版被加入.

super

描述被描述類別的直接基底類別的 Class 物件串列。被命名為超類別(superclasses)但無法被 readmodule_ex() 發現的類別會以類別名稱的字串形式列出,而不是 Class 物件。

methods

一個 dictionary 方法名稱對應到行號。這可以從較新的 children 字典衍生出來,但為了向後相容性而保留。