wip
This commit is contained in:
parent
78cc5ff0cd
commit
70f4a89e30
10 changed files with 564 additions and 182 deletions
63
learn_sql_model/optional.py
Normal file
63
learn_sql_model/optional.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import textwrap
|
||||
|
||||
|
||||
def _optional_import_(
|
||||
module: str,
|
||||
name: str = None,
|
||||
group: str = None,
|
||||
package="learn_sql_model",
|
||||
):
|
||||
"""
|
||||
lazily throws import errors only then the optional import is used, and
|
||||
includes a group install command for the user to install all dependencies
|
||||
for the requested feature.
|
||||
"""
|
||||
import importlib
|
||||
|
||||
try:
|
||||
module = importlib.import_module(module)
|
||||
return module if name is None else getattr(module, name)
|
||||
except ImportError as e:
|
||||
msg = textwrap.dedent(
|
||||
f"""
|
||||
"pip install '{package}[{group}]'" package to make use of this feature
|
||||
Alternatively "pip install '{package}[all]'" package to install all optional dependencies
|
||||
"""
|
||||
)
|
||||
import_error = e
|
||||
|
||||
class _failed_import:
|
||||
"""
|
||||
Lazily throw an import error. Errors should be thrown whether the
|
||||
user tries to call the module, get an attubute from the module, or
|
||||
getitem from the module.
|
||||
|
||||
"""
|
||||
|
||||
def _failed_import(self, *args):
|
||||
raise ImportError(msg) from import_error
|
||||
|
||||
def __call__(self, *args):
|
||||
"""
|
||||
Throw error if the user tries to call the module i.e
|
||||
_optional_import_('dummy')()
|
||||
"""
|
||||
self._failed_import(*args)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""
|
||||
Throw error if the user tries to get an attribute from the
|
||||
module i.e _optional_import_('dummy').dummy.
|
||||
"""
|
||||
if name == "_failed_import":
|
||||
return object.__getattribute__(self, name)
|
||||
self._failed_import()
|
||||
|
||||
def __getitem__(self, name):
|
||||
"""
|
||||
Throw error if the user tries to get an item from the module
|
||||
i.e _optional_import_('dummy')['dummy']
|
||||
"""
|
||||
self._failed_import()
|
||||
|
||||
return _failed_import()
|
||||
Loading…
Add table
Add a link
Reference in a new issue