ch25: changed MicroEnum to use __class_getitem__
This commit is contained in:
@@ -54,12 +54,10 @@ class MicroEnumMeta(type):
|
|||||||
def __prepare__(name, bases, **kwargs):
|
def __prepare__(name, bases, **kwargs):
|
||||||
return AutoFillDict()
|
return AutoFillDict()
|
||||||
|
|
||||||
def __getitem__(cls, key):
|
|
||||||
|
class MicroEnum(metaclass=MicroEnumMeta):
|
||||||
|
def __class_getitem__(cls, key):
|
||||||
for k, v in cls.__dict__.items():
|
for k, v in cls.__dict__.items():
|
||||||
if v == key:
|
if v == key:
|
||||||
return k
|
return k
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
|
|
||||||
class MicroEnum(metaclass=MicroEnumMeta):
|
|
||||||
pass
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ Testing ``KeyIsValueDict``::
|
|||||||
|
|
||||||
|
|
||||||
class KeyIsValueDict(dict):
|
class KeyIsValueDict(dict):
|
||||||
|
|
||||||
def __missing__(self, key):
|
def __missing__(self, key):
|
||||||
if key.startswith('__') and key.endswith('__'):
|
if key.startswith('__') and key.endswith('__'):
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|||||||
Reference in New Issue
Block a user