lazy_import.py 1.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Lazy import utility."""

import importlib


class LazyImport:
    """Lazy import Python moduels, only import when modules are used."""
    def __init__(self, name, attr=None, callback=None):
        """Init lazy import class.

        Args:
            name (str): Python module name.
            attr (str, optional): Function or class name in the module. Defaults to None.
            callback (callable, optional): Callback function. Defaults to None.
        """
        self._module = None
        self._name = name
        self._attr = attr
        self._callback = callback

    def _import(self):
        """Import the needed module when it is used."""
        if self._module is None:
            self._module = importlib.import_module(self._name)
            if self._attr is not None:
                self._module = getattr(self._module, self._attr)
            if self._callback is not None:
                self._callback()

    def __getattr__(self, item):
        """Override __getattr__.

        Args:
            item (str): Attribute name.

        Returns:
            Any: Attribute value.
        """
        self._import()
        return getattr(self._module, item)

    def __dir__(self):
        """Override __dir__.

        Returns:
            List[str]: The list of attributes.
        """
        self._import()
        return dir(self._module)
53
54
55
56
57
58
59
60
61
62
63
64
65

    def __call__(self, *args, **kwargs):
        """Override __call__.

        Args:
            *args (list): Arguments.
            **kwargs (dict): Keyword arguments.

        Returns:
            Any: The return value of the function.
        """
        self._import()
        return self._module(*args, **kwargs)