meerschaum.utils.packages.lazy_loader
A LazyLoader class.
1#! /usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# ============================================================================== 17 18"""A LazyLoader class.""" 19 20from __future__ import absolute_import 21from __future__ import division 22from __future__ import print_function 23 24from meerschaum.utils.typing import Optional 25import importlib, types 26 27class LazyLoader(types.ModuleType): 28 """Lazily import a module, mainly to avoid pulling in large dependencies. 29 30 `contrib`, and `ffmpeg` are examples of modules that are large and not always 31 needed, and this allows them to only be loaded when they are used. 32 """ 33 34 # The lint error here is incorrect. 35 def __init__( 36 self, 37 local_name: str, 38 parent_module_globals, 39 name: str, 40 **kw 41 ): 42 self._local_name = local_name 43 self._parent_module_globals = parent_module_globals 44 kw['lazy'] = False 45 self._attempt_import_kw = kw 46 self._module = None 47 super(LazyLoader, self).__init__(name) 48 49 def _load(self): 50 """Load the module and insert it into the parent's globals.""" 51 if self._module is not None: 52 return self._module 53 54 from meerschaum.utils.packages import attempt_import 55 self._module = attempt_import(self.__name__, **self._attempt_import_kw) 56 self._parent_module_globals[self._local_name] = self._module 57 58 # Update this object's dict so that if someone keeps a reference to the 59 # LazyLoader, lookups are efficient (__getattr__ is only called on lookups 60 # that fail). 61 self.__dict__.update(self._module.__dict__) 62 63 return self._module 64 65 def __getattr__(self, item): 66 module = self._load() if self._module is None else self._module 67 return getattr(module, item) 68 69 def __dir__(self): 70 module = self._load() if self._module is None else self._module 71 return dir(module)
class
LazyLoader(builtins.module):
28class LazyLoader(types.ModuleType): 29 """Lazily import a module, mainly to avoid pulling in large dependencies. 30 31 `contrib`, and `ffmpeg` are examples of modules that are large and not always 32 needed, and this allows them to only be loaded when they are used. 33 """ 34 35 # The lint error here is incorrect. 36 def __init__( 37 self, 38 local_name: str, 39 parent_module_globals, 40 name: str, 41 **kw 42 ): 43 self._local_name = local_name 44 self._parent_module_globals = parent_module_globals 45 kw['lazy'] = False 46 self._attempt_import_kw = kw 47 self._module = None 48 super(LazyLoader, self).__init__(name) 49 50 def _load(self): 51 """Load the module and insert it into the parent's globals.""" 52 if self._module is not None: 53 return self._module 54 55 from meerschaum.utils.packages import attempt_import 56 self._module = attempt_import(self.__name__, **self._attempt_import_kw) 57 self._parent_module_globals[self._local_name] = self._module 58 59 # Update this object's dict so that if someone keeps a reference to the 60 # LazyLoader, lookups are efficient (__getattr__ is only called on lookups 61 # that fail). 62 self.__dict__.update(self._module.__dict__) 63 64 return self._module 65 66 def __getattr__(self, item): 67 module = self._load() if self._module is None else self._module 68 return getattr(module, item) 69 70 def __dir__(self): 71 module = self._load() if self._module is None else self._module 72 return dir(module)
Lazily import a module, mainly to avoid pulling in large dependencies.
contrib, and ffmpeg are examples of modules that are large and not always
needed, and this allows them to only be loaded when they are used.
LazyLoader(local_name: str, parent_module_globals, name: str, **kw)
36 def __init__( 37 self, 38 local_name: str, 39 parent_module_globals, 40 name: str, 41 **kw 42 ): 43 self._local_name = local_name 44 self._parent_module_globals = parent_module_globals 45 kw['lazy'] = False 46 self._attempt_import_kw = kw 47 self._module = None 48 super(LazyLoader, self).__init__(name)