From 7b010266bd56994b3b30a1cb89a02fc3d294a35d Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 8 Aug 2024 17:54:50 +0200 Subject: [PATCH] Improved module loading --- fin_depo/__init__.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/fin_depo/__init__.py b/fin_depo/__init__.py index 01427e6..42023b7 100644 --- a/fin_depo/__init__.py +++ b/fin_depo/__init__.py @@ -57,6 +57,8 @@ __all__ = [ import importlib import logging +import inspect +import sys from . import ( data, @@ -67,9 +69,12 @@ from ._version import __version__ logger = logging.getLogger(__name__) +current_module = sys.modules[__name__] + def load_backend(name: str) -> object | None: try: - return importlib.import_module(f'{__name__}.{name}') + imported_module = importlib.import_module(f'{__name__}.{name}') + current_module.setmember(name, imported_module) except Exception: logger.exception( 'Backend %s could not be imported? Are all module dependencies installed?', @@ -79,7 +84,8 @@ def load_backend(name: str) -> object | None: # Import modules -defi_kraken = load_backend('defi_kraken') -defi_kucoin = load_backend('defi_kucoin') -investbank_nordnet = load_backend('investbank_nordnet') -defi_partisia_blockchain = load_backend('defi_partisia_blockchain') +backend_modules = ['defi_kraken', 'defi_kucoin', 'investbank_nordnet', 'defi_partisia_blockchain'] +for m in backend_modules: + load_backend(m) + del m +del backend_modules, current_module