繁体   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