Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的延迟加载模块_Python_Module_Lazy Loading - Fatal编程技术网

python中的延迟加载模块

python中的延迟加载模块,python,module,lazy-loading,Python,Module,Lazy Loading,我正在尝试构建一个系统,该系统将处理不显式存在的模块的延迟加载。基本上,我有一个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.")

我正在尝试构建一个系统,该系统将处理不显式存在的模块的延迟加载。基本上,我有一个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
模块,在我目前的理解下,这将导致一种非常糟糕的黑客实现(比如查找所有导入语句和手动重写导入函数)


我不想看讲义,只想看一段python代码库,或者一个做过类似事情的人的例子。

我在谷歌搜索时有点聪明,并设法找到了一个专门解决这个问题的PEP,它只是相对未知,可能是因为它的合理用途非常有限

我发现了一段优秀的示例代码,展示了新的
sys.meta\u路径
实现。我在下面发布了它,以获取有关如何动态引导导入语句的信息

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()

完整的博文是

你们在找什么?@jornsharpe不,有点相反。我没有试图在其他地方使用导入,我只是试图覆盖现有的python导入操作,以接近投票结果:没有要求提供一个库,主要是这个功能在python中是否可行,因为我找不到任何文档。看起来你真的想从子类派生@polyvertex我不太想自己做,这部分很可行,更希望覆盖默认的导入实现