You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lazy_imports.py 3.7 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """This module provides lazy import functionality to improve the import
  4. performance of nitime. For example, some parts of nitime leverage and import
  5. matplotlib, which is quite a big package, yet most of the nitime code does not
  6. depend on matplotlib. By lazily-loading a module, we defer the overhead of
  7. importing it until the first time it is actually used, thereby speeding up
  8. nitime imports.
  9. A generic :class:`LazyImport` class is implemented which takes the module name
  10. as a parameter, and acts as a proxy for that module, importing it only when
  11. the module is used, but effectively acting as the module in every other way
  12. (including inside IPython with respect to introspection and tab completion)
  13. with the *exception* of reload() - reloading a :class:`LazyImport` raises an
  14. :class:`ImportError`.
  15. Commonly used nitime lazy imports are also defined in :mod:`nitime.lazy`, so
  16. they can be reused throughout nitime.
  17. """
  18. import os
  19. import sys
  20. import types
  21. class LazyImport(types.ModuleType):
  22. """
  23. This class takes the module name as a parameter, and acts as a proxy for
  24. that module, importing it only when the module is used, but effectively
  25. acting as the module in every other way (including inside IPython with
  26. respect to introspection and tab completion) with the *exception* of
  27. reload()- reloading a :class:`LazyImport` raises an :class:`ImportError`.
  28. >>> mlab = LazyImport('matplotlib.mlab')
  29. No import happens on the above line, until we do something like call an
  30. ``mlab`` method or try to do tab completion or introspection on ``mlab``
  31. in IPython.
  32. >>> mlab
  33. <module 'matplotlib.mlab' will be lazily loaded>
  34. Now the :class:`LazyImport` will do an actual import, and call the dist
  35. function of the imported module.
  36. >>> mlab.dist(1969,2011)
  37. 42.0
  38. """
  39. def __getattribute__(self, x):
  40. # This method will be called only once, since we'll change
  41. # self.__class__ to LoadedLazyImport, and __getattribute__ will point
  42. # to module.__getattribute__
  43. name = object.__getattribute__(self, '__name__')
  44. __import__(name)
  45. # if name above is 'package.foo.bar', package is returned, the docs
  46. # recommend that in order to get back the full thing, that we import
  47. # and then lookup the full name is sys.modules, see:
  48. # http://docs.python.org/library/functions.html#__import__
  49. module = sys.modules[name]
  50. # Now that we've done the import, cutout the middleman and make self
  51. # act as the imported module
  52. class LoadedLazyImport(types.ModuleType):
  53. __getattribute__ = module.__getattribute__
  54. __repr__ = module.__repr__
  55. object.__setattr__(self, '__class__', LoadedLazyImport)
  56. # The next line will make "reload(l)" a silent no-op
  57. return module.__getattribute__(x)
  58. def __repr__(self):
  59. return "<module '%s' will be lazily loaded>" % object.__getattribute__(self, '__name__')
  60. if 'READTHEDOCS' in os.environ:
  61. lazy_doc = """
  62. WARNING: To get Sphinx documentation to build we disable
  63. LazyImports, which makes Sphinx incorrectly report this
  64. class as having a base class of object. In reality,
  65. :class:`LazyImport`'s base class is
  66. :class:`types.ModuleType`.
  67. """
  68. lazy_doc += LazyImport.__doc__
  69. class LazyImport(object):
  70. __doc__ = lazy_doc
  71. def __init__(self, x):
  72. __import__(x)
  73. self.module = sys.modules[x]
  74. def __getattr__(self, x):
  75. return self.module.__getattribute__(x)

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.