skcriteria.utils.ondemand_import module
On-demand importer of modules.
The on-demand importer is a function that returns a callable object that imports the module when the object is called.
Notes
This ondemand importer is inspired on the one from scikit-learn, but adds a more power to introspection.
- class skcriteria.utils.ondemand_import.OnDemandImporter(package_name: str, package: ModuleType)[source]
Bases:
objectEnhanced on-demand importer for lazy loading of package modules.
This class implements a mechanism for lazy loading of modules within a package. It postpones the import of a module until it is explicitly requested, allowing for more efficient loading of large packages. Unlike simpler implementations, this version also provides directory listing capabilities.
- Parameters:
package_name (str) – The fully qualified name of the package.
package (types.ModuleType) – The package module object.
- Raises:
ValueError – If the provided package object is not actually a package.
Notes
This implementation uses a frozen dataclass to ensure immutability of the importer’s state.
- package: ModuleType
- property package_context
Get the package’s context dictionary.
- Returns:
Dictionary of the package’s variables and modules.
- Return type:
- property package_path
Get the package’s search path.
- Returns:
List of directories where the package’s modules can be found.
- Return type:
- import_or_get_attribute(name)[source]
Dynamically imports or retrieves a module as an attribute.
This function is the core of the lazy-loading mechanism. It either returns an already loaded module from cache or imports it when first requested, then adds it to the parent package namespace.
- Parameters:
name (str) – Module name to import or retrieve (without parent package prefix)
- Returns:
The cached or newly imported module or subpackage
- Return type:
module
- Raises:
AttributeError – If the module doesn’t exist or cannot be imported
Notes
The implementation:
First checks if the module exists in the package_context dictionary cache
Imports the module if not found in cache
Sets up recursive lazy-loading for any imported subpackages
Raises AttributeError specifically for Jedi compatibility
Jedi, the autocompletion engine used in Jupyter and other scientific environments, explores namespaces by calling __getattr__ and only ignores ImportError and AttributeError exceptions during this process. This implementation ensures compatibility with Jedi’s behavior.
- skcriteria.utils.ondemand_import.mk_ondemand_importer_for(package_name)[source]
Create an on-demand importer for a specific package.
This function creates and returns an instance of _OnDemandImporter for the specified package. The package must already be imported and available in sys.modules.
- Parameters:
package_name (str) – The fully qualified name of the package for which to create an importer.
- Returns:
An instance of _OnDemandImporter configured for the specified package.
- Return type:
_OnDemandImporter
Examples
>>> # In a package's __init__.py >>> importer = mk_ondemand_importer_for(__name__) >>> __getattr__ = importer.import_module >>> __dir__ = importer.list_available_modules