CIEnum¶
- class deepfos.lib.utils.CIEnum(value)¶
大小写非敏感的枚举类
经常用于入参为一组可选字符串的情况。 可以简化参数检查逻辑代码的书写。 当入参不支持时,提供友好的报错提示。
>>> class Flag(CIEnum): ... zero = 'zero' ... negative = 'negative' ... positive = 'positive' >>> def example(flag: Union[Flag, str]): ... flag = Flag[flag] ... if flag is Flag.zero: ... return '0' ... elif flag is Flag.negative: ... return '-' ... else: ... return '+' >>> example('zero') '0' >>> example('Zero') '0' >>> example(Flag.negative) '-' >>> example('unknown') Traceback (most recent call last): ... KeyError: "Flag: 'unknown' is not valid. Possible choices are: [zero, negative, positive]"