EnumChoices

Format: Python
 ( View Raw)
Date: Thu, 15 Aug 2024 at 22:02:42

class EnumChoices(enum.EnumMeta):
    def __new__(cls, name, bases, classdict):
        @classmethod
        def choices(cls) -> typing.Tuple[typing.Tuple[typing.Union[str, typing.Any]]]:
            """
            Return a tuple of tuples with value and name for each item in an Enum
            This is useful to pass an Enum to a ChoiceField

            :param items: Enum object
            :return: tuple of tuples of items with value and name
            """
            return tuple((item.value, item.name) for item in cls)

        classdict['choices'] = choices
        return super().__new__(cls, name, bases, classdict)


class QualityCheck(enum.IntEnum, metaclass=EnumChoices):
    NOT_PASSED = 0
    PASSED = 1
    DISCARDED = 2