54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
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)
|