..

Python枚举类自定义状态

  • 枚举类不支持实例化;
  • 成员的值不能在类的外部做任何修改,每个成员都有`name`,`value`两个属性,其中`name`为该枚举值的名称,`value`为该枚举值的内容;
  • 定义枚举时,成员名称不允许重复,但在默认情况下,不同的成员值允许相同,两个相同值的成员,第二个成员的名称将会被视作第一个成员的别名,若要限制不能定义相同值的成员。可以使用装饰器`@unique`;
  • 枚举支持迭代器,可以遍历枚举成员;如果枚举有值重复的成员,循环遍历枚举时只获取值重复成员的第一个成员,若想把值重复的成员也遍历出来,要用枚举的一个特殊属性:`__members__`;
from enum import Enum
from typing import Optional

class BaseEnm(Enum):

  def getCode(self):
    """根据枚举名称取状态码code

    @return: 状态码code
   """
    return self.value[0]

  def getMessage(self):
    """根据枚举名称取状态说明message

    @return: 状态说明message
    """
    return self.value[1]

  @classmethod
  def messageByCode(cls, code) -> Optional[str]:
    """根据状态码获取状态码说明"""
    for name, member in cls.__members__.items():
      if code in member.value:
        return cls[name].getMessage()
    return None

@unique
class CallStateResult(BaseEnm):
  """通话状态结果"""

  USER_STATE_NULL = (0, "未知的初始状态")
  USER_STATE_ANSWERED = (1, "接通")
  USER_STATE_POWER_OFF = (2, "关机")
  USER_STATE_NOT_EXIST = (3, "空号")
  USER_STATE_INVALID_NUM = (4, "非法号码")
  USER_STATE_OUTSERVICE = (5, "停机")
  USER_STATE_NOT_IN_SERVICE = (6, "不在服务区")
  USER_STATE_NOT_ANSWER = (7, "无人接听")
  USER_STATE_BUSY = (8, "用户忙")
  USER_STATE_NOT_REACHABLE = (9, "无法接通")
  USER_STATE_NOT_LOCALNUM_NEED_ZERO = (10, "本地号码未加零")
  USER_STATE_LOCALNUM_ADD_ZERO = (11, "本地号码多加零")
  USER_STATE_BARRING_INCOMING = (12, "呼入限制")
  USER_STATE_CALL_REMINDER = (13, "来电提醒")
  USER_STATE_DEFAULTING = (14, "欠费")
  USER_STATE_LINE_BUSY = (15, "网络忙")
  USER_STATE_FORWARDED = (16, "呼叫转移失败")
  USER_STATE_CANNOT_CONNECTED = (17, "无法接听")
  USER_STATE_NUMBER_CHANGE = (18, "改号")
  USER_STATE_LINE_FAULT = (19, "线路故障")
  USER_STATE_CALL_REJECTED = (20, "拒接")
  USER_STATE_NOT_RECOGNITION = (21, "未知状态")
  USER_STATE_CALL_FAILED = (22, "呼叫失败")
  USER_STATE_HANGUP = (100, "通话结束")


if __name__ == '__main__':
  print(CallStateResult.USER_STATE_NULL)
  print(CallStateResult.USER_STATE_NULL.getCode())
  print(CallStateResult.USER_STATE_NULL.getMessage())
  print(CallStateResult.messageByCode(0))

输出

CallStateResult.USER_STATE_NULL
0
未知的初始状态
未知的初始状态