[英]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源文件并获取有关它们的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.