GenericLoader
What it is
- A small Pydantic model that dynamically imports a Python module, retrieves a named callable (class or function), and instantiates/calls it using a configuration dict as keyword arguments.
Public API
class GenericLoader(pydantic.BaseModel)- Fields:
python_module: str | None- fully qualified module path to import.module_callable: str | None- attribute name in the module to call.custom_config: Dict[str, Any] | None- keyword arguments passed to the callable.
- Methods:
load() -> Any- Imports
python_moduleviaimportlib.import_module. - Retrieves the callable via
getattr(module, module_callable). - Calls it as
callable_obj(**custom_config)and returns the result.
- Imports
- Fields:
Configuration/Dependencies
- Dependencies:
- Standard library:
importlib - Third-party:
pydantic.BaseModel
- Standard library:
- Required inputs (enforced at runtime via
assertinload()):python_modulemust be non-Nonemodule_callablemust be non-Nonecustom_configmust be non-None
Usage
from naas_abi_core.engine.engine_configuration.EngineConfiguration_GenericLoader import GenericLoader
# Example: call a standard library callable (datetime.datetime)
loader = GenericLoader(
python_module="datetime",
module_callable="datetime",
custom_config={"year": 2024, "month": 1, "day": 2},
)
dt = loader.load()
print(dt) # 2024-01-02 00:00:00
Caveats
load()usesassertstatements; missing fields raiseAssertionError.- The target attribute must exist on the imported module; otherwise
getattrraisesAttributeError. custom_configmust match the callable’s expected keyword arguments; otherwise aTypeErrormay be raised.- Only supports callables invoked with keyword arguments (
callable_obj(**custom_config)).