Skip to content
Snippets Groups Projects
Commit f95ed1a8 authored by Dr. Felix Tobias Schindler's avatar Dr. Felix Tobias Schindler
Browse files

[python] simplify guarded_import()

parent 08a756de
No related branches found
No related tags found
2 merge requests!20Update bindings,!11WIP: Update bindings
......@@ -26,18 +26,15 @@ from importlib import import_module
def guarded_import(globs, base_name, mod_name):
# see https://stackoverflow.com/questions/43059267/how-to-do-from-module-import-using-importlib
try:
mod = import_module('.{}'.format(mod_name), base_name)
if "__all__" in mod.__dict__:
names = mod.__dict__["__all__"]
else:
names = [x for x in mod.__dict__ if not x.startswith("_")]
# check the rest for duplicity
for nm in names:
if nm in globs:
raise ImportError(
f'{base_name}: not overwriting existing name \'{nm}\' when importing from \'{mod_name}\'!' )
# and finally import
globs.update({k: getattr(mod, k) for k in names})
except ImportError as e:
raise ImportError(f'{base_name}: could not import module \'{mod_name}\':\n\n{e}')
mod = import_module('.{}'.format(mod_name), base_name)
if "__all__" in mod.__dict__:
names = mod.__dict__["__all__"]
else:
names = [x for x in mod.__dict__ if not x.startswith("_")]
# check the rest for duplicity
for nm in names:
if nm in globs:
raise ImportError(
f'{base_name}: not overwriting existing name \'{nm}\' when importing from \'{mod_name}\'!' )
# and finally import
globs.update({k: getattr(mod, k) for k in names})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment