Skip to content

Commit f04d253

Browse files
committed
use lazy-loader
1 parent 40c3ec2 commit f04d253

7 files changed

Lines changed: 175 additions & 128 deletions

File tree

lumispy/__init__.py

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,85 +17,6 @@
1717
# along with LumiSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
1818

1919

20-
import importlib
20+
import lazy_loader
2121

22-
from importlib.metadata import version
23-
from pathlib import Path
24-
from typing import Any
25-
26-
27-
# rresolve version
28-
__version__ = version("lumispy")
29-
30-
# For development version, `setuptools_scm` will be used at build time
31-
# to get the dev version, in case of missing vcs information (git archive,
32-
# shallow repository), the fallback version defined in pyproject.toml will
33-
# be used
34-
35-
# If we have an editable installed from a git repository try to use
36-
# `setuptools_scm` to find a more accurate version:
37-
# `importlib.metadata` will provide the version at installation
38-
# time and for editable version this may be different
39-
40-
# we only do that if we have enough git history, e.g. not shallow checkout
41-
_root = Path(__file__).resolve().parents[1]
42-
if (_root / ".git").exists() and not (_root / ".git/shallow").exists():
43-
try:
44-
# setuptools_scm may not be installed
45-
from setuptools_scm import get_version
46-
47-
__version__ = get_version(_root)
48-
except ImportError: # pragma: no cover
49-
# setuptools_scm not install, we keep the existing __version__
50-
pass
51-
52-
53-
__all__ = [
54-
"__version__",
55-
"components",
56-
"signals",
57-
"utils",
58-
]
59-
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-
100-
def __dir__():
101-
return sorted(list(__all__) + list(_lazy_attributes.keys()))
22+
__getattr__, __dir__, __all__ = lazy_loader.attach_stub(__name__, __file__)

lumispy/__init__.pyi

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2019-2025 The LumiSpy developers
3+
#
4+
# This file is part of LumiSpy.
5+
#
6+
# LumiSpy is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the license, or
9+
# (at your option) any later version.
10+
#
11+
# LumiSpy is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with LumiSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
18+
19+
20+
from lumispy import (
21+
components,
22+
signals,
23+
utils,
24+
)
25+
from lumispy_version import __version__
26+
from lumispy.utils.axes import nm2eV, eV2nm, nm2invcm, invcm2nm, join_spectra
27+
from lumispy.utils.io import to_array, savetxt
28+
from lumispy.utils import crop_edges
29+
30+
__all__ = [
31+
"__version__",
32+
"components",
33+
"signals",
34+
"utils",
35+
"nm2eV",
36+
"eV2nm",
37+
"nm2invcm",
38+
"invcm2nm",
39+
"join_spectra",
40+
"to_array",
41+
"savetxt",
42+
"crop_edges",
43+
]

lumispy/_version.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2019-2025 The LumiSpy developers
3+
#
4+
# This file is part of LumiSpy.
5+
#
6+
# LumiSpy is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the license, or
9+
# (at your option) any later version.
10+
#
11+
# LumiSpy is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with LumiSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
18+
19+
20+
import functools
21+
from importlib.metadata import version
22+
from pathlib import Path
23+
24+
25+
@functools.cache
26+
def _get_version():
27+
version_ = version("hyperspy")
28+
# For development version, `setuptools_scm` will be used at build time
29+
# to get the dev version, in case of missing vcs information (git archive,
30+
# shallow repository), the fallback version defined in pyproject.toml will
31+
# be used
32+
33+
# if we have a editable install from a git repository try to use
34+
# `setuptools_scm` to find a more accurate version:
35+
# `importlib.metadata` will provide the version at installation
36+
# time and for editable version this may be different
37+
38+
# we only do that if we have enough git history, e.g. not shallow checkout
39+
_root = Path(__file__).resolve().parents[1]
40+
if (_root / ".git").exists() and not (_root / ".git/shallow").exists():
41+
try:
42+
# setuptools_scm may not be installed
43+
from setuptools_scm import get_version
44+
45+
version_ = get_version(_root)
46+
except ImportError: # pragma: no cover
47+
# setuptools_scm not install, we keep the existing __version__
48+
pass
49+
50+
return version_
51+
52+
53+
__version__ = _get_version()

lumispy/hyperspy_extension.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ signals:
2525
signal_dimension: 1
2626
dtype: real
2727
lazy: False
28-
module: lumispy.signals.luminescence_spectrum
28+
module: lumispy.signals
2929
LazyLumiSpectrum:
3030
signal_type: Luminescence
3131
signal_type_aliases:
@@ -34,7 +34,7 @@ signals:
3434
signal_dimension: 1
3535
dtype: real
3636
lazy: True
37-
module: lumispy.signals.luminescence_spectrum
37+
module: lumispy.signals
3838

3939
CLSpectrum:
4040
signal_type: CL
@@ -44,7 +44,7 @@ signals:
4444
signal_dimension: 1
4545
dtype: real
4646
lazy: False
47-
module: lumispy.signals.cl_spectrum
47+
module: lumispy.signals
4848
LazyCLSpectrum:
4949
signal_type: CL
5050
signal_type_aliases:
@@ -53,7 +53,7 @@ signals:
5353
signal_dimension: 1
5454
dtype: real
5555
lazy: True
56-
module: lumispy.signals.cl_spectrum
56+
module: lumispy.signals
5757

5858
ELSpectrum:
5959
signal_type: EL
@@ -63,7 +63,7 @@ signals:
6363
signal_dimension: 1
6464
dtype: real
6565
lazy: False
66-
module: lumispy.signals.el_spectrum
66+
module: lumispy.signals
6767
LazyELSpectrum:
6868
signal_type: EL
6969
signal_type_aliases:
@@ -72,7 +72,7 @@ signals:
7272
signal_dimension: 1
7373
dtype: real
7474
lazy: True
75-
module: lumispy.signals.el_spectrum
75+
module: lumispy.signals
7676

7777
PLSpectrum:
7878
signal_type: PL
@@ -82,7 +82,7 @@ signals:
8282
signal_dimension: 1
8383
dtype: real
8484
lazy: False
85-
module: lumispy.signals.pl_spectrum
85+
module: lumispy.signals
8686
LazyPLSpectrum:
8787
signal_type: PL
8888
signal_type_aliases:
@@ -91,7 +91,7 @@ signals:
9191
signal_dimension: 1
9292
dtype: real
9393
lazy: True
94-
module: lumispy.signals.pl_spectrum
94+
module: lumispy.signals
9595

9696
CLSEMSpectrum:
9797
signal_type: CL_SEM
@@ -101,7 +101,7 @@ signals:
101101
signal_dimension: 1
102102
dtype: real
103103
lazy: False
104-
module: lumispy.signals.cl_spectrum
104+
module: lumispy.signals
105105
LazyCLSEMSpectrum:
106106
signal_type: CL_SEM
107107
signal_type_aliases:
@@ -110,7 +110,7 @@ signals:
110110
signal_dimension: 1
111111
dtype: real
112112
lazy: True
113-
module: lumispy.signals.cl_spectrum
113+
module: lumispy.signals
114114

115115
CLSTEMSpectrum:
116116
signal_type: CL_STEM
@@ -120,7 +120,7 @@ signals:
120120
signal_dimension: 1
121121
dtype: real
122122
lazy: False
123-
module: lumispy.signals.cl_spectrum
123+
module: lumispy.signals
124124
LazyCLSTEMSpectrum:
125125
signal_type: CL_STEM
126126
signal_type_aliases:
@@ -129,7 +129,7 @@ signals:
129129
signal_dimension: 1
130130
dtype: real
131131
lazy: True
132-
module: lumispy.signals.cl_spectrum
132+
module: lumispy.signals
133133

134134
LumiTransient:
135135
signal_type: Transient
@@ -140,7 +140,7 @@ signals:
140140
signal_dimension: 1
141141
dtype: real
142142
lazy: False
143-
module: lumispy.signals.luminescence_transient
143+
module: lumispy.signals
144144
LazyLumiTransient:
145145
signal_type: Transient
146146
signal_type_aliases:
@@ -150,7 +150,7 @@ signals:
150150
signal_dimension: 1
151151
dtype: real
152152
lazy: True
153-
module: lumispy.signals.luminescence_transient
153+
module: lumispy.signals
154154

155155
TransientSpectrumCasting: # allows casting to either Luminescence or Transient when dimensionality is reduced
156156
signal_type: TransientSpectrum
@@ -174,7 +174,7 @@ signals:
174174
signal_dimension: 2
175175
dtype: real
176176
lazy: False
177-
module: lumispy.signals.luminescence_transientspec
177+
module: lumispy.signals
178178
LazyLumiTransientSpectrum:
179179
signal_type: TransientSpectrum
180180
signal_type_aliases:
@@ -185,4 +185,4 @@ signals:
185185
signal_dimension: 2
186186
dtype: real
187187
lazy: True
188-
module: lumispy.signals.luminescence_transientspec
188+
module: lumispy.signals

lumispy/signals/__init__.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,11 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with LumiSpy. If not, see <https://www.gnu.org/licenses/#GPL>.
1818

19-
from .luminescence_spectrum import LumiSpectrum
20-
from .lazy_luminescence_spectrum import LazyLumiSpectrum
21-
from .cl_spectrum import CLSpectrum, CLSEMSpectrum, CLSTEMSpectrum
22-
from .lazy_cl_spectrum import LazyCLSpectrum, LazyCLSEMSpectrum, LazyCLSTEMSpectrum
23-
from .pl_spectrum import PLSpectrum
24-
from .lazy_pl_spectrum import LazyPLSpectrum
25-
from .el_spectrum import ELSpectrum
26-
from .lazy_el_spectrum import LazyELSpectrum
27-
from .luminescence_transient import LumiTransient
28-
from .lazy_luminescence_transient import LazyLumiTransient
29-
from .luminescence_transientspec import LumiTransientSpectrum
30-
from .lazy_luminescence_transientspec import LazyLumiTransientSpectrum
19+
"""
20+
Modules containing the eXSpy signals and their lazy counterparts.
3121
22+
"""
3223

33-
__all__ = [
34-
"LumiSpectrum",
35-
"LazyLumiSpectrum",
36-
"CLSpectrum",
37-
"LazyCLSpectrum",
38-
"CLSEMSpectrum",
39-
"LazyCLSEMSpectrum",
40-
"CLSTEMSpectrum",
41-
"LazyCLSTEMSpectrum",
42-
"PLSpectrum",
43-
"LazyPLSpectrum",
44-
"ELSpectrum",
45-
"LazyELSpectrum",
46-
"LumiTransient",
47-
"LazyLumiTransient",
48-
"LumiTransientSpectrum",
49-
"LazyLumiTransientSpectrum",
50-
]
24+
import lazy_loader
25+
26+
__getattr__, __dir__, __all__ = lazy_loader.attach_stub(__name__, __file__)

0 commit comments

Comments
 (0)