17.13. pyclbr — 类浏览器 | 开发者工具 |《python 3 标准库实例教程》| python 技术论坛-金年会app官方网

未匹配的标注

目的:实现适用于源代码编辑器的 api ,用于创建类浏览器。

pyclbr 可以扫描 python 源代码以查找类和独立函数。 使用 tokenize 收集有关类、方法和函数名称以及行号的信息,而不用导入代码。

本节中的示例使用以下源文件作为输入。

pyclbr_example.py

"""example source for pyclbr.
"""
class base:
    """this is the base class.
    """
    def method1(self):
        return
class sub1(base):
    """this is the first subclass.
    """
class sub2(base):
    """this is the second subclass.
    """
class mixin:
    """a mixin class.
    """
    def method2(self):
        return
class mixinuser(sub2, mixin):
    """overrides method1 and method2
    """
    def method1(self):
        return
    def method2(self):
        return
    def method3(self):
        return
def my_function():
    """stand-alone function.
    """
    return

扫描类

pyclbr 暴露了两个公开函数。 第一个是 readmodule() ,它接受模块的名称,因为参数返回类名到包含类源元数据的 class 对象的映射。

pyclbr_readmodule.py

import pyclbr
import os
from operator import itemgetter
def show_class(name, class_data):
    print('class:', name)
    filename = os.path.basename(class_data.file)
    print('  file: {0} [{1}]'.format(
        filename, class_data.lineno))
    show_super_classes(name, class_data)
    show_methods(name, class_data)
    print()
def show_methods(class_name, class_data):
    for name, lineno in sorted(class_data.methods.items(),
                               key=itemgetter(1)):
        print('  method: {0} [{1}]'.format(name, lineno))
def show_super_classes(name, class_data):
    super_class_names = []
    for super_class in class_data.super:
        if super_class == 'object':
            continue
        if isinstance(super_class, str):
            super_class_names.append(super_class)
        else:
            super_class_names.append(super_class.name)
    if super_class_names:
        print('  super classes:', super_class_names)
example_data = pyclbr.readmodule('pyclbr_example')
for name, class_data in sorted(example_data.items(),
                               key=lambda x: x[1].lineno):
    show_class(name, class_data)

该类的元数据包括定义它的文件和行号,以及超类的名称。 类的方法储存为方法名称和行号之间的映射。 输出显示了根据源文件中的行号按顺序列出的类和方法。

$ python3 pyclbr_readmodule.py
class: base
  file: pyclbr_example.py [11]
  method: method1 [15]
class: sub1
  file: pyclbr_example.py [19]
  super classes: ['base']
class: sub2
  file: pyclbr_example.py [24]
  super classes: ['base']
class: mixin
  file: pyclbr_example.py [29]
  method: method2 [33]
class: mixinuser
  file: pyclbr_example.py [37]
  super classes: ['sub2', 'mixin']
  method: method1 [41]
  method: method2 [44]
  method: method3 [47]

函数扫描

pyclbr 中的另一个公开函数是 readmodule_ex() 。 它执行 readmodule() 所做的一切,并将函数添加到结果集中。

pyclbr_readmodule_ex.py

import pyclbr
import os
from operator import itemgetter
example_data = pyclbr.readmodule_ex('pyclbr_example')
for name, data in sorted(example_data.items(),
                         key=lambda x: x[1].lineno):
    if isinstance(data, pyclbr.function):
        print('function: {0} [{1}]'.format(name, data.lineno))

每个 function 对象都具有与 class 对象非常相似的属性。

$ python3 pyclbr_readmodule_ex.py
function: my_function [51]

另请参阅

  •  -- inspect 模块可以发现有关类和函数的更多元数据,但需要导入代码。
  • tokenize -- tokenize 模块将 python 源代码解析为令牌。

本文章首发在 金年会app官方网 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系金年会app官方网。

原文地址:https://learnku.com/docs/pymotw/pyclbr-c...

译文地址:https://learnku.com/docs/pymotw/pyclbr-c...

上一篇 下一篇
讨论数量: 0



暂无话题~
网站地图