1.4.0
New parameter is added to ModuleLoader
constructor.
ModuleLoader(
base_path: Optional[str] = None,
strict: bool = False # New!!
)
If you decorate some classes or functions with @load_config
, the loader will load them. However, initialized strict=True
, the loader denies multiple loading as below.
- pkg/validator_a.py
from autoload import load_config
# This will be loaded because of name's rule.
class ValidatorA:
def validate(self):
print("validateA!!")
# Anything goes.
@load_config(order=2)
class Foo:
pass
- main.py
from autoload import ModuleLoader
from autoload.exception import LoaderStrictModeError
loader = ModuleLoader()
# return ValidatorA and Foo class objects.
classes = loader.load_classes("pkg")
# ModuleLoader strictly try to load a class or function object
# per a Python module on a basis of its name.
strict_loader = ModuleLoader(strict=True)
try:
classes = strict_loader.load_classes("pkg")
except LoaderStrictModeError as e:
print(e)
# -> Loader can only load a 'ValidatorA' class in validator_a module.
# -> Please check 'Foo' in validator_a module.