From fad2bbae677a9d9cbe09ef91b18fded6c021d790 Mon Sep 17 00:00:00 2001 From: ZekShawn Date: Tue, 30 Dec 2025 22:42:24 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algos/__init__.py | 2 ++ algos/base.py | 23 ++++++++++++++++ algos/blank.py | 35 ++++++++++++++++++++++++ confs/logging.conf | 33 +++++++++++++++++++++++ confs/template.json | 4 +++ tools/__init__.py | 0 tools/config.py | 53 ++++++++++++++++++++++++++++++++++++ tools/easydict.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 215 insertions(+) create mode 100644 algos/__init__.py create mode 100644 algos/base.py create mode 100644 algos/blank.py create mode 100644 confs/logging.conf create mode 100644 confs/template.json create mode 100644 tools/__init__.py create mode 100644 tools/config.py create mode 100644 tools/easydict.py diff --git a/algos/__init__.py b/algos/__init__.py new file mode 100644 index 0000000..00c294f --- /dev/null +++ b/algos/__init__.py @@ -0,0 +1,2 @@ +from algos.base import BaseRunner, BaseAlgoModel +from algos.blank import BlankAlgo, BlankRunner \ No newline at end of file diff --git a/algos/base.py b/algos/base.py new file mode 100644 index 0000000..8cc6aa1 --- /dev/null +++ b/algos/base.py @@ -0,0 +1,23 @@ +from abc import ABC, abstractmethod + + +class BaseRunner(ABC): + + @abstractmethod + def build_model(self): + pass + + @abstractmethod + def run(self): + pass + + @abstractmethod + def run_test(self): + pass + + +class BaseAlgoModel(ABC): + + @abstractmethod + def forward(self, *args, **kwargs): + pass diff --git a/algos/blank.py b/algos/blank.py new file mode 100644 index 0000000..70d0598 --- /dev/null +++ b/algos/blank.py @@ -0,0 +1,35 @@ +import logging +from algos import BaseRunner, BaseAlgoModel +logger = logging.getLogger(__name__) + + +class BlankAlgo(BaseAlgoModel): + + def forward(self, *args, **kwargs): + logger.warning("There is no such algo type, running blank algo class.") + + +class BlankRunner(BaseRunner): + + def build_model(self): + logger.warning("There is no such algo runner, running blank building process.") + + def run(self): + logger.warning("There is no such algo runner, running blank runner.") + + def run_test(self): + logger.warning("There is no such algo runner, running blank testing process.") + + +def test_blank_runner(): + runner = BlankRunner() + runner.run() + runner.run_test() + +def test_blank_algo(): + algo = BlankAlgo() + algo.forward() + +if __name__ == '__main__': + test_blank_algo() + test_blank_runner() diff --git a/confs/logging.conf b/confs/logging.conf new file mode 100644 index 0000000..de0a5b3 --- /dev/null +++ b/confs/logging.conf @@ -0,0 +1,33 @@ +# logging.conf +[loggers] +keys=root + +[handlers] +keys=consoleHandler,fileHandler + +[formatters] +keys=simpleFormatter,detailedFormatter + +[logger_root] +level=INFO +handlers=consoleHandler + +[handler_consoleHandler] +class=StreamHandler +level=INFO +formatter=simpleFormatter +args=(sys.stdout,) + +[handler_fileHandler] +class=FileHandler +level=DEBUG +formatter=detailedFormatter +args=('app.log', 'a') + +[formatter_simpleFormatter] +format=%(asctime)s - %(name)s - %(levelname)s - %(message)s +datefmt=%Y-%m-%d %H:%M:%S + +[formatter_detailedFormatter] +format=%(asctime)s - %(name)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s +datefmt=%Y-%m-%d %H:%M:%S diff --git a/confs/template.json b/confs/template.json new file mode 100644 index 0000000..cbb4f5c --- /dev/null +++ b/confs/template.json @@ -0,0 +1,4 @@ +{ + "model_name": "What's your model name", + "file_path": "Where's your data file ptah" +} \ No newline at end of file diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tools/config.py b/tools/config.py new file mode 100644 index 0000000..1f02459 --- /dev/null +++ b/tools/config.py @@ -0,0 +1,53 @@ +import argparse +import os +import json +from abc import ABC, abstractmethod +from tools.easydict import EasyDict + + +class AbstractConfig(ABC): + + @staticmethod + @abstractmethod + def to_dict(parser_args, conf_path_list=None): + pass + + @staticmethod + @abstractmethod + def from_dict(dict_): + pass + +class Config(AbstractConfig): + + @staticmethod + def to_dict(parser_args, conf_path_list=None): + args = None + if conf_path_list is None or len(parser_args) == 0: + args = EasyDict(parser_args.__dict__) + else: + args = EasyDict() + for json_file_path in conf_path_list: + if not os.path.exists(json_file_path): + raise FileNotFoundError(json_file_path) + with open(json_file_path, 'r', encoding='utf-8') as file: + json_dict = json.load(file) + args.update(dict=json_dict) + return args + + @staticmethod + def from_dict(dict_): + return EasyDict(dict_) + + +if __name__ == '__main__': + # 命令行参数传递 + parser = argparse.ArgumentParser(description='Python App Runner') + parser.add_argument('-filename', type=str, default="", help='文件名称') + parser.add_argument('-upload', '--upload', action='store_true', help='是否上传模型') + parser.add_argument('-save', '--save', action='store_true', help='是否保存模型') + parser.add_argument('-online', '--online', action='store_true', help='是否生产环境') + parse_args = parser.parse_args() + + # 配置文件参数传递 + config = Config("/Users/shawn/Code/python_algo_template/confs/template.json", parser=parse_args) + print(config.to_dict) diff --git a/tools/easydict.py b/tools/easydict.py new file mode 100644 index 0000000..78e7caf --- /dev/null +++ b/tools/easydict.py @@ -0,0 +1,65 @@ + + +class EasyDict(dict): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._convert_nested() + + def _convert_nested(self): + for key, value in self.items(): + if isinstance(value, dict): + self[key] = EasyDict(value) + elif isinstance(value, list): + self[key] = [EasyDict(item) if isinstance(item, dict) else item for item in value] + + def __getattr__(self, key): + try: + return self[key] + except KeyError: + raise AttributeError(f"'EasyDict' object has no attribute '{key}'") + + def update(self, **kwargs): + assert kwargs is not None and kwargs.get("dict", None) is not None + for key, value in kwargs["dict"].items(): + + cur_value = self.get(key, None) + if isinstance(cur_value, dict) and isinstance(value, dict): + self[key].update(dict = value) + continue + + if isinstance(cur_value, list) and isinstance(value, list): + self[key].extend([EasyDict(item) if isinstance(item, dict) else item for item in value]) + continue + + if isinstance(value, dict): + self[key] = EasyDict(value) + continue + + if isinstance(value, list): + self[key] = [EasyDict(item) if isinstance(item, dict) else item for item in value] + continue + + self[key] = value + + def to_dict(self): + result = {} + for key, value in self.__dict__.items(): + if isinstance(value, EasyDict): + result[key] = value.to_dict() + elif isinstance(value, list): + result[key] = [ + item.to_dict() if isinstance(item, EasyDict) else item + for item in value + ] + else: + result[key] = value + return result + +def test_easy_dict(): + easy_dict = EasyDict({"c": 2, "2": 3}) + easy_dict.update(dict = {"c": 4, "2": {"d": 5}}) + print(easy_dict) + + +if __name__ == '__main__': + test_easy_dict()