feat(tools): 新增常量字典基类 ConstantDict
This commit is contained in:
0
nc_http/tools/constant/__init__.py
Normal file
0
nc_http/tools/constant/__init__.py
Normal file
75
nc_http/tools/constant/base.py
Normal file
75
nc_http/tools/constant/base.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import re
|
||||
|
||||
|
||||
class ConstantDict:
|
||||
"""
|
||||
常量字典基类
|
||||
"""
|
||||
|
||||
values = {}
|
||||
|
||||
@classmethod
|
||||
def get_keys(cls):
|
||||
return cls.values.keys()
|
||||
|
||||
@classmethod
|
||||
def get_values(cls):
|
||||
return cls.values.values()
|
||||
|
||||
@classmethod
|
||||
def get_data(cls):
|
||||
return cls.values
|
||||
|
||||
@classmethod
|
||||
def present(cls):
|
||||
return [{"id": key, "name": val} for key, val in cls.values.items()]
|
||||
|
||||
@classmethod
|
||||
def get_value(cls, key):
|
||||
return cls.values.get(int(key))
|
||||
|
||||
@classmethod
|
||||
def validate(cls, key):
|
||||
return int(key) in cls.values
|
||||
|
||||
@classmethod
|
||||
def get_multi_dict(cls, keys):
|
||||
return [{"id": key, "name": cls.get_value(key)} for key in keys if int(key) in cls.values.keys()]
|
||||
|
||||
@classmethod
|
||||
def get_dict(cls, key):
|
||||
return [{"id": key, "name": cls.get_value(key)} if int(key) in cls.values.keys() else {}][0]
|
||||
|
||||
@classmethod
|
||||
def get_key_str(cls, val):
|
||||
for key, value in cls.__dict__.items():
|
||||
if re.match('^[A-Z_]+$', key):
|
||||
if value == val:
|
||||
return key
|
||||
return ''
|
||||
|
||||
@classmethod
|
||||
def get_key_value_str(cls):
|
||||
res = {}
|
||||
for key, value in cls.__dict__.items():
|
||||
if re.match('^[A-Z_]+$', key):
|
||||
res[key] = value
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
def get_id_by_name(cls, name):
|
||||
for key, value in cls.values.items():
|
||||
if value == name:
|
||||
return key
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_key_choices(cls):
|
||||
keys = cls.get_keys()
|
||||
return ','.join(str(i) for i in keys)
|
||||
|
||||
@classmethod
|
||||
def contains(cls, keyword):
|
||||
keyword = keyword.strip()
|
||||
result = [key for key, value in cls.values.items() if keyword in value]
|
||||
return result
|
Reference in New Issue
Block a user