Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-T-G authored Jan 16, 2025
1 parent 283d05d commit 5fd88ac
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions autoimport/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def __repr__(self):

class lazy:
"""Context manager for lazy imports."""
_lazy_modules = {} # Store lazy modules here

def __init__(self):
"""Initializes a context manager for lazy module imports to optimize startup time and memory usage."""
self._original_import = builtins.__import__
self._lazy_modules = {} # Store lazy modules here
self._globals = {}

def __enter__(self):
Expand All @@ -71,10 +72,10 @@ def lazy_import(name, globals=None, locals=None, fromlist=(), level=0):
return types.SimpleNamespace(
**{from_item: self._lazy_modules[f"{module_name}.{from_item}"] for from_item in fromlist}
)
elif module_name in globals:
return globals[module_name]
else:
if module_name in sys.modules:
self._lazy_modules[module_name] = globals[module_name] # avoid duplicate issue
elif module_name in sys.modules:
self._lazy_modules[module_name] = sys.modules[module_name] # module already loaded in session
elif module_name not in self._lazy_modules:
self._lazy_modules[module_name] = LazyLoader(module_name)
Expand Down

0 comments on commit 5fd88ac

Please sign in to comment.