1from enum import IntEnum 2 3 4class ParamEnum(IntEnum): 5 """Wraps IntEnum to provide validation of a requested item. 6 7 Intended for enums used for function parameters. 8 9 Use enum.get_value(item) for this behavior instead of builtin enum[item]. 10 """ 11 12 @classmethod 13 def get_value(cls, item): 14 """Validate incoming item and raise a ValueError with valid options if not present.""" 15 try: 16 return cls[item].value 17 except KeyError: 18 valid_options = {e.name for e in cls} 19 raise ValueError( 20 "'{}' is not a valid option, must be one of '{}'".format( 21 item, "', '".join(valid_options) 22 ) 23 ) 24