-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathlazy_heavy_imports.py
More file actions
65 lines (50 loc) · 1.98 KB
/
Copy pathlazy_heavy_imports.py
File metadata and controls
65 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Lazy imports facade for heavy third-party dependencies.
This module provides a centralized facade that lazily imports heavy dependencies
only when accessed, significantly improving import performance.
The below mapping for lazy imports represents all third-party packages that, if used with
Data Designer, we strongly recommend using this lazy imports pattern to improve import performance.
Usage:
import data_designer.lazy_heavy_imports as lazy
df = lazy.pd.DataFrame(...)
arr = lazy.np.array([1, 2, 3])
Important:
Avoid `from data_designer.lazy_heavy_imports import pd`.
That import style resolves the attribute immediately and eagerly imports the heavy dependency.
"""
from __future__ import annotations
import importlib
# Mapping of lazy import names to their actual module paths
_LAZY_IMPORTS = {
"pd": "pandas",
"np": "numpy",
"pq": "pyarrow.parquet",
"pa": "pyarrow",
"faker": "faker",
"sqlfluff": "sqlfluff",
"httpx": "httpx",
"duckdb": "duckdb",
"nx": "networkx",
"scipy": "scipy",
"jsonschema": "jsonschema",
"PIL": "PIL",
"Image": "PIL.Image",
"tiktoken": "tiktoken",
}
def __getattr__(name: str) -> object:
"""Lazily import heavy third-party dependencies when accessed.
This allows fast imports of data_designer while deferring loading of heavy
libraries until they're actually needed.
"""
if name in _LAZY_IMPORTS:
module_name = _LAZY_IMPORTS[name]
module = importlib.import_module(module_name)
# Cache so subsequent accesses find a real attribute and skip __getattr__.
globals()[name] = module
return module
raise AttributeError(f"module 'data_designer.lazy_heavy_imports' has no attribute {name!r}")
def __dir__() -> list[str]:
"""Return list of available lazy imports."""
return list(_LAZY_IMPORTS.keys())