I'm trying to create an abstract enum (Flag
actually) with an abstract method.
My final goal is to be able to create a string representation of compound enums, based on the basic enums I defined.
I'm able to get this functionality without making the class abstract.
This is the basic Flag
class and an example implementation:
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"}
An example usage is:
((Students.ALICE | Students.BOB).translate())
[Out]: 'Alice | Bob'
Switching to TranslateableFlag(Flag, ABC)
fails due to MetaClass conflicts.
(I didn't understand this post - Abstract Enum Class using ABCMeta and EnumMeta, so I'm not sure if it's answering my question).
I would like get a functionality like this somehow:
@abstractclassmethod
@classmethod
def base(cls):
pass
Is it possible to achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…