refactor: The first refactored version for sdk release (#907)

Co-authored-by: chengfangyin2 <chengfangyin3@jd.com>
This commit is contained in:
FangYin Cheng
2023-12-08 14:45:59 +08:00
committed by GitHub
parent e7e4aff667
commit cd725db1fb
573 changed files with 2094 additions and 3571 deletions

View File

@@ -0,0 +1,28 @@
from typing import Type
from importlib import import_module
def import_from_string(module_path: str, ignore_import_error: bool = False):
try:
module_path, class_name = module_path.rsplit(".", 1)
except ValueError:
raise ImportError(f"{module_path} doesn't look like a module path")
module = import_module(module_path)
try:
return getattr(module, class_name)
except AttributeError:
if ignore_import_error:
return None
raise ImportError(
f'Module "{module_path}" does not define a "{class_name}" attribute/class'
)
def import_from_checked_string(module_path: str, supper_cls: Type):
cls = import_from_string(module_path)
if not issubclass(cls, supper_cls):
raise ImportError(
f'Module "{module_path}" does not the subclass of {str(supper_cls)}'
)
return cls