创建一个抽象的Enum类

时间:2019-05-14 13:11:09

标签: python enums python-3.6

我正在尝试使用抽象方法创建抽象枚举(实际上是Flag)。 我的最终目标是能够根据我定义的基本枚举创建复合枚举的字符串表示形式。 我可以在不使类抽象化的情况下获得此功能。

这是基本的Flag类和示例实现:

from enum import auto, Flag

class TranslateableFlag(Flag):
    @classmethod
    def base(cls):
        pass

    def translate(self):
        base = self.base()
        if self in base:
            return base[self]
        else:
            ret = []
            for basic in base:
                if basic in self:
                    ret.append(base[basic])
            return " | ".join(ret)

class Students(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE

    @classmethod
    def base(cls):
        return {Students.ALICE: "Alice", Students.BOB: "Bob",
                Students.CHARLIE: "Charlie"}

用法示例为:

((Students.ALICE | Students.BOB).translate())
[Out]: 'Alice | Bob'

由于MetaClass冲突,切换到TranslateableFlag(Flag, ABC)失败。 (我不了解这篇文章-Abstract Enum Class using ABCMeta and EnumMeta,所以不确定它是否在回答我的问题。)

我想以某种方式获得这样的功能:

@abstractclassmethod
@classmethod
    def base(cls):
        pass

有可能实现这一目标吗?

3 个答案:

答案 0 :(得分:8)

以下是将accepted answer修改为问题Abstract Enum Class using ABCMeta and EnumMeta的方法,以创建所需的抽象Enum类:

from abc import abstractmethod, ABC, ABCMeta
from enum import auto, Flag, EnumMeta


class ABCEnumMeta(ABCMeta, EnumMeta):

    def __new__(mcls, *args, **kw):
        abstract_enum_cls = super().__new__(mcls, *args, **kw)
        try:  # Handle existence of undefined abstract methods.
            absmethods = list(abstract_enum_cls.__abstractmethods__)
            absmethods_str = ', '.join(f'{method!r}' for method in absmethods)
            plural = 's' if len(absmethods) > 1 else ''
            raise TypeError(
                f"cannot instantiate abstract class {abstract_enum_cls.__name__!r}"
                f" with abstract method{plural} {absmethods_str}")
        except AttributeError:
            pass
        return abstract_enum_cls


class TranslateableFlag(Flag, metaclass=ABCEnumMeta):

    @classmethod
    @abstractmethod
    def base(cls):
        pass

    def translate(self):
        base = self.base()
        if self in base:
            return base[self]
        else:
            ret = []
            for basic in base:
                if basic in self:
                    ret.append(base[basic])
            return " | ".join(ret)


class Students1(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE

    @classmethod
    def base(cls):
        return {Students1.ALICE: "Alice", Students1.BOB: "Bob",
                Students1.CHARLIE: "Charlie"}


class Students2(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE

# Abstract method not defined - should raise TypeError.
#    @classmethod
#    def base(cls):
#        ...

结果:

Traceback (most recent call last):
  ...
TypeError: cannot instantiate abstract class 'TranslateableFlag' with abstract method 'base'

答案 1 :(得分:1)

如果目标只是更改__str__的{​​{1}}输出,则无需使用抽象类:

Students1

并在使用中:

from enum import auto, Flag
from functools import reduce

class TranslateableFlag(Flag):

    def __init__(self, value):
        self.translated = self.name.title()

    def __str__(self):
        cls = self.__class__
        total = self._value_
        i = 1
        bits = set()
        while i <= total:
            bits.add(i)
            i *= 2
        members = [m for m in cls if m._value_ in bits]
        return '%s' % (
                ' | '.join([str(m.translated) for m in members]),
                )

class Students1(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE

答案 2 :(得分:0)

这是python 3.8可接受答案的修复。唯一的变化是ABCEnumMeta。其余部分从原始答案中复制粘贴,以提供可运行的示例。还在python 3.6.2上测试过。

from abc import abstractmethod, ABC, ABCMeta
from enum import auto, Flag, EnumMeta


class ABCEnumMeta(EnumMeta, ABCMeta):
    pass


class TranslateableFlag(Flag, metaclass=ABCEnumMeta):

    @classmethod
    @abstractmethod
    def base(cls):
        pass

    def translate(self):
        base = self.base()
        if self in base:
            return base[self]
        else:
            ret = []
            for basic in base:
                if basic in self:
                    ret.append(base[basic])
            return " | ".join(ret)


class Students1(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE

    @classmethod
    def base(cls):
        return {Students1.ALICE: "Alice", Students1.BOB: "Bob",
                Students1.CHARLIE: "Charlie"}


class Students2(TranslateableFlag):
    ALICE = auto()
    BOB = auto()
    CHARLIE = auto()
    ALL = ALICE | BOB | CHARLIE
    
# Abstract method not defined - should raise TypeError.
#    @classmethod
#    def base(cls):
#        ...