Python — 以反射方式初始化 class

Python — Init class reflectively

我正在 Python 中创建命令系统。我有一个模块 vkcommands,它有一个 class 来处理来自聊天的命令(这是一个聊天机器人),在它里面,我还有 class VKCommand 属性如下nameusagemin_rank 等。然后我有模块 vkcmds 和实现这些命令的子模块:

...
vkcommands.py
vkcmds
    |- __init__.py  # empty
    |- add_group.py
    |- another_cmd.py
    |- ...

命令的实现(例如add_group)如下所示:

import ranks
import vkcommands
from vkcommands import VKCommand


class AddGroup(VKCommand):
    def __init__(self, kristy):
        VKCommand.__init__(self, kristy,
                           label='create',
                           # ... (other attributes)
                           min_rank=ranks.Rank.USER)

    def execute(self, chat, peer, sender, args=None, attachments=None):
        # implementation (called from vkcommands.py)

当用户在聊天中发送消息时,命令管理器会对其进行分析并查看已注册的 commands 列表,以查看这是一条普通消息还是机器人命令。目前我像这样手动注册 commands 列表中的所有命令:

class VKCommandsManager:
    def __init__(self, kristy):
        from vkcmds import (
            add_group,
            next_class
        )

        self.kristy = kristy
        self.commands = (
            add_group.AddGroup(kristy),
            next_class.NextClass(kristy)
        )

现在我希望使用反射自动注册所有命令。 在 Java 中,我将遍历我的所有 classes命令包,每个反射 getConstructor,调用它来检索 VKCommand 对象,并将其添加到命令列表中。

如何在 Python 中执行此操作? 同样,我需要的是:

  1. 遍历模块(文件夹)中的所有子模块vkcmds/
  2. 对于每个子模块,检查是否有一些 class X 扩展到 VKCommand 内部;
  3. 如果 (2) 是 true,则使用一个参数调用 class 的构造函数(保证 所有命令的构造函数只有一个已知类型的参数(我的机器人的主要class));
  4. 将在 (3) 中构造的对象 (? extends VKCommand) 添加到 commands 列表中,我稍后可以对其进行迭代。

我相信您可以将文件夹中的所有命令组成一个数组,然后遍历它们并实例化对象。

__init__.py

all_commands = [AddGroup, AnotherCmd, ...]

像这样实例化它们:

objects = [Cmd(arg1, arg2, ...) for Cmd in all_commands]

编辑: 您还可以使用您所说的获取文件夹中所有 class 个名称的方法来检索 class 个名称。

使用此文件结构:

- Project
   ├─ commands
   |   ├─ base.py
   |   ├─ baz.py
   |   └─ foo_bar.py
   |
   └─ main.py

以及 commands 目录文件中的以下内容:

  • base.py

    class VKCommand:
        """ We will inherit from this class if we want to include the class in commands.  """
    
  • baz.py

    from commands.base import VKCommand
    
    class Baz(VKCommand):
        pass
    
    
    def baz():
        """ Random function we do not want to retrieve.  
    
  • foo_bar.py

    from .base import VKCommand
    
    
    class Foo(VKCommand):
        """ We only want to retrieve this command.  """
        pass
    
    
    class Bar:
        """ We want to ignore this class.  """
        pass
    
    
    def fizz():
        """  Random function we do not want to retrieve. """
    

我们可以使用以下代码直接检索 class 个实例和名称:

  • main.py

    """
      Dynamically load all commands located in submodules.
      This file is assumed to be at most 1 level higher than the
      specified folder.
    """
    
    import pyclbr
    import glob
    import os
    
    def filter_class(classes):
        inherit_from = 'VKCommand'
        classes = {name: info for name, info in classes.items() if inherit_from in info.super}
        return classes
    
    # Locate all submodules and classes that it contains without importing it.
    folder = 'commands'  # `vkcmds`.
    submodules = dict()
    absolute_search_path = os.path.join(os.path.dirname(__file__), folder, '*.py')
    for path in glob.glob(absolute_search_path):
        submodule_name = os.path.basename(path)[:-3]
        all_classes = pyclbr.readmodule(f"commands.{submodule_name}")
        command_classes = filter_class(all_classes)
        if command_classes:
            submodules[submodule_name] = command_classes
    
    # import the class and store an instance of the class into the command list
    class_instances = dict()
    for submodule_name, class_names in submodules.items():
        module = __import__(f"{folder}.{submodule_name}")
        submodule = getattr(module, submodule_name)
        for class_name in class_names:
            class_instance = getattr(submodule, class_name)
            class_instances[class_name] = class_instance
    
    print(class_instances)
    

说明

解决方案有两个。它首先找到所有具有 class 的子模块,这些子模块继承自 VKCommand 并且位于文件夹“commands”中。这导致以下输出包含必须分别导入和实例化的模块和 class:

{'baz': {'Baz': <pyclbr.Class object at 0x000002BF886357F0>}, 'foo_bar': {'Foo': <pyclbr.Class object at 0x000002BF88660668>}}

代码的第二部分在 运行 时导入了正确的模块和 class 名称。变量 class_instance 包含 class 名称和对 class 的引用,可用于实例化它。最终输出将是:

{'Baz': <class 'commands.baz.Baz'>, 'Foo': <class 'commands.foo_bar.Foo'>}

重要提示:

  1. 该代码仅在导入比字典深 1 层的模块时有效。如果要递归使用它,则必须找到 并使用正确的(完整的)相对导入路径更新 pyclbr.readmodule__import__

  2. 只有包含继承自 VKCommand 的 class 的模块才会被加载。所有其他模块导入,必须手动导入。