[docs]@staticmethoddefget_class_from_path(path,class_name:str=None):""" obtain the class reference from the given path Args: path (str or pathlib.Path): path to file where the class is located class_name (str): class name to import_dataset from the file; if None, it is assumed that the class name is the same as the file name Returns: class """path=Path(path)ifclass_nameisNone:class_name=path.stemreturnReflectionHandler._import_class(path,class_name)
@staticmethoddef_import_class(path:Path,class_name:str):try:module_path=".".join(path.parts[len(list(path.parts))-list(path.parts)[::-1].index("immuneML")-1:])[:-3]mod=import_module(module_path)cls=getattr(mod,class_name)returnclsexceptImportErrorase:logging.warning(f"Class {class_name} could not be imported as the dependency {e.name} is not installed.")returnNone
[docs]@staticmethoddefget_class_by_name(class_name:str,subdirectory:str=""):filenames=ReflectionHandler._get_filenames(class_name,subdirectory)assertlen(filenames)==1,f"ReflectionHandler could not find class named {class_name}. Check spelling and try again."returnReflectionHandler._import_class(filenames[0],class_name)