def __delattr__(cls, name): if name in cls._member_names_: raise AttributeError( f"{cls.__name__}: cannot delete Enum members.") if _is_dunder(name): for value in cls._member_map_: delattr(value, name) super().__delattr__(name)
def __setattr__(cls, name, value): if name in cls._member_names_: raise AttributeError( f"{cls.__name__}: cannot reassign Enum members.") if _is_dunder(name): for value in cls._member_map_: setattr(value, name, value) super().__setattr__(name, value)
def test_is_dunder(self): for s in ('__a__', '__aa__'): self.assertTrue(enum._is_dunder(s)) for s in ( 'a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_', '__', '___', '____', '_____', ): self.assertFalse(enum._is_dunder(s))
def __getattr__(cls, name): r""" Return the enum member matching `name`. """ if _is_dunder(name): raise AttributeError(name) try: return cls._member_map_[name] except KeyError: text = '"{}" does not have an attribute of "{}"'.format(cls.__name__,name) raise AttributeError(text) from None
def __getattr__(cls, name): r""" Return the enum member matching `name`. """ if _is_dunder(name): raise AttributeError(name) try: return cls._member_map_[name] except KeyError: text = '"{}" does not have an attribute of "{}"'.format( cls.__name__, name) raise AttributeError(text) from None
def __setitem__(self, key, value): """Changes anything not dundered or not a descriptor. If an enum member name is used twice, an error is raised; duplicate values are not checked for. Single underscore (sunder) names are reserved. """ if _is_sunder(key): if key not in { '_order_', '_create_pseudo_member_', '_generate_next_value_', '_missing_', '_ignore_', }: raise ValueError('_names_ are reserved for future Enum use') if key == '_generate_next_value_': setattr(self, '_generate_next_value', value) elif key == '_ignore_': if isinstance(value, str): value = value.replace(',', ' ').split() else: value = list(value) self._ignore = value already = set(value) & self.members.keys() if already: raise ValueError( '_ignore_ cannot specify already set names: %r' % (already, )) elif _is_dunder(key): if key == '__order__': key = '_order_' elif key in self.members: # descriptor overwriting an enum? raise TypeError('Attempted to reuse key: %r' % key) elif key in self._ignore: pass elif not _is_descriptor(value): if key in self: # enum overwriting a descriptor? raise TypeError('%r already defined as: %r' % (key, self[key])) if isinstance(value, auto): if value.value == _auto_null: value.value = self._generate_next_value( key, 1, len(self.members), list(self.members.values())) value = value.value self.members[key] = value dict.__setitem__(self, key, value)
def __getattr__(cls, name): # first test for empty element if name == DynamicEnumMeta.empty_attr: return EnumMeta.__getattr__( cls.__get_dynamic_enum_class( {DynamicEnumMeta.empty_attr: object()}), DynamicEnumMeta.empty_attr) # ignore dunder attributes elif _is_dunder(name): return EnumMeta.__getattr__(cls, name) # this avoids recursion enum_dict = cls.dynamic_enum_dict if name in enum_dict: return EnumMeta.__getattr__( cls.__get_dynamic_enum_class(enum_dict), name) else: return EnumMeta.__getattr__(cls, name)
def is_not_dunder(name: str) -> bool: return not _is_dunder(name)
def test_is_dunder(self): for s in ('__a__', '__aa__'): self.assertTrue(enum._is_dunder(s)) for s in ('a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_', '__', '___', '____', '_____',): self.assertFalse(enum._is_dunder(s))
def update_event(self, inp=-1): self.set_output_val(0, enum._is_dunder(self.input(0)))