from importlib import import_module from typing import Type class ClassLoader: def loadClass(self, fullclasspath:str)->Type: ''' @param fullclasspath a string of the form full.path.to.pyfile.classname where pyfile is the name of the module (file) without the .py and classname is the actual name of the class inside that file @return Type (=class object) @raise ModuleNotFoundError if module or class not found: ''' if not "." in fullclasspath: raise ModuleNotFoundError("classpath misses a '.':"+fullclasspath) mo,cl = fullclasspath.rsplit('.', 1) try: mod = import_module(mo) except ModuleNotFoundError as e: raise e except BaseException as e: raise ModuleNotFoundError("Module crashes on load",e) if hasattr(mod, cl): return getattr(mod, cl) raise ModuleNotFoundError("module "+mo+" does not contain class "+cl)