簡體   English   中英

嵌入式python解釋器,生成自動完成的存根源代碼

[英]Embedded python interpreter, generate stub source code for autocompletion

我有一個嵌入python並將其內部對象模型公開為python對象/類的應用程序。

出於自動完成/腳本編寫的目的,我想提取內部對象模型的模擬,其中包含doc標簽,結構,函數等,因此我可以將其用作IDE自動完成的庫源。

有人知道一個庫,或者有一些代碼片段可用於將這些類轉儲到源代碼嗎?

使用dir()globals()函數獲取已定義內容的列表。 然后,使用檢查模塊過濾和瀏覽類

示例toto.py:

class Example(object):
    """class docstring"""

    def hello(self):
        """hello doctring"""
        pass

示例browser.py:

import inspect
import toto

for name, value in inspect.getmembers(toto):
    # First ignore python defined variables
    if name.startswith('__'):
        continue

    # Now only browse classes
    if not inspect.isclass(value):
        continue
    print "Found class %s with doctring \"%s\"" % (name, inspect.getdoc(value))

    # Only browse functions in the current class
    for sub_name, sub_value in inspect.getmembers(value):
        if not inspect.ismethod(sub_value):
            continue
        print "  Found method %s with docstring \"%s\"" % \
            (sub_name, inspect.getdoc(sub_value))

python Browse.py:

Found class Example with doctring "class docstring"
  Found method hello with docstring "hello doctring"

另外,這並不能真正回答您的問題,但是,如果您正在編寫某種IDE,則還可以使用ast模塊來解析python源文件並獲取有關它們的信息。

Python數據結構是可變的(請參閱什么是猴子補丁? ),因此僅提取模擬是不夠的。 相反,您可以使用dir()內置函數動態地向解釋器詢問可能的自動完成字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM