在python中加载延迟模块

在python中加载延迟模块,第1张

概述我正在尝试组建一个系统来处理延迟加载未明确存在的模块.基本上我有一个http服务器,其中包含许多我不知道的端点,我想以编程方式提供导入.这些模块都有统一的方法签名,它们不会提前存在. import lazy.route as testimport lazy.fake as test2test('Does this exist?') # This sends a post request. 我正在尝试组建一个系统来处理延迟加载未明确存在的模块.基本上我有一个http服务器,其中包含许多我不知道的端点,我想以编程方式提供导入.这些模块都有统一的方法签名,它们不会提前存在.

import lazy.route as testimport lazy.fake as test2test('Does this exist?')  # This sends a post request.test2("This doesn't exist.")  # Also sends a post request

我可以使用统一的装饰器处理这些导入所需的所有逻辑,我在python中找不到任何“装饰”导入的方法,或者实际上以任何编程方式与它们进行交互.

有任何人对此有经验吗?我一直在寻找,最接近我发现的是ast模块,在我目前的理解下,这将导致我目前的一种非常糟糕的Hacky实现(比如查找所有import语句和手动覆盖导入功能)

不是寻找讲义,只是开始查看的一段python代码库,或者是一个类似的东西的例子.

解决方法 我在Google搜索中有点聪明,设法找到一个专门解决这个问题的PEP,它恰好相对未知,可能是因为合理用途的子集非常狭窄.

我找到了一个很好的示例代码,展示了新的sys.Meta_path实现.我已经在下面发布了它,以获取有关如何动态引导import语句的信息.

import sysclass 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

总结

以上是内存溢出为你收集整理的在python中加载延迟模块全部内容,希望文章能够帮你解决在python中加载延迟模块所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存