LazyLoader: Modules#
A LazyLoader for modules.
Example
The following example demonstrates how to use the attach_module function to lazily load submodules of a package.
>>> from typing import TYPE_CHECKING
>>>
>>> if TYPE_CHECKING:
>>> from tad_libcint.interface.symmetry import s1 as s1
>>> from tad_libcint.interface.symmetry import s4 as s4
>>> else:
>>> import tad_libcint.lazyloader as _lazy
>>>
>>> __getattr__, __dir__, __all__ = _lazy.attach_module(
>>> __name__,
>>> ["s1", "s4"],
>>> )
>>>
>>> del _lazy
>>>
>>> del TYPE_CHECKING
- tad_libcint.lazyloader.module.attach_module(package_name, submodules)[source]#
Lazily loads submodules of a given package, providing a way to access them on demand.
This function is intended to be used in a package’s __init__.py file to allow lazy loading of its submodules. It returns a tuple containing two callables (__getattr__ and __dir__) and a list of submodule names (__all__). __getattr__ is used to load a submodule when it’s accessed for the first time, while __dir__ lists the available submodules.
- Parameters:
package_name (str) – The name of the package for which submodules are to be lazily loaded.
submodules (Sequence[str]) – A sequence of strings representing the names of the submodules to be lazily loaded.
- Returns:
A tuple containing: - A __getattr__ function loading a submodule when it’s accessed. - A __dir__ function returning a list of all lazily loaded submodules. - A list of strings (__all__) containing the names of the submodules.
- Return type:
tuple[Callable[[str], Any], Callable[[], list[str]], list[str]]
- Raises:
AttributeError – Raised when an attempt is made to access a submodule that is not listed in the submodules parameter.
Example
>>> __getattr__, __dir__, __all__ = attach_module(__name__, ["sub1", "sub2"])