#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = "Carp-Li"
__date__ = "2020/10/18"
from enum import Enum
from collections import namedtuple
from types import DynamicClassAttribute
T = namedtuple("Field", ["value", "desc"])
class FieldEnum(Enum):
@DynamicClassAttribute
def value(self):
""" 直接获取namedtuple的value的值 """
return self._value_.value
@DynamicClassAttribute
def desc(self):
""" 直接获取namedtuple的desc的值 """
return self._value_.desc
@classmethod
def by(cls, value):
""" 根据value获取对应的枚举 """
for field, enum in cls._value2member_map_.items():
if value == field.value:
return enum
raise ValueError(f"{cls.__name__}(value={value}):没有对应的枚举")
@classmethod
def by_desc(cls, text):
""" 根据desc获取对应的枚举 """
for field, enum in cls._value2member_map_.items():
if text == field.desc:
return enum
raise ValueError(f"{cls.__name__}(desc={text}):没有对应的枚举")
@DynamicClassAttribute
def field(self):
""" 返回一个namedtuple """
return self._value_
@classmethod
def iter_field(cls):
""" 返回一个迭代器对象,方便遍历 """
for field in cls._value2member_map_.keys():
yield field
class Gender(FieldEnum):
""" 一个例子 """
MAN = T(1, "男")
WOMAN = T(2, "女")
NULL = T(0, "未知")
if __name__ == '__main__':
print(Gender.by(0))
print(Gender.by_desc("男"))