in 3_optimization-design-ptn/03_prompt-optimization/promptwizard/glue/common/utils/runtime_tasks.py [0:0]
def str_to_class(class_name: str, import_path: str = None, file_path: str = None):
"""
For a given `class_name` in string format, return the class instance (not object).
You need to specify any one of the 2: import_path or file_path. When both are specified `import_path` takes
precedence.
:param class_name: Class name, specified as string e.g. CSVReader
:param import_path: Import path for the specified class_name e.g. llama_index.readers.file
:param file_path: Path to the file where this class is present. e.g. C:\\dir1\\sub_dir1\\filename.py
:return: Class
"""
if import_path:
cls = getattr(import_module(import_path), class_name)
elif file_path:
file_name_without_extsn = splitext(basename(file_path))[0]
spec = spec_from_file_location(file_name_without_extsn, file_path)
module = module_from_spec(spec)
spec.loader.exec_module(module)
cls = getattr(module, class_name)
else:
cls = getattr(sys.modules[__name__], class_name)
return cls