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.

skcriteria.utils.ondemand_import.is_package(obj)[source]

Check if the object is a package.

Parameters:

obj (object) – The object to check.

Returns:

True if the object is a package, False otherwise.

Return type:

bool

class skcriteria.utils.ondemand_import.OnDemandImporter(package_name: str, package: ModuleType)[source]

Bases: object

Enhanced 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_name: str
package: ModuleType
property package_context

Get the package’s context dictionary.

Returns:

Dictionary of the package’s variables and modules.

Return type:

dict

property package_path

Get the package’s search path.

Returns:

List of directories where the package’s modules can be found.

Return type:

list

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.

list_available_modules()[source]

List all available modules in the package.

This method combines the already imported modules with the modules available on disk that have not yet been imported.

Returns:

Sorted list of all available module names in the package.

Return type:

list

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