mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-06 19:47:44 +00:00
example workflow code for model implementation (#17)
* upload example code for model implementation * Refactor Model Implement * add export --------- Co-authored-by: Young <afe.young@gmail.com>
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
This is some common utils functions.
|
||||
it is not binding to the scenarios or framework (So it is not placed in rdagent.core.utils)
|
||||
"""
|
||||
# TODO: merge the common utils in `rdagent.core.utils` into this folder
|
||||
# TODO: split the utils in this module into different modules in the future.
|
||||
|
||||
import importlib
|
||||
import re
|
||||
import sys
|
||||
from types import ModuleType
|
||||
from typing import Union
|
||||
|
||||
|
||||
def get_module_by_module_path(module_path: Union[str, ModuleType]):
|
||||
"""Load module from path like a/b/c/d.py or a.b.c.d
|
||||
|
||||
:param module_path:
|
||||
:return:
|
||||
:raises: ModuleNotFoundError
|
||||
"""
|
||||
if module_path is None:
|
||||
raise ModuleNotFoundError("None is passed in as parameters as module_path")
|
||||
|
||||
if isinstance(module_path, ModuleType):
|
||||
module = module_path
|
||||
else:
|
||||
if module_path.endswith(".py"):
|
||||
module_name = re.sub("^[^a-zA-Z_]+", "", re.sub("[^0-9a-zA-Z_]", "", module_path[:-3].replace("/", "_")))
|
||||
module_spec = importlib.util.spec_from_file_location(module_name, module_path)
|
||||
module = importlib.util.module_from_spec(module_spec)
|
||||
sys.modules[module_name] = module
|
||||
module_spec.loader.exec_module(module)
|
||||
else:
|
||||
module = importlib.import_module(module_path)
|
||||
return module
|
||||
Reference in New Issue
Block a user