|
17 | 17 | # along with LumiSpy. If not, see <https://www.gnu.org/licenses/#GPL>. |
18 | 18 |
|
19 | 19 |
|
| 20 | +import importlib |
| 21 | + |
20 | 22 | from importlib.metadata import version |
21 | 23 | from pathlib import Path |
22 | | - |
23 | | -from lumispy.utils.axes import nm2eV, eV2nm, nm2invcm, invcm2nm, join_spectra |
24 | | -from lumispy.utils.io import to_array, savetxt |
25 | | - |
26 | | -from lumispy import signals, components, utils |
27 | | - |
28 | | -from lumispy.utils import crop_edges |
| 24 | +from typing import Any |
29 | 25 |
|
30 | 26 |
|
| 27 | +# rresolve version |
31 | 28 | __version__ = version("lumispy") |
32 | 29 |
|
33 | 30 | # For development version, `setuptools_scm` will be used at build time |
|
61 | 58 | ] |
62 | 59 |
|
63 | 60 |
|
| 61 | +# Map exported names that will be resolved lazily |
| 62 | +_lazy_modules = { |
| 63 | + "signals": "lumispy.signals", |
| 64 | + "components": "lumispy.components", |
| 65 | + "utils": "lumispy.utils", |
| 66 | +} |
| 67 | + |
| 68 | +# Map top-level utility names to their submodule and attribute name |
| 69 | +_lazy_attributes = { |
| 70 | + # name: (module, attribute) |
| 71 | + "nm2eV": ("lumispy.utils.axes", "nm2eV"), |
| 72 | + "eV2nm": ("lumispy.utils.axes", "eV2nm"), |
| 73 | + "nm2invcm": ("lumispy.utils.axes", "nm2invcm"), |
| 74 | + "invcm2nm": ("lumispy.utils.axes", "invcm2nm"), |
| 75 | + "join_spectra": ("lumispy.utils.axes", "join_spectra"), |
| 76 | + "to_array": ("lumispy.utils.io", "to_array"), |
| 77 | + "savetxt": ("lumispy.utils.io", "savetxt"), |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +def __getattr__(name: str) -> Any: |
| 82 | + """Lazy-load subpackages and selected attributes on demand.""" |
| 83 | + # Lazy subpackages |
| 84 | + if name in _lazy_modules: |
| 85 | + mod = importlib.import_module(_lazy_modules[name]) |
| 86 | + globals()[name] = mod |
| 87 | + return mod |
| 88 | + |
| 89 | + # Lazy attributes (forward from submodules) |
| 90 | + if name in _lazy_attributes: |
| 91 | + mod_name, attr = _lazy_attributes[name] |
| 92 | + mod = importlib.import_module(mod_name) |
| 93 | + val = getattr(mod, attr) |
| 94 | + globals()[name] = val |
| 95 | + return val |
| 96 | + |
| 97 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
| 98 | + |
| 99 | + |
64 | 100 | def __dir__(): |
65 | | - return sorted(__all__) |
| 101 | + return sorted(list(__all__) + list(_lazy_attributes.keys())) |
0 commit comments