python模块的延迟加载模块

lazy-import的Python项目详细描述


Build Status

lazy_import提供一组加载模块的函数,以及 属性,以一种懒散的方式。这允许将ImportErrors延迟到 实际模块使用时间。同样,实际的模块初始化只会发生 使用时。在重载情况下使用可选依赖项时,这很有用 时间和/或足迹,因为只有当模块实际 使用。

对同一会话中运行的其他代码的影响最小lazy_import 功能是在不使用导入挂钩的情况下实现的。

lazy_import与python≥2.7或≥3.4兼容。

示例:延迟模块加载

importlazy_importnp=lazy_import.lazy_module("numpy")# np is now available in the namespace and is listed in sys.modules under#  the 'numpy' key:importsyssys.modules['numpy']# The module is present as "Lazily-loaded module numpy"# Subsequent imports of the same module return the lazy version present#  in sys.modulesimportnumpy# At this point numpy and np point to the same lazy module.# This is true for any import of 'numpy', even if from other modules!# Accessing attributes causes the full loading of the module ...np.pi# ... and the module is changed in place. np and numpy are now#  "<module 'numpy' from '/usr/local/lib/python/site-packages/numpy/__init__.py'>"# Lazy-importing a module that's already fully loaded returns the full#  module instead (even if it was loaded elsewhere in the current session)#  because there's no point in being lazy in this case:os=lazy_import.lazy_module("os")# "<module 'os' from '/usr/lib/python/os.py'>"

在上面的代码中可以看到 lazy_import.lazy_module("numpy")在 会话范围sys.modules注册表。这意味着any后续导入 在同一个会话中的numpy,而模块仍未完全加载, 将获得numpy模块的懒惰版本。这也会发生 在调用lazy_module

importlazy_importnp=lazy_import.lazy_module("numpy")importmodule_that_uses_numpy# This module will get a lazy module upon# 'import numpy'

通常这是可以的,因为惰性模块的行为与真实的 东西一旦装满。不过,记录下这一点可能是一个很好的做法 你在懒洋洋地导入一些模块,这样用户就会收到警告。

进一步的用法是延迟ImportErrors

importlazy_import# The following succeeds even when asking for a module that's not availablemissing=lazy_import.lazy_module("missing_module")missing.some_attr# This causes the full loading of the module, which now fails."ImportError: __main__ attempted to use a functionality that requires modulemissing_module,butitcouldn't be loaded. Please install missing_module and retry."

子模块也可以工作:

importlazy_importmod=lazy_import.lazy_module("some.sub.module")# mod now points to the some.sub.module lazy module#  equivalent to "from some.sub import module as mod"# Alternatively the returned reference can be made to point to the#  base module:some=lazy_import.lazy_module("some.sub.module",level="base")# This is equivalent to "import some.sub.module" in that only the base#  module's name is added to the namespace. All submodules must be accessed#  via that:some.sub# Returns lazy module 'some.sub' without triggering full loading.some.sub.attr# Triggers full loading of 'some' and 'some.sub'.some.sub.module.function()# Triggers loading also of 'some.sub.module'.

最后,如果要标记某些模块和子模块,则包导入 像往常一样懒惰,从根本上导入它们就很简单 初始水平。其他文件可以正常导入所有模块,并且 那些已经被加载为lazy in.py的文件将保持如下状态:

# in __init__.py:importlazy_importlazy_import.lazy_module("numpy")lazy_import.lazy_module("scipy.stats")# then, in any other file in the package just use the imports normally:importrequests# This one is not lazy.importnumpy# This one is lazy, as long as no other code caused its#  loading in the meantime.importscipy# This one is also lazy. It was lazily loaded as part of the#  lazy loading of scipy.stats.importscipy.stats# Also lazy.importscipy.linalg# Uh-oh, we didn't lazily import the 'linalg' submodule#  earlier, and importing it like this here will cause#  both scipy and scipy.linalg (but not scipy.stats) to#  immediately become fully loaded.

示例:延迟可调用加载

模拟from some.module import function语法lazy_module 提供lazy_callable。它返回一个包装函数。只有在 它是否会触发目标模块的加载和 目标可调用(函数、类等)。

importlazy_importfn=lazy_import.lazy_callable("numpy.arange")# 'numpy' is now in sys.modules and is 'Lazily-loaded module numpy'fn(10)# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

lazy_callable仅当要调用目标可调用时才有用:

importlazy_importcl=lazy_import.lazy_callable("numpy.ndarray")# a classobj=cl([1,2])# This works OK (and also triggers the loading of numpy)classMySubclass(cl):# This fails because cl is just a wrapper,pass#  not an actual class.

安装

pip install lazy_import

或者,要包含运行回归测试所需的依赖项:

pip install lazy_import[test]

测试

lazy_module模块附带一系列测试。如果安装时使用 测试依赖项(见上文),只需运行

importlazy_import.test_lazylazy_import.test_lazy.run()# This will automatically parallelize over the available number of cores

或者,可以从命令行运行测试:

pytest -n 4 --boxed -v --pyargs lazy_import
# (replace '4' with the number of cores in your machine, or set to 1 if
#  you'd rather test in serial)

测试只依赖于^{tt16}$^{tt17}$,因此如果没有安装 它们沿着lazy_import(如Installation所述)只需运行

pip install pytest pytest-xdist

注意,pytest-xdist甚至对于串行测试也是必需的,因为它 --boxed功能。

许可证

lazy_import在gpl v3下发布。它基于 ^来自PEAK包的{a5}模块。两者的许可证 lazy_import和峰值包包含在LICENSE文件中。这个 此处复制了相应的许可声明:

lazy_import — a module to allow lazy importing of python modules

Copyright (C) 2017-2018 Manuel Nuno Melo

lazy_import is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

lazy_import is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with lazy_import. If not, see <http://www.gnu.org/licenses/>.

峰值importing代码是

Copyright (C) 1996-2004 by Phillip J. Eby and Tyler C. Sarna. All rights reserved. This software may be used under the same terms as Zope or Python. THERE ARE ABSOLUTELY NO WARRANTIES OF ANY KIND. Code quality varies between modules, from “beta” to “experimental pre-alpha”. :)

与从峰值importing延迟加载相关的代码包含在 lazy_import,以多种方式修改。这些在 CHANGELOG文件,共lazy_import。变化主要涉及python 3 兼容性,允许自定义行为的扩展,并添加 功能(可调用对象的延迟导入)。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaJSonarray不会从SeekBar读取double   使用另一个类从Java中的2D数组打印用户输入   java ClassNotFoundException的原因   spring调用两个方法以返回Java中的不同页面   httpurlconnection Java禁止的代码错误,但浏览器错误(2)   java画布矩阵转换   java:在另一个java映射中使用“Map”作为值   java“未找到用于解密的证书”(Apache CXF,WSSecurity)   java如何查看JTable中选择的行   java在没有xmlwrappers的情况下重复xml元素序列集   java将垂直直方图打印到控制台   java Spring JDBCTemplate:构造不带特殊字符的JSON   java PayPal RestApi获取用户信息