source: utilitiespy/tudelft/utilities/tools/classloader.py@ 1536

Last change on this file since 1536 was 1536, checked in by wouter, 2 weeks ago

#2122 add ClassLoader to Utilities. Bump to 1.1.5

File size: 805 bytes
RevLine 
[1536]1from importlib import import_module
2from typing import Type
3
4class ClassLoader:
5 def loadClass(self, fullclasspath:str)->Type:
6 '''
7 @param fullclasspath a string of the form full.path.to.pyfile.classname
8 where pyfile is the name of the module (file) without the .py
9 and classname is the actual name of the class inside that file
10 @return Type (=class object)
11 @raise ModuleNotFoundError if module or class not found:
12 '''
13 if not "." in fullclasspath:
14 raise ModuleNotFoundError("classpath misses a '.':"+fullclasspath)
15
16 mo,cl = fullclasspath.rsplit('.', 1)
17 mod = import_module(mo)
18 if hasattr(mod, cl):
19 return getattr(mod, cl)
20 raise ModuleNotFoundError("module "+mo+" does not contain class "+cl)
21
Note: See TracBrowser for help on using the repository browser.