1. 提交代码,初始化项目;
This commit is contained in:
2
algos/__init__.py
Normal file
2
algos/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from algos.base import BaseRunner, BaseAlgoModel
|
||||
from algos.blank import BlankAlgo, BlankRunner
|
||||
23
algos/base.py
Normal file
23
algos/base.py
Normal file
@@ -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
|
||||
35
algos/blank.py
Normal file
35
algos/blank.py
Normal file
@@ -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()
|
||||
33
confs/logging.conf
Normal file
33
confs/logging.conf
Normal file
@@ -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
|
||||
4
confs/template.json
Normal file
4
confs/template.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"model_name": "What's your model name",
|
||||
"file_path": "Where's your data file ptah"
|
||||
}
|
||||
0
tools/__init__.py
Normal file
0
tools/__init__.py
Normal file
53
tools/config.py
Normal file
53
tools/config.py
Normal file
@@ -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)
|
||||
65
tools/easydict.py
Normal file
65
tools/easydict.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user