Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7cbd355

Browse filesBrowse files
committed
Add an optional_imports module.
This is a stand-alone module to the following: * Don’t import things until we need them * Keep all the information centralized * No possibility of circular imports by using this module
1 parent 62263b7 commit 7cbd355
Copy full SHA for 7cbd355

File tree

Expand file treeCollapse file tree

1 file changed

+41
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+41
-0
lines changed

‎plotly/optional_imports.py

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Stand-alone module to provide information about whether optional deps exist.
3+
4+
"""
5+
from __future__ import absolute_import
6+
7+
import importlib
8+
9+
__all__ = ['get_module']
10+
11+
import_cache = {}
12+
13+
14+
def get_module(name, raise_exc=False, msg=None):
15+
"""
16+
Return the module if it's available. Raise if desired.
17+
18+
A wrapper around importlib.import_module that only allows absolute import.
19+
20+
:param (str) name: Dot-separated module path. E.g., 'scipy.stats'.
21+
:param (bool) raise_exc: Raise ImportError if not found?
22+
:param (str|None) msg: Optional message to raise with ImportError.
23+
:raises: (ImportError) If `raise_exception` is True, this can happen.
24+
:return: (module|None) If import succeeds, the module will be returned.
25+
26+
"""
27+
if name in import_cache:
28+
return import_cache[name]
29+
30+
try:
31+
module = importlib.import_module(name) # only allow absolute imports
32+
except ImportError:
33+
if raise_exc:
34+
if msg is None:
35+
raise # just raise the current import error
36+
else:
37+
raise ImportError(msg)
38+
else:
39+
import_cache[name] = module
40+
41+
return import_cache.get(name, None) # Don't store the None!

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.