first commit
This commit is contained in:
144
SetParams/DataType/BaseParam.py
Normal file
144
SetParams/DataType/BaseParam.py
Normal file
@ -0,0 +1,144 @@
|
||||
import json
|
||||
from .TypeDef import *
|
||||
from typing import Union
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class BaseParam:
|
||||
"""
|
||||
参数基类
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
参数基类,算法中所有需要参数的方法中,使用的参数必须继承该类型
|
||||
"""
|
||||
self.file = ''
|
||||
self.param_list = []
|
||||
|
||||
def save_to_file(self, file: str) -> None:
|
||||
"""
|
||||
将参数保存到文件
|
||||
:param file: 文件完整路径
|
||||
:return: None
|
||||
"""
|
||||
with open(file, 'w') as f:
|
||||
f.write(str(self))
|
||||
f.flush()
|
||||
|
||||
def add_param(self, p: Union[IntType, BoolType, FloatType, StringType, ListType, EnumType]) -> None:
|
||||
"""
|
||||
将数据添加到参数列表中,以支持序列化
|
||||
:param p: 单个数据,必须为系统支持的参数类型之一:IntType, BoolType, FloatType, StringType, ListType, EnumType
|
||||
:return: None
|
||||
"""
|
||||
self.param_list.append(p)
|
||||
|
||||
def read_from_str(self, content: str) -> None:
|
||||
"""
|
||||
将字符串反序列化为对象中的参数
|
||||
:param content: 要反序列化的字符串
|
||||
:return: None
|
||||
"""
|
||||
self.param_list.clear()
|
||||
objs = json.loads(content)
|
||||
for obj in objs:
|
||||
#try:
|
||||
item = obj #json.loads(obj)
|
||||
t = item["type"]
|
||||
param = None
|
||||
|
||||
if t == "B":
|
||||
param = BoolType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "I":
|
||||
param = IntType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "F":
|
||||
param = FloatType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
#param.length = item["length"]
|
||||
elif t == "S":
|
||||
item['length']=len(item["value"])
|
||||
param = StringType(item["name"],
|
||||
item["value"],
|
||||
item['length'],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
elif t == "L":
|
||||
param = ListType(item["name"],
|
||||
item["value"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
#param.length = item["length"]
|
||||
elif t == "E":
|
||||
param = EnumType(item["name"],
|
||||
item["value"],
|
||||
item["items"],
|
||||
item["description"],
|
||||
item["default"],
|
||||
item["show"])
|
||||
param.index = item["index"]
|
||||
|
||||
if param:
|
||||
self.param_list.append(param)
|
||||
# except Exception as e:
|
||||
# pass
|
||||
|
||||
def read_from_file(self, file: str) -> None:
|
||||
"""
|
||||
从文件读取序列化后的参数字符串,并反序列化为对象
|
||||
:param file: 文件完整路径
|
||||
:return: None
|
||||
"""
|
||||
with open(file, 'r') as f:
|
||||
content = f.read()
|
||||
self.read_from_str(content)
|
||||
|
||||
@property
|
||||
def param_string(self) -> str:
|
||||
"""
|
||||
获取Json格式的参数字符串
|
||||
:return: 返回Json格式的参数字符串
|
||||
"""
|
||||
for i, p in enumerate(self.param_list):
|
||||
p.index = i
|
||||
return self.__str__()
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
重写str方法用于将对象序列化为Json字符串
|
||||
:return: 序列化后的JSON字符串
|
||||
"""
|
||||
ret = []
|
||||
for index, item in enumerate(self.param_list):
|
||||
obj = item.obj
|
||||
if isinstance(obj, dict):
|
||||
obj["index"] = index
|
||||
item_obj = json.dumps(obj)
|
||||
ret.append(item_obj)
|
||||
return json.dumps(ret)
|
||||
|
||||
def get(self, name: str) -> Union[None, IntType, BoolType, FloatType, StringType, ListType, EnumType,]:
|
||||
"""
|
||||
根据数据名称获取指定的数据对象
|
||||
:param name: 数据名称
|
||||
:return:
|
||||
"""
|
||||
return next((x for x in self.param_list if x.name == name), None)
|
21
SetParams/DataType/ParamDef.py
Normal file
21
SetParams/DataType/ParamDef.py
Normal file
@ -0,0 +1,21 @@
|
||||
from .TypeDef import *
|
||||
from .BaseParam import *
|
||||
|
||||
|
||||
class TrainParams(BaseParam):
|
||||
"""
|
||||
训练参数示例,使用时需要根据实际情况修改,如有其他参数(如推理函数的参数)需要自定义,并继承自 BaseParam
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
# 例如训练的时候需要传入以下参数
|
||||
gpu_num = IntType("gpu_num", 2)
|
||||
support_cpu = BoolType("support_cpu", True)
|
||||
labels = EnumType("labels", 0, ["dog", "cat"])
|
||||
labels.default = 0
|
||||
|
||||
self.add_param(gpu_num)
|
||||
self.add_param(support_cpu)
|
||||
self.add_param(labels)
|
243
SetParams/DataType/TypeDef.py
Normal file
243
SetParams/DataType/TypeDef.py
Normal file
@ -0,0 +1,243 @@
|
||||
import json
|
||||
from typing import List
|
||||
from abc import abstractmethod
|
||||
|
||||
|
||||
class __BaseType:
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: object,
|
||||
description: str,
|
||||
default: object,
|
||||
show: bool):
|
||||
"""
|
||||
所有数据类型的基类
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.index = -1
|
||||
self.default = default
|
||||
self.value = value
|
||||
self.show = show
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def type(self) -> str:
|
||||
"""
|
||||
参数类型,用于前端/后台的序列化与反序列化。前端展示时也需要根据此字段类型绘制不同的控件
|
||||
:return: 一般使用单个字母,如:整型(I),布尔型(B),浮点型(F),字符串(S),列表(L),枚举(E)
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def _base_obj(self) -> dict:
|
||||
"""
|
||||
用于创建所有数据类型的共有对象字典
|
||||
:return:
|
||||
"""
|
||||
return {
|
||||
"index": self.index,
|
||||
"name": self.name,
|
||||
"value": self.value,
|
||||
"description": self.description,
|
||||
"default": self.default,
|
||||
"type": self.type,
|
||||
"show": self.show
|
||||
}
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def obj(self) -> dict:
|
||||
"""
|
||||
抽象属性,获取数据类型的对象字典
|
||||
:return:
|
||||
"""
|
||||
return self._base_obj
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
重写数据类型的字符串序列化方法
|
||||
:return:
|
||||
"""
|
||||
return json.dumps(self.obj)
|
||||
|
||||
|
||||
class BoolType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: bool = False,
|
||||
description: str = "",
|
||||
default: bool = False,
|
||||
show: bool = True):
|
||||
"""
|
||||
布尔类型数据
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'B'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class IntType(__BaseType):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: int = 0,
|
||||
description: str = "",
|
||||
default: int = 0,
|
||||
show: bool = True):
|
||||
"""
|
||||
整数类型
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'I'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class FloatType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: float = 0.,
|
||||
description: str = "",
|
||||
default: float = 0.,
|
||||
show: bool = True):
|
||||
"""
|
||||
浮点类型
|
||||
:param name: 数据名称
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端UI展示
|
||||
:param default: 该参数的默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'F'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
return super()._base_obj
|
||||
|
||||
|
||||
class StringType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: str = "",
|
||||
length: int = -1,
|
||||
description: str = "",
|
||||
default: str = "",
|
||||
show: bool = True):
|
||||
"""
|
||||
字符串类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值
|
||||
:param length: 字符串长度
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
super().__init__(name, value, description, default, show)
|
||||
self.length = length
|
||||
if length > 0:
|
||||
self.value = value[:length]
|
||||
else:
|
||||
self.value = value
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return 'S'
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["length"] = self.length
|
||||
return base_obj
|
||||
|
||||
|
||||
class ListType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: list = None,
|
||||
description: str = "",
|
||||
default: list = None,
|
||||
show: bool = True):
|
||||
"""
|
||||
列表类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
if not default:
|
||||
default = []
|
||||
|
||||
if not value:
|
||||
value = default
|
||||
|
||||
self.length = len(value)
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return "L"
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["length"] = self.length
|
||||
return base_obj
|
||||
|
||||
|
||||
class EnumType(__BaseType):
|
||||
def __init__(self,
|
||||
name: str,
|
||||
value: int = -1,
|
||||
items: List[str] = None,
|
||||
description: str = "",
|
||||
default: int = -1,
|
||||
show: bool = True):
|
||||
"""
|
||||
枚举类型
|
||||
:param name: 数据名称,用于程序内部标识该数据
|
||||
:param value: 数据值,即选择了枚举列表中的第几项
|
||||
:param items: 枚举范围,枚举项必须为字符串类型
|
||||
:param description: 描述,用于前端页面对该数据进行展示
|
||||
:param default: 默认值
|
||||
"""
|
||||
if not items:
|
||||
items = []
|
||||
default = -1
|
||||
self.items = items
|
||||
super().__init__(name, value, description, default, show)
|
||||
|
||||
@property
|
||||
def type(self) -> str:
|
||||
return "E"
|
||||
|
||||
@property
|
||||
def obj(self) -> dict:
|
||||
base_obj = super()._base_obj
|
||||
base_obj["items"] = self.items
|
||||
return base_obj
|
0
SetParams/DataType/__init__.py
Normal file
0
SetParams/DataType/__init__.py
Normal file
Reference in New Issue
Block a user