lazy_loader
Project description
lazy-loader
makes it easy to load subpackages and functions on demand.
Motivation
- Allow subpackages to be made visible to users without incurring import costs.
- Allow external libraries to be imported only when used, improving import times.
For a more detailed discussion, see the SPEC.
Installation
pip install -U lazy-loader
Usage
Lazily load subpackages
Consider the __init__.py
from scikit-image:
subpackages = [
...,
'filters',
...
]
import lazy_loader as lazy
__getattr__, __dir__, _ = lazy.attach(__name__, subpackages)
You can now do:
import skimage as ski
ski.filters.gaussian(...)
The filters
subpackages will only be loaded once accessed.
Lazily load subpackages and functions
Consider skimage/filters/__init__.py
:
from ..util import lazy
__getattr__, __dir__, __all__ = lazy.attach(
__name__,
submodules=['rank']
submod_attrs={
'_gaussian': ['gaussian', 'difference_of_gaussians'],
'edges': ['sobel', 'scharr', 'prewitt', 'roberts',
'laplace', 'farid']
}
)
The above is equivalent to:
from . import rank
from ._gaussian import gaussian, difference_of_gaussians
from .edges import (sobel, scharr, prewitt, roberts,
laplace, farid)
Except that all subpackages (such as rank
) and functions (such as sobel
) are loaded upon access.
Early failure
With lazy loading, missing imports no longer fail upon loading the
library. During development and testing, you can set the EAGER_IMPORT
environment variable to disable lazy loading.
External libraries
The lazy.attach
function discussed above is used to set up package
internal imports.
Use lazy.load
to lazily import external libraries:
linalg = lazy.load('scipy.linalg') # `linalg` will only be loaded when accessed
You can also ask lazy.load
to raise import errors as soon as it is called:
linalg = lazy.load('scipy.linalg', error_on_import=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for lazy_loader-0.1rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd0519f7d20c8f10b39b3f8536e9ae4c0a9b691e9a01cb14ef06c6344f2c53ef |
|
MD5 | 01efe6ef4eabf4bc526adb74cfb2bde4 |
|
BLAKE2b-256 | b2272bf5640d88a9be48b6421c9cba7fbd6a6a5380892a3e2f85124fe023ebee |