Python3 Enum 默认值

Enum的使用场景一般都是在某一个数据结构中,某一个字段的值是几个固定的值,比方说

from enum import Enum

class Status(Enum):
    RUNNING = "running"
    STOPPED = "stopped"
    ERROR = "error"

假设你作为服务端,可能从接口定义上来看,Status 这个字段只能有3个值

  • running
  • pending
  • error

但是实际上客户端发来的数据我们是没办法保证的,如果它发来一个 closing ,用Enum类直接通过closing这个值来parse出Status是不可能的

>>> Status("closing")
ValueError: 'closing' is not a valid Status

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shaokaix/ENV/python3.7/lib/python3.7/enum.py", line 310, in __call__
    return cls.__new__(cls, value)
  File "/home/shaokaix/ENV/python3.7/lib/python3.7/enum.py", line 564, in __new__
    raise exc
  File "/home/shaokaix/ENV/python3.7/lib/python3.7/enum.py", line 548, in __new__
    result = cls._missing_(value)
  File "/home/shaokaix/ENV/python3.7/lib/python3.7/enum.py", line 577, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 'closing' is not a valid Status

为了避免这种情况,可能需要做一些处理,比如说:

try:
    status = Status(value)
except ValueError:
    status = Status.ERROR

其实还有另一种神奇的方式可以在Enum类中直接给定默认值,通过重写_missing_方法,如下

class Status(Enum):
    RUNNING = "running"
    STOPPED = "stopped"
    ERROR = "error"
    UNKNOWN = "unknown"

    @classmethod
    def _missing_(cls, value: object) -> "Status":
        """ Overwrite from Enum to set a default return when value is not defined in  Status

        Args:
            value: the undefined value

        Returns:
            Default Status
        """
        logger.warning("%s is not defined in %s, use default value: %s", value, cls.__name__, Status.UNKNOWN)
        return Status.UNKNOWN


if __name__ == '__main__':
    print(Status("closing"))


# 输出如下
closing is not defined in Status, use default value: Status.UNKNOWN
Status.UNKNOWN

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值