From 305bc9d3ce926d52eef1b5ec4010daf6a400c2f1 Mon Sep 17 00:00:00 2001 From: hf-krechan Date: Fri, 22 Dec 2023 17:04:54 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Use=20dynamical=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bo4e/bo/__init__.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/bo4e/bo/__init__.py b/src/bo4e/bo/__init__.py index e69de29bb..ea7bc2fcb 100644 --- a/src/bo4e/bo/__init__.py +++ b/src/bo4e/bo/__init__.py @@ -0,0 +1,20 @@ +# src/bo4e/bo/__init__.py +import importlib +import pkgutil + +# Iterate through all the modules in the current package +package = __name__ +for _, modname, _ in pkgutil.iter_modules(__path__, package + "."): + # Import the module + module = importlib.import_module(modname) + # Iterate through the attributes of the module + for attribute_name in dir(module): + # Get the attribute + attribute = getattr(module, attribute_name) + # Check if the attribute is a class and it's defined in this module (not imported) + if isinstance(attribute, type) and attribute.__module__ == module.__name__: + # Add the class to the namespace of this module + globals()[attribute_name] = attribute + +# Now you can directly import any class from the bo package +# from bo4e.bo import Vertrag, Ansprechpartner, etc.