I'm setting an enumeration "A" of possible commands in my Python code. I would also create another enumeration "B" listing some specific commands "A".
Thus I tried :
class all_commands(IntEnum):
SET_ELEMENT_A = 0x00000001
GET_ELEMENT_A = 0x00000007
GET_ELEMENT_B = 0x0000000E
SET_ELEMENT_C = 0x00000010
GET_ELEMENT_C = 0x00000011
GET_ELEMENT_D = 0x00000015
class getter_commands(IntEnum):
all_commands.GET_ELEMENT_A
all_commands.GET_ELEMENT_B
all_commands.GET_ELEMENT_C
all_commands.GET_ELEMENT_D
This seems correct to me, however when I try
all_commands.GET_ELEMENT_A in getter_commands
I get a False
result value.
Is that a normal behaviour ? Or is my code wrong ?
I tried using a list, which works with all_commands.GET_ELEMENT_A
but I think an Enum
type would be better
getter_commands = [all_commands.GET_ELEMENT_A,
all_commands.GET_ELEMENT_B,
all_commands.GET_ELEMENT_C,
all_commands.GET_ELEMENT_D]
Thanks for your help !
Enum
is not the solution to you problem. What you need is something like an EnumSet
which would allow iteration, containment checks, and attribute access. So something that is created like:
getter_commands = EnumSet(
all_commands.GET_ELEMENT_A,
all_commands.GET_ELEMENT_B,
all_commands.GET_ELEMENT_C,
all_commands.GET_ELEMENT_D,
)
and with behavior:
>>> list(getter_commands)
[all_commands.GET_ELEMENT_A, all_commands.GET_ELEMENT_B, all_commands.GET_ELEMENT_C, all_commands.GET_ELEMENT_D]
>>> getter_commands.GET_ELEMENT_C
<all_commands.GET_ELEMENT_C: 16>
>>> all_commands.GET_ELEMENT_D in getter_commands
True
>> all_commands.SET_ELEMENT_A in getter_commands
False
Such an object could look like:
class EnumSet:
def __init__(self, *enum_members):
self.__dict__.update({m._name: m for m in enum_members})
__iter__ = self.__dict__.values
User contributions licensed under CC BY-SA 3.0