File tree Expand file tree Collapse file tree 3 files changed +37
-10
lines changed
Expand file tree Collapse file tree 3 files changed +37
-10
lines changed Original file line number Diff line number Diff line change 1- from . import (
2- definitions ,
3- builder ,
4- config ,
5- functions ,
6- constraints ,
7- tcp ,
8- ocp ,
9- )
1+ """Top-level package for OpEn with lazy submodule imports.
2+
3+ This module defers importing heavy subpackages to attribute access
4+ to avoid circular import problems during package initialization.
5+
6+ Lazy submodule imports defer the loading of Python modules and their
7+ attributes until they are first accessed, reducing startup time and
8+ memory usage. This is achieved using PEP 562 (__getattr__ and __dir__)
9+ to intercept attribute access and load the underlying code only when
10+ necessary.
11+ """
12+
13+ from importlib import import_module
1014
1115__all__ = [
1216 "definitions" ,
1721 "tcp" ,
1822 "ocp" ,
1923]
24+
25+
26+ def __getattr__ (name ):
27+ """Lazily import submodules on attribute access.
28+
29+ Example: accessing ``opengen.builder`` will import
30+ ``opengen.builder`` and cache it on the package module.
31+
32+ This defers importing heavy subpackages until they're actually used
33+ (lazy imports), reducing startup cost and helping avoid import-time
34+ circular dependencies.
35+ """
36+ if name in __all__ :
37+ module = import_module (f"{ __name__ } .{ name } " )
38+ globals ()[name ] = module
39+ return module
40+ raise AttributeError (f"module { __name__ !r} has no attribute { name !r} " )
41+
42+
43+ def __dir__ ():
44+ return sorted (list (__all__ ) + list (globals ().keys ()))
Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
13import subprocess
24import shutil
35import yaml
Original file line number Diff line number Diff line change @@ -623,7 +623,7 @@ def test_dimension_sphere2(self):
623623 # -----------------------------------------------------------------------
624624
625625 def test_zero_dimension (self ):
626- z = og .opengen . constraints .Zero ()
626+ z = og .constraints .Zero ()
627627 self .assertIsNone (z .dimension ())
628628
629629if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments