55class EnumValue :
66 # An immutable object representing a specific enum member
77 def __init__ (self , value , name ):
8- object .__setattr__ (self , ' value' , value )
9- object .__setattr__ (self , ' name' , name )
8+ object .__setattr__ (self , " value" , value )
9+ object .__setattr__ (self , " name" , name )
1010
1111 def __repr__ (self ):
1212 return f"{ self .name } : { self .value } "
@@ -30,30 +30,30 @@ def __new__(cls, name=None, names=None):
3030 if cls is not Enum :
3131 return cls ._lookup (name )
3232
33- # Scenario 2: Functional API (e.g., Enum(' Color' , {' RED' : 1}))
33+ # Scenario 2: Functional API (e.g., Enum(" Color" , {" RED" : 1}))
3434 return super (Enum , cls ).__new__ (cls )
3535
3636 def __init__ (self , name = None , names = None ):
37- if hasattr (self , ' _initialized' ):
37+ if hasattr (self , " _initialized" ):
3838 return
3939
4040 # 1. Convert class-level attributes (constants) to EnumValue objects
4141 self ._scan_class_attrs ()
4242
43- # Support Functional API: Enum(' Name' , {' KEY' : VALUE})
43+ # Support Functional API: Enum(" Name" , {" KEY" : VALUE})
4444 if name is not None and isinstance (names , dict ):
4545 for key , value in names .items ():
4646 # Prevent addition if the key already exists
4747 if not hasattr (self , key ):
4848 self ._update (key , value )
4949
50- object .__setattr__ (self , ' _initialized' , True )
50+ object .__setattr__ (self , " _initialized" , True )
5151
5252 @classmethod
5353 def _lookup (cls , value ):
5454 # Finds an EnumValue by its raw value
5555 for key in dir (cls ):
56- if key .startswith ('_' ):
56+ if key .startswith ("_" ):
5757 continue
5858 attr = getattr (cls , key )
5959 if isinstance (attr , EnumValue ) and (attr .value == value or attr .name == value ):
@@ -65,26 +65,25 @@ def _lookup(cls, value):
6565
6666 @classmethod
6767 def __iter__ (cls ):
68- if ' _initialized' not in cls .__dict__ :
68+ if " _initialized" not in cls .__dict__ :
6969 cls ._scan_class_attrs ()
70- setattr (cls , ' _initialized' , True )
70+ setattr (cls , " _initialized" , True )
7171
7272 for key in dir (cls ):
73- if key .startswith ('_' ):
73+ if key .startswith ("_" ):
7474 continue
7575 attr = getattr (cls , key )
7676 if isinstance (attr , EnumValue ):
7777 yield attr
7878
7979 @classmethod
8080 def list (cls ):
81- if ' _initialized' not in cls .__dict__ :
81+ if " _initialized" not in cls .__dict__ :
8282 cls ._scan_class_attrs ()
83- setattr (cls , ' _initialized' , True )
83+ setattr (cls , " _initialized" , True )
8484
8585 # Returns a list of all members
86- return [getattr (cls , key ) for key in dir (cls )
87- if isinstance (getattr (cls , key ), EnumValue )]
86+ return [getattr (cls , key ) for key in dir (cls ) if isinstance (getattr (cls , key ), EnumValue )]
8887
8988 @classmethod
9089 def _update (cls , key , value ):
@@ -94,10 +93,10 @@ def _update(cls, key, value):
9493 def _scan_class_attrs (cls ):
9594 # Converts static class attributes into EnumValue objects
9695 # List of methods and internal names that should not be converted
97- ignored = (' is_value' , ' list' )
96+ ignored = (" is_value" , " list" )
9897 for key in dir (cls ):
9998 # Skip internal names and methods
100- if key .startswith ('_' ) or key in ignored :
99+ if key .startswith ("_" ) or key in ignored :
101100 continue
102101
103102 value = getattr (cls , key )
@@ -111,23 +110,23 @@ def is_value(self, value):
111110 def __repr__ (self ):
112111 # Supports the condition: obj == eval(repr(obj))
113112 members = {member .name : member .value for member in self }
114- if self .__class__ .__name__ == ' Enum' :
113+ if self .__class__ .__name__ == " Enum" :
115114 return f"Enum(name='Enum', names={ members } )"
116- # Return a string like: Name(names={' KEY1' : VALUE1, ' KEY2' : VALUE2, ..})
115+ # Return a string like: Name(names={" KEY1" : VALUE1, " KEY2" : VALUE2, ..})
117116 return f"{ self .__class__ .__name__ } (names={ members } )"
118117
119118 def __call__ (self , value ):
120- if not hasattr (self , ' _initialized' ):
119+ if not hasattr (self , " _initialized" ):
121120 self ._scan_class_attrs ()
122- object .__setattr__ (self , ' _initialized' , True )
121+ object .__setattr__ (self , " _initialized" , True )
123122
124123 for member in self :
125124 if member .value == value or member .name == value :
126125 return member
127126 raise AttributeError (f"{ value } is not in { self .__class__ .__name__ } " )
128127
129128 def __setattr__ (self , key , value ):
130- if hasattr (self , ' _initialized' ):
129+ if hasattr (self , " _initialized" ):
131130 raise AttributeError (f"Enum '{ self .__class__ .__name__ } ' is static" )
132131 super ().__setattr__ (key , value )
133132
@@ -145,7 +144,7 @@ def __eq__(self, other):
145144 return self .list () == other .list ()
146145
147146
148- if __name__ == ' __main__' :
147+ if __name__ == " __main__" :
149148 # --- Usage Example 1 ---
150149 # Standard Class Definition
151150 class Color (Enum ):
@@ -169,14 +168,14 @@ class Color(Enum):
169168 print (f"RED: Name={ c .RED .name } , Value={ c .RED .value } , EnumValue={ c .RED } , Call={ c .RED ()} " )
170169
171170 # Assertions
172- assert c .RED .name == ' RED'
171+ assert c .RED .name == " RED"
173172 assert c .RED .value == 1
174173 assert c .RED == 1
175174 assert c .RED () == 1
176175
177176 # Reverse Lookup via instance call
178- # print(f"c(1) lookup object: {c(1)}, Name={c(1).name}, value={c(1).value}") # RED
179- assert c (1 ).name == ' RED'
177+ print (f"c(1) lookup object: { c (1 )} , Name={ c (1 ).name } , value={ c (1 ).value } " ) # RED
178+ assert c (1 ).name == " RED"
180179 assert c (1 ).value == 1
181180 assert c (1 ) == 1
182181
@@ -242,6 +241,6 @@ class Status(Enum):
242241 state = eval ("Enum(name='State', names={'ON':1, 'OFF':2})" )
243242 print (f"Functional Enum instance (state): { state } " )
244243 assert state .ON == 1
245- assert state .ON .name == 'ON'
244+ assert state .ON .name == "ON"
246245
247246 print ("\n All tests passed successfully!" )
0 commit comments