lazy_loader
util.lazy_loader
모듈이나 객체를 실제 사용할 때까지 import를 지연시키는 클래스를 정의합니다.
_LazyModule
Bases: ModuleType
Source code in SaigeToolkit/util/lazy_loader.py
_load
__getattr__
_LazyClassMeta
Bases: type
Source code in SaigeToolkit/util/lazy_loader.py
_load
__call__
__getattr__
_resolve_package
Source code in SaigeToolkit/util/lazy_loader.py
lazy_module
lazy_module(name_as: str, /, import_from: Optional[str] = None, name: Optional[str] = None, *, globals: Optional[Dict[str, Any]] = None)
A wrapper for modules that delays the import until it is needed. After the module is imported, it is stored in the namespace.
Parameters:
-
name_as(str) –The name of the module in the namespace.
-
import_from(Optional[str], default:None) –The base package of the module.
-
name(Optional[str], default:None) –The name of the module to import. Defaults to the value of
name_as. -
globals(dict, default:None) –The current namespace (pass
globals()here).
Usage
# import numpy as np
np = lazy_module("np", name="numpy", globals=globals())
print(np) # <lazy module 'numpy'>
np.random.rand(5) # `numpy` is imported here
print(np) # <module 'numpy' from '...'>
# import torch
torch = lazy_module("torch", globals=globals())
# from torchvision.models import resnet
resnet = lazy_module("resnet", "torchvision.models", globals=globals())
# from torch.nn import functional as F
F = lazy_module("F", "torch.nn", "functional", globals=globals())
# from .engine import api
api = lazy_module("api", ".engine", globals=globals())
Note
The name_as parameter should match the name of the variable in the namespace.
Source code in SaigeToolkit/util/lazy_loader.py
lazy_class
lazy_class(name_as: str, /, import_from: str, name: Optional[str] = None, *, globals: Optional[Dict[str, Any]] = None)
A wrapper for classes that delays the import until it is needed. After the class is imported, it is stored in the namespace.
Parameters:
-
name_as(str) –The name of the class in the namespace.
-
import_from(str) –The module path of the class.
-
name(Optional[str], default:None) –The name of the class to import. Defaults to the value of
name_as. -
globals(dict, default:None) –The current namespace (pass
globals()here).
Usage
# from torch.nn import Linear
Linear = lazy_class("Linear", "torch.nn", globals=globals())
print(Linear) # <lazy class 'torch.nn.Linear'> (cannot resolve the actual path)
linear = Linear(3, 4) # `Linear` is imported here
print(linear) # Linear(in_features=3, out_features=4, bias=True)
print(Linear) # <class 'torch.nn.modules.linear.Linear'>
# from torchvision.models.resnet import ResNet as _ResNet
_ResNet = lazy_module("_ResNet", "torchvision.models.resnet", "ResNet", globals=globals())
# from .engine.api import Trainer
Trainer = lazy_class("Trainer", ".engine.api", globals=globals())
trainer = Trainer.build(...)
Note
The name_as parameter should match the name of the variable in the namespace.
Warning
The only safe way to use this is class attribute access or instantiation.
Do not use this with class-level methods like isinstance or issubclass.