-2

How to reverse Flag_to_Marker during compilation:

class FLAG(IntEnum):
    DEFAULT_ZERO = 0b0
    L1 = 0b01
    L2 = 0b10
    RSU = 0b100
    ESCALATED_COMMENTS = 0b1000

    Flag_to_Marker = {L1: 'is_l1', L2: 'is_l2', RSU: 'is_rsu'}
    Marker_to_Flag = {v: k for k, v in Flag_to_Marker.items()}

    def marker(self, flag):
       return self.Flag_to_Marker[flag]

    def flag(self, marker):
       return self.Marker_to_Flag[marker]

but I have the following exception when I trying to construct Marker_to_Flag: int() argument must be a string, a bytes-like object or a number, not 'dict'. How to fix that?

I want to have Marker_to_Flag = {'is_l1': L1, 'is_l2': L2, 'is_rsu': RSU}

indigo153
  • 1,021
  • 2
  • 10
  • 23
  • 3
    Do you want `Marker_to_Flag = {v: k for k, v in Flag_to_Marker.items()}` instead? – DavidG Feb 07 '18 at 14:53
  • I tried, but I got `int() argument must be a string, a bytes-like object or a number, not 'dict'` – indigo153 Feb 07 '18 at 14:54
  • Possible duplicate of [Python reverse / invert a mapping](https://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping) – Mike Scotty Feb 07 '18 at 14:56
  • the problem is somewhere we don't see – Jean-François Fabre Feb 07 '18 at 14:56
  • 1
    You can't define arbitrary data in an `IntEnum`. Define your dictionaries outside of the class. Otherwise python tries to convert them into an integer. Take a look at the documentation - there is also some information about using enums as flags. – Wombatz Feb 07 '18 at 15:02

2 Answers2

-2
Marker_to_Flag = {v: k for k, v in Flag_to_Marker.items()}
Sebastian
  • 1,623
  • 19
  • 23
  • I tried, but I got `int() argument must be a string, a bytes-like object or a number, not 'dict'` – indigo153 Feb 07 '18 at 14:55
  • I think that error is being generated somewhere else. I ran the code through my interpreter and the dictionary is reversed – Sebastian Feb 07 '18 at 14:57
-2
  1. Your values must be unique.
  2. reversed_dict = {y:x for x,y in Flag_to_Marker.items()}
Ilia Levikov
  • 166
  • 1
  • 4
  • 13