python中的懒加载模块

2024-04-25 10:14:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个系统来处理不显式存在的模块的延迟加载。基本上,我有一个http服务器,它有许多我事先不知道的端点,我想通过编程方式提供给导入。这些模块都有一个统一的方法签名,它们只是不可能提前存在。在

import lazy.route as test
import lazy.fake as test2

test('Does this exist?')  # This sends a post request.
test2("This doesn't exist.")  # Also sends a post request

我可以用一个统一的decorator处理这些导入所需的所有逻辑,只是找不到任何方法用python“装饰”导入,或者以任何编程方式与它们交互。在

有人有这方面的经验吗?我一直在四处寻找,找到的最接近的是ast模块,在我目前的理解下,这将导致我当前的实现非常糟糕(比如查找所有的import语句并手动重写import函数)

不需要讲义,只需要开始查看python代码库的一部分,或者某人做过类似事情的例子。在


Tags: 模块方法testimportrequest系统as编程
1条回答
网友
1楼 · 发布于 2024-04-25 10:14:07

我在谷歌上有点聪明,找到了一个专门解决这个问题的政治公众人物,它碰巧是相对未知的,可能是因为它的合理用途的子集相当狭窄。在

我发现了一段优秀的示例代码,展示了新的sys.meta_path实现。我把它贴在下面,了解如何动态引导导入语句。在

import sys


class VirtualModule(object):

   def hello(self):
      return 'Hello World!'


class CustomImporter(object):

   virtual_name = 'my_virtual_module'

   def find_module(self, fullname, path):
      """This method is called by Python if this class
         is on sys.path. fullname is the fully-qualified
         name of the module to look for, and path is either
         __path__ (for submodules and subpackages) or None (for
         a top-level module/package).

         Note that this method will be called every time an import
         statement is detected (or __import__ is called), before
         Python's built-in package/module-finding code kicks in."""

      if fullname ==  self.virtual_name:

         # As per PEP #302 (which implemented the sys.meta_path protocol),
         # if fullname is the name of a module/package that we want to
         # report as found, then we need to return a loader object.
         # In this simple example, that will just be self.

         return self

      # If we don't provide the requested module, return None, as per
      # PEP #302.

      return None

   def load_module(self, fullname):
      """This method is called by Python if CustomImporter.find_module
         does not return None. fullname is the fully-qualified name
         of the module/package that was requested."""

      if fullname != self.virtual_name:
         # Raise ImportError as per PEP #302 if the requested module/package
         # couldn't be loaded. This should never be reached in this
         # simple example, but it's included here for completeness. :)
         raise ImportError(fullname)

      # PEP#302 says to return the module if the loader object (i.e,
      # this class) successfully loaded the module.
      # Note that a regular class works just fine as a module.
      return VirtualModule()


if __name__ == '__main__':

   # Add our import hook to sys.meta_path
   sys.meta_path.append(CustomImporter())

   # Let's use our import hook
   import my_virtual_module
   print my_virtual_module.hello()

完整的博文是here

相关问题 更多 >