From 789e7f626e40d20cea7f1b526d798bec941344cf Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Fri, 23 Feb 2018 19:25:31 +0100 Subject: [PATCH] add lazy module importer We will need to issue deprecation warnings when using this with 'kwant.continuum', so we have to roll our own so we can hook into the attribute access. When we no longer need this feature we can replace this with a simpler implementation using 'importlib.util.LazyLoader'. --- kwant/_common.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/kwant/_common.py b/kwant/_common.py index 76e952cf..48c45eaa 100644 --- a/kwant/_common.py +++ b/kwant/_common.py @@ -6,10 +6,12 @@ # the file AUTHORS.rst at the top-level directory of this distribution and at # http://kwant-project.org/authors. +import sys import numpy as np import numbers import inspect import warnings +import importlib from contextlib import contextmanager __all__ = ['KwantDeprecationWarning', 'UserCodeError'] @@ -123,3 +125,26 @@ def get_parameters(func): takes_kwargs = any(i.kind is inspect.Parameter.VAR_KEYWORD for i in pars.values()) return required_params, default_params, takes_kwargs + + +class lazy_import: + def __init__(self, module, package='kwant', deprecation_warning=False): + if module.startswith('.') and not package: + raise ValueError('Cannot import a relative module without a package.') + self.__module = module + self.__package = package + self.__deprecation_warning = deprecation_warning + + def __getattr__(self, name): + if self.__deprecation_warning: + absolute_module = '.'.join((self.__package, self.__module)) + msg = ("Accessing {0} without an explicit import is deprecated. " + "Instead, explicitly 'import {0}'." + ).format('.'.join((self.__package, self.__module))) + warnings.warn(msg, KwantDeprecationWarning, stacklevel=2) + relative_module = '.' + self.__module + mod = importlib.import_module(relative_module, self.__package) + # Replace this _LazyModuleProxy with an actual module + package = sys.modules[self.__package] + setattr(package, self.__module, mod) + return getattr(mod, name) -- GitLab