|
1 | 1 | # Lazy imports to avoid slow startup |
2 | 2 | # Only import essential components that are needed for basic functionality |
3 | 3 |
|
| 4 | +from importlib.metadata import PackageNotFoundError, version |
| 5 | + |
4 | 6 | # Import core modules without heavy dependencies |
5 | 7 | from .config import search_spaces |
6 | | -from importlib.metadata import PackageNotFoundError, version |
7 | 8 |
|
8 | 9 | # Use the distribution name as published on PyPI (may differ from import name in some projects) |
9 | 10 | _DISTRIBUTION_NAME = "flexynesis" |
|
13 | 14 | except PackageNotFoundError: |
14 | 15 | # Happens in some dev scenarios before the package is installed |
15 | 16 | __version__ = "0+unknown" |
16 | | - |
| 17 | + |
| 18 | + |
17 | 19 | class LazyModule: |
18 | 20 | """Lazy module that only imports when accessed.""" |
19 | | - |
| 21 | + |
20 | 22 | def __init__(self, module_name): |
21 | 23 | self._module_name = module_name |
22 | 24 | self._module = None |
23 | 25 | self._import_error = None |
24 | | - |
| 26 | + |
25 | 27 | def _import_module(self): |
26 | 28 | if self._module is None and self._import_error is None: |
27 | 29 | try: |
28 | 30 | import importlib |
29 | | - self._module = importlib.import_module(f'.{self._module_name}', package=__name__) |
| 31 | + |
| 32 | + self._module = importlib.import_module( |
| 33 | + f".{self._module_name}", package=__name__ |
| 34 | + ) |
30 | 35 | except ImportError as e: |
31 | 36 | self._import_error = e |
32 | | - raise ImportError(f"Failed to import {self._module_name} module: {e}. " |
33 | | - f"This usually means some dependencies are missing. " |
34 | | - f"Try installing required packages or check your environment.") |
| 37 | + raise ImportError( |
| 38 | + f"Failed to import {self._module_name} module: {e}. " |
| 39 | + f"This usually means some dependencies are missing. " |
| 40 | + f"Try installing required packages or check your environment." |
| 41 | + ) |
35 | 42 | elif self._import_error is not None: |
36 | 43 | raise self._import_error |
37 | 44 | return self._module |
38 | | - |
| 45 | + |
39 | 46 | def __getattr__(self, name): |
40 | 47 | module = self._import_module() |
41 | 48 | return getattr(module, name) |
42 | | - |
| 49 | + |
43 | 50 | def __dir__(self): |
44 | 51 | if self._module is None: |
45 | 52 | return [] |
46 | 53 | module = self._import_module() |
47 | 54 | return dir(module) |
48 | | - |
| 55 | + |
49 | 56 | def __repr__(self): |
50 | 57 | if self._module is None: |
51 | 58 | return f"<LazyModule '{self._module_name}' (not yet imported)>" |
52 | 59 | else: |
53 | 60 | return f"<LazyModule '{self._module_name}' (imported)>" |
54 | 61 |
|
| 62 | + |
55 | 63 | # Create lazy module proxies - these are NOT imported yet |
56 | | -modules = LazyModule('modules') |
57 | | -data = LazyModule('data') |
58 | | -main = LazyModule('main') |
59 | | -models = LazyModule('models') |
60 | | -feature_selection = LazyModule('feature_selection') |
61 | | -utils = LazyModule('utils') |
| 64 | +modules = LazyModule("modules") |
| 65 | +data = LazyModule("data") |
| 66 | +main = LazyModule("main") |
| 67 | +models = LazyModule("models") |
| 68 | +feature_selection = LazyModule("feature_selection") |
| 69 | +utils = LazyModule("utils") |
| 70 | + |
62 | 71 |
|
63 | 72 | # Import commonly used classes directly for easy access |
64 | 73 | # These will be imported lazily when first accessed |
65 | 74 | def _get_data_importer(): |
66 | 75 | """Lazy getter for DataImporter class.""" |
67 | 76 | return data.DataImporter |
68 | 77 |
|
| 78 | + |
69 | 79 | def _get_models(): |
70 | 80 | """Lazy getter for model classes.""" |
71 | 81 | return models |
72 | 82 |
|
| 83 | + |
73 | 84 | # Export all modules and commonly used classes |
74 | 85 | __all__ = [ |
75 | 86 | "search_spaces", |
76 | 87 | "modules", |
77 | | - "data", |
| 88 | + "data", |
78 | 89 | "main", |
79 | 90 | "models", |
80 | 91 | "feature_selection", |
81 | 92 | "utils", |
82 | | - "DataImporter" |
| 93 | + "DataImporter", |
83 | 94 | ] |
84 | | - |
|
0 commit comments