Skip to content

Commit dad3ee6

Browse files
committed
Lazy-load ThinLayerPysces and ThinLayerCopasi imports
Refactor __init__.py to only import ThinLayerPysces and ThinLayerCopasi when they are explicitly accessed. This avoids unnecessary ImportErrors and improves module import performance by deferring dependency checks until needed.
1 parent 79be825 commit dad3ee6

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

pyenzyme/thinlayers/__init__.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
from .base import BaseThinLayer
22

3-
try:
4-
from .psyces import ThinLayerPysces
5-
except ImportError:
6-
pass
7-
8-
try:
9-
from .basico import ThinLayerCopasi
10-
except ImportError:
11-
pass
3+
# Only import and show warnings if the modules are explicitly requested
4+
ThinLayerPysces = None
5+
ThinLayerCopasi = None
6+
7+
8+
def _get_pysces():
9+
global ThinLayerPysces
10+
if ThinLayerPysces is None:
11+
try:
12+
from .psyces import ThinLayerPysces as _ThinLayerPysces
13+
14+
ThinLayerPysces = _ThinLayerPysces
15+
except ImportError as e:
16+
raise ImportError(
17+
f"ThinLayerPysces is not available because of missing dependencies: {e}"
18+
)
19+
return ThinLayerPysces
20+
21+
22+
def _get_copasi():
23+
global ThinLayerCopasi
24+
if ThinLayerCopasi is None:
25+
try:
26+
from .basico import ThinLayerCopasi as _ThinLayerCopasi
27+
28+
ThinLayerCopasi = _ThinLayerCopasi
29+
except ImportError as e:
30+
raise ImportError(
31+
f"ThinLayerCopasi is not available because of missing dependencies: {e}"
32+
)
33+
return ThinLayerCopasi
34+
35+
36+
def __getattr__(name):
37+
if name == "ThinLayerPysces":
38+
return _get_pysces()
39+
elif name == "ThinLayerCopasi":
40+
return _get_copasi()
41+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
42+
1243

1344
__all__ = ["BaseThinLayer", "ThinLayerPysces", "ThinLayerCopasi"]

0 commit comments

Comments
 (0)