- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathimporters.py
50 lines (39 loc) · 1.59 KB
/
importers.py
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
importimportlib
defrelative_import(parent_name, rel_modules=(), rel_classes=()):
"""
Helper function to import submodules lazily in Python 3.7+
Parameters
----------
rel_modules: list of str
list of submodules to import, of the form .submodule
rel_classes: list of str
list of submodule classes/variables to import, of the form ._submodule.Foo
Returns
-------
tuple
Tuple that should be assigned to __all__, __getattr__ in the caller
"""
module_names= {rel_module.split(".")[-1]: rel_moduleforrel_moduleinrel_modules}
class_names= {rel_path.split(".")[-1]: rel_pathforrel_pathinrel_classes}
def__getattr__(import_name):
# In Python 3.7+, lazy import submodules
# Check for submodule
ifimport_nameinmodule_names:
rel_import=module_names[import_name]
returnimportlib.import_module(rel_import, parent_name)
# Check for submodule class
ifimport_nameinclass_names:
rel_path_parts=class_names[import_name].split(".")
rel_module=".".join(rel_path_parts[:-1])
class_name=import_name
class_module=importlib.import_module(rel_module, parent_name)
returngetattr(class_module, class_name)
raiseAttributeError(
"module {__name__!r} has no attribute {name!r}".format(
name=import_name, __name__=parent_name
)
)
__all__=list(module_names) +list(class_names)
def__dir__():
return__all__
return__all__, __getattr__, __dir__