nd2py.utils.nn package#
- nd2py.utils.nn.setup_lazy_imports(module_name: str, import_mapping: Dict[str, Tuple[str, str]])[source]#
Set up lazy imports for a module’s
__init__.py.Returns
(__getattr__, __dir__, __all__)which should be assigned at the module level so thatfrom package import OptionalClassworks without importing the optional dependency until it is actually needed.- Parameters:
module_name – The
__name__of the calling module.import_mapping – A dict mapping attribute names to
(module_path, requires)tuples. module_path is a relative import path (e.g.".torch_calc") and requires is the optional-dependency group name (e.g."nn") shown in the error message when the dependency is missing.
Usage:
# __init__.py from .core import CoreClass from ..utils.lazy_loader import setup_lazy_imports if TYPE_CHECKING: from .optional import OptionalClass __getattr__, __dir__, __all__ = setup_lazy_imports(__name__, { "OptionalClass": (".optional", "nn"), })