Skip to content

Commit

Permalink
adds ignore_errors to import_module_globals; misc. cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom O'Hara committed Oct 4, 2023
1 parent 214454f commit aa6bf7d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mezcla/ipython_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,29 @@ def grep_obj_methods(obj, pattern, flags=None):
return list(m for m in dir(obj) if re.search(pattern, m, flags))


def import_module_globals(module_name, include_private=False, include_dunder=False, globals_dict=None):
def import_module_globals(module_name, include_private=False, include_dunder=False, globals_dict=None, ignore_errors=None):
"""Import MODULE_NAME optionally with INCLUDE_PRIVATE and INCLUDE_DUNDER and setting GLOBALS_DICT"""
# note: intended to support reloading modules imported in ipython via 'from module import *'
# TODO3: find a cleaner way of doing this (e.g., via import support)
# EX: import_module_globals("mezcla.misc_utils", globals_dict=builtins.globals()); VALUE_EPSILON => 1e-3
# pylint: disable=eval-used,exec-used
if globals_dict is None:
globals_dict = builtins.globals()
if ignore_errors is None:
ignore_errors = False

# Get list of modules attributes (e.g., variables)
module_attrs = []
try:
exec(f"import {module_name}")
import_command = f"import {module_name}"
exec(import_command)
module = eval(module_name)
module_attrs = dir(module)
except:
system.print_exception_info("import_module attribute check")
if not ignore_errors:
system.print_exception_info(import_command)
else:
debug.trace_exception(5, import_command)

# Import each individually
for var in module_attrs:
Expand All @@ -124,6 +130,7 @@ def import_module_globals(module_name, include_private=False, include_dunder=Fal
globals_dict[var] = eval(f"{module_name}.{var}")
except:
debug.trace_exception(4, import_desc)
return

#-------------------------------------------------------------------------------
# Helper class
Expand Down

0 comments on commit aa6bf7d

Please sign in to comment.