extract class/func and kwargs from config info Parameters ---------- config : [dict, str] similar to config please refer to the doc of init_instance_by_config default_module : Python module or str It should be a python module to load the class type
(config: InstConf, default_module: Union[str, ModuleType] = None)
| 65 | |
| 66 | |
| 67 | def get_callable_kwargs(config: InstConf, default_module: Union[str, ModuleType] = None) -> (type, dict): |
| 68 | """ |
| 69 | extract class/func and kwargs from config info |
| 70 | |
| 71 | Parameters |
| 72 | ---------- |
| 73 | config : [dict, str] |
| 74 | similar to config |
| 75 | please refer to the doc of init_instance_by_config |
| 76 | |
| 77 | default_module : Python module or str |
| 78 | It should be a python module to load the class type |
| 79 | This function will load class from the config['module_path'] first. |
| 80 | If config['module_path'] doesn't exists, it will load the class from default_module. |
| 81 | |
| 82 | Returns |
| 83 | ------- |
| 84 | (type, dict): |
| 85 | the class/func object and it's arguments. |
| 86 | |
| 87 | Raises |
| 88 | ------ |
| 89 | ModuleNotFoundError |
| 90 | """ |
| 91 | if isinstance(config, dict): |
| 92 | key = "class" if "class" in config else "func" |
| 93 | if isinstance(config[key], str): |
| 94 | # 1) get module and class |
| 95 | # - case 1): "a.b.c.ClassName" |
| 96 | # - case 2): {"class": "ClassName", "module_path": "a.b.c"} |
| 97 | m_path, cls = split_module_path(config[key]) |
| 98 | if m_path == "": |
| 99 | m_path = config.get("module_path", default_module) |
| 100 | module = get_module_by_module_path(m_path) |
| 101 | |
| 102 | # 2) get callable |
| 103 | _callable = getattr(module, cls) # may raise AttributeError |
| 104 | else: |
| 105 | _callable = config[key] # the class type itself is passed in |
| 106 | kwargs = config.get("kwargs", {}) |
| 107 | elif isinstance(config, str): |
| 108 | # a.b.c.ClassName |
| 109 | m_path, cls = split_module_path(config) |
| 110 | module = get_module_by_module_path(default_module if m_path == "" else m_path) |
| 111 | |
| 112 | _callable = getattr(module, cls) |
| 113 | kwargs = {} |
| 114 | else: |
| 115 | raise NotImplementedError(f"This type of input is not supported") |
| 116 | return _callable, kwargs |
| 117 | |
| 118 | |
| 119 | get_cls_kwargs = get_callable_kwargs # NOTE: this is for compatibility for the previous version |
no test coverage detected