From 9ff9c04c35841ff9e73512463ce8ba82bf9455b1 Mon Sep 17 00:00:00 2001 From: han0 Date: Mon, 9 Aug 2021 15:19:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(tools):=20=E6=96=B0=E5=A2=9E=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=AD=97=E5=85=B8=E5=9F=BA=E7=B1=BB=20ConstantDict?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nc_http/tools/constant/__init__.py | 0 nc_http/tools/constant/base.py | 75 ++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 nc_http/tools/constant/__init__.py create mode 100644 nc_http/tools/constant/base.py diff --git a/nc_http/tools/constant/__init__.py b/nc_http/tools/constant/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/tools/constant/base.py b/nc_http/tools/constant/base.py new file mode 100644 index 0000000..09bb011 --- /dev/null +++ b/nc_http/tools/constant/base.py @@ -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