Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: enum.py
import sys
[0] Fix | Delete
from types import MappingProxyType, DynamicClassAttribute
[1] Fix | Delete
[2] Fix | Delete
[3] Fix | Delete
__all__ = [
[4] Fix | Delete
'EnumMeta',
[5] Fix | Delete
'Enum', 'IntEnum', 'Flag', 'IntFlag',
[6] Fix | Delete
'auto', 'unique',
[7] Fix | Delete
]
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
def _is_descriptor(obj):
[11] Fix | Delete
"""
[12] Fix | Delete
Returns True if obj is a descriptor, False otherwise.
[13] Fix | Delete
"""
[14] Fix | Delete
return (
[15] Fix | Delete
hasattr(obj, '__get__') or
[16] Fix | Delete
hasattr(obj, '__set__') or
[17] Fix | Delete
hasattr(obj, '__delete__')
[18] Fix | Delete
)
[19] Fix | Delete
[20] Fix | Delete
def _is_dunder(name):
[21] Fix | Delete
"""
[22] Fix | Delete
Returns True if a __dunder__ name, False otherwise.
[23] Fix | Delete
"""
[24] Fix | Delete
return (
[25] Fix | Delete
len(name) > 4 and
[26] Fix | Delete
name[:2] == name[-2:] == '__' and
[27] Fix | Delete
name[2] != '_' and
[28] Fix | Delete
name[-3] != '_'
[29] Fix | Delete
)
[30] Fix | Delete
[31] Fix | Delete
def _is_sunder(name):
[32] Fix | Delete
"""
[33] Fix | Delete
Returns True if a _sunder_ name, False otherwise.
[34] Fix | Delete
"""
[35] Fix | Delete
return (
[36] Fix | Delete
len(name) > 2 and
[37] Fix | Delete
name[0] == name[-1] == '_' and
[38] Fix | Delete
name[1:2] != '_' and
[39] Fix | Delete
name[-2:-1] != '_'
[40] Fix | Delete
)
[41] Fix | Delete
[42] Fix | Delete
def _is_private(cls_name, name):
[43] Fix | Delete
# do not use `re` as `re` imports `enum`
[44] Fix | Delete
pattern = '_%s__' % (cls_name, )
[45] Fix | Delete
if (
[46] Fix | Delete
len(name) >= 5
[47] Fix | Delete
and name.startswith(pattern)
[48] Fix | Delete
and name[len(pattern)] != '_'
[49] Fix | Delete
and (name[-1] != '_' or name[-2] != '_')
[50] Fix | Delete
):
[51] Fix | Delete
return True
[52] Fix | Delete
else:
[53] Fix | Delete
return False
[54] Fix | Delete
[55] Fix | Delete
def _make_class_unpicklable(cls):
[56] Fix | Delete
"""
[57] Fix | Delete
Make the given class un-picklable.
[58] Fix | Delete
"""
[59] Fix | Delete
def _break_on_call_reduce(self, proto):
[60] Fix | Delete
raise TypeError('%r cannot be pickled' % self)
[61] Fix | Delete
cls.__reduce_ex__ = _break_on_call_reduce
[62] Fix | Delete
cls.__module__ = '<unknown>'
[63] Fix | Delete
[64] Fix | Delete
_auto_null = object()
[65] Fix | Delete
class auto:
[66] Fix | Delete
"""
[67] Fix | Delete
Instances are replaced with an appropriate value in Enum class suites.
[68] Fix | Delete
"""
[69] Fix | Delete
value = _auto_null
[70] Fix | Delete
[71] Fix | Delete
[72] Fix | Delete
class _EnumDict(dict):
[73] Fix | Delete
"""
[74] Fix | Delete
Track enum member order and ensure member names are not reused.
[75] Fix | Delete
[76] Fix | Delete
EnumMeta will use the names found in self._member_names as the
[77] Fix | Delete
enumeration member names.
[78] Fix | Delete
"""
[79] Fix | Delete
def __init__(self):
[80] Fix | Delete
super().__init__()
[81] Fix | Delete
self._member_names = []
[82] Fix | Delete
self._last_values = []
[83] Fix | Delete
self._ignore = []
[84] Fix | Delete
self._auto_called = False
[85] Fix | Delete
[86] Fix | Delete
def __setitem__(self, key, value):
[87] Fix | Delete
"""
[88] Fix | Delete
Changes anything not dundered or not a descriptor.
[89] Fix | Delete
[90] Fix | Delete
If an enum member name is used twice, an error is raised; duplicate
[91] Fix | Delete
values are not checked for.
[92] Fix | Delete
[93] Fix | Delete
Single underscore (sunder) names are reserved.
[94] Fix | Delete
"""
[95] Fix | Delete
if _is_private(self._cls_name, key):
[96] Fix | Delete
import warnings
[97] Fix | Delete
warnings.warn(
[98] Fix | Delete
"private variables, such as %r, will be normal attributes in 3.10"
[99] Fix | Delete
% (key, ),
[100] Fix | Delete
DeprecationWarning,
[101] Fix | Delete
stacklevel=2,
[102] Fix | Delete
)
[103] Fix | Delete
if _is_sunder(key):
[104] Fix | Delete
if key not in (
[105] Fix | Delete
'_order_', '_create_pseudo_member_',
[106] Fix | Delete
'_generate_next_value_', '_missing_', '_ignore_',
[107] Fix | Delete
):
[108] Fix | Delete
raise ValueError('_names_ are reserved for future Enum use')
[109] Fix | Delete
if key == '_generate_next_value_':
[110] Fix | Delete
# check if members already defined as auto()
[111] Fix | Delete
if self._auto_called:
[112] Fix | Delete
raise TypeError("_generate_next_value_ must be defined before members")
[113] Fix | Delete
setattr(self, '_generate_next_value', value)
[114] Fix | Delete
elif key == '_ignore_':
[115] Fix | Delete
if isinstance(value, str):
[116] Fix | Delete
value = value.replace(',',' ').split()
[117] Fix | Delete
else:
[118] Fix | Delete
value = list(value)
[119] Fix | Delete
self._ignore = value
[120] Fix | Delete
already = set(value) & set(self._member_names)
[121] Fix | Delete
if already:
[122] Fix | Delete
raise ValueError(
[123] Fix | Delete
'_ignore_ cannot specify already set names: %r'
[124] Fix | Delete
% (already, )
[125] Fix | Delete
)
[126] Fix | Delete
elif _is_dunder(key):
[127] Fix | Delete
if key == '__order__':
[128] Fix | Delete
key = '_order_'
[129] Fix | Delete
elif key in self._member_names:
[130] Fix | Delete
# descriptor overwriting an enum?
[131] Fix | Delete
raise TypeError('Attempted to reuse key: %r' % key)
[132] Fix | Delete
elif key in self._ignore:
[133] Fix | Delete
pass
[134] Fix | Delete
elif not _is_descriptor(value):
[135] Fix | Delete
if key in self:
[136] Fix | Delete
# enum overwriting a descriptor?
[137] Fix | Delete
raise TypeError('%r already defined as: %r' % (key, self[key]))
[138] Fix | Delete
if isinstance(value, auto):
[139] Fix | Delete
if value.value == _auto_null:
[140] Fix | Delete
value.value = self._generate_next_value(
[141] Fix | Delete
key,
[142] Fix | Delete
1,
[143] Fix | Delete
len(self._member_names),
[144] Fix | Delete
self._last_values[:],
[145] Fix | Delete
)
[146] Fix | Delete
self._auto_called = True
[147] Fix | Delete
value = value.value
[148] Fix | Delete
self._member_names.append(key)
[149] Fix | Delete
self._last_values.append(value)
[150] Fix | Delete
super().__setitem__(key, value)
[151] Fix | Delete
[152] Fix | Delete
[153] Fix | Delete
# Dummy value for Enum as EnumMeta explicitly checks for it, but of course
[154] Fix | Delete
# until EnumMeta finishes running the first time the Enum class doesn't exist.
[155] Fix | Delete
# This is also why there are checks in EnumMeta like `if Enum is not None`
[156] Fix | Delete
Enum = None
[157] Fix | Delete
[158] Fix | Delete
class EnumMeta(type):
[159] Fix | Delete
"""
[160] Fix | Delete
Metaclass for Enum
[161] Fix | Delete
"""
[162] Fix | Delete
@classmethod
[163] Fix | Delete
def __prepare__(metacls, cls, bases, **kwds):
[164] Fix | Delete
# check that previous enum members do not exist
[165] Fix | Delete
metacls._check_for_existing_members(cls, bases)
[166] Fix | Delete
# create the namespace dict
[167] Fix | Delete
enum_dict = _EnumDict()
[168] Fix | Delete
enum_dict._cls_name = cls
[169] Fix | Delete
# inherit previous flags and _generate_next_value_ function
[170] Fix | Delete
member_type, first_enum = metacls._get_mixins_(cls, bases)
[171] Fix | Delete
if first_enum is not None:
[172] Fix | Delete
enum_dict['_generate_next_value_'] = getattr(
[173] Fix | Delete
first_enum, '_generate_next_value_', None,
[174] Fix | Delete
)
[175] Fix | Delete
return enum_dict
[176] Fix | Delete
[177] Fix | Delete
def __new__(metacls, cls, bases, classdict, **kwds):
[178] Fix | Delete
# an Enum class is final once enumeration items have been defined; it
[179] Fix | Delete
# cannot be mixed with other types (int, float, etc.) if it has an
[180] Fix | Delete
# inherited __new__ unless a new __new__ is defined (or the resulting
[181] Fix | Delete
# class will fail).
[182] Fix | Delete
#
[183] Fix | Delete
# remove any keys listed in _ignore_
[184] Fix | Delete
classdict.setdefault('_ignore_', []).append('_ignore_')
[185] Fix | Delete
ignore = classdict['_ignore_']
[186] Fix | Delete
for key in ignore:
[187] Fix | Delete
classdict.pop(key, None)
[188] Fix | Delete
member_type, first_enum = metacls._get_mixins_(cls, bases)
[189] Fix | Delete
__new__, save_new, use_args = metacls._find_new_(
[190] Fix | Delete
classdict, member_type, first_enum,
[191] Fix | Delete
)
[192] Fix | Delete
[193] Fix | Delete
# save enum items into separate mapping so they don't get baked into
[194] Fix | Delete
# the new class
[195] Fix | Delete
enum_members = {k: classdict[k] for k in classdict._member_names}
[196] Fix | Delete
for name in classdict._member_names:
[197] Fix | Delete
del classdict[name]
[198] Fix | Delete
[199] Fix | Delete
# adjust the sunders
[200] Fix | Delete
_order_ = classdict.pop('_order_', None)
[201] Fix | Delete
[202] Fix | Delete
# check for illegal enum names (any others?)
[203] Fix | Delete
invalid_names = set(enum_members) & {'mro', ''}
[204] Fix | Delete
if invalid_names:
[205] Fix | Delete
raise ValueError('Invalid enum member name: {0}'.format(
[206] Fix | Delete
','.join(invalid_names)))
[207] Fix | Delete
[208] Fix | Delete
# create a default docstring if one has not been provided
[209] Fix | Delete
if '__doc__' not in classdict:
[210] Fix | Delete
classdict['__doc__'] = 'An enumeration.'
[211] Fix | Delete
[212] Fix | Delete
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
[213] Fix | Delete
enum_class._member_names_ = [] # names in definition order
[214] Fix | Delete
enum_class._member_map_ = {} # name->value map
[215] Fix | Delete
enum_class._member_type_ = member_type
[216] Fix | Delete
[217] Fix | Delete
# save DynamicClassAttribute attributes from super classes so we know
[218] Fix | Delete
# if we can take the shortcut of storing members in the class dict
[219] Fix | Delete
dynamic_attributes = {
[220] Fix | Delete
k for c in enum_class.mro()
[221] Fix | Delete
for k, v in c.__dict__.items()
[222] Fix | Delete
if isinstance(v, DynamicClassAttribute)
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
# Reverse value->name map for hashable values.
[226] Fix | Delete
enum_class._value2member_map_ = {}
[227] Fix | Delete
[228] Fix | Delete
# If a custom type is mixed into the Enum, and it does not know how
[229] Fix | Delete
# to pickle itself, pickle.dumps will succeed but pickle.loads will
[230] Fix | Delete
# fail. Rather than have the error show up later and possibly far
[231] Fix | Delete
# from the source, sabotage the pickle protocol for this class so
[232] Fix | Delete
# that pickle.dumps also fails.
[233] Fix | Delete
#
[234] Fix | Delete
# However, if the new class implements its own __reduce_ex__, do not
[235] Fix | Delete
# sabotage -- it's on them to make sure it works correctly. We use
[236] Fix | Delete
# __reduce_ex__ instead of any of the others as it is preferred by
[237] Fix | Delete
# pickle over __reduce__, and it handles all pickle protocols.
[238] Fix | Delete
if '__reduce_ex__' not in classdict:
[239] Fix | Delete
if member_type is not object:
[240] Fix | Delete
methods = ('__getnewargs_ex__', '__getnewargs__',
[241] Fix | Delete
'__reduce_ex__', '__reduce__')
[242] Fix | Delete
if not any(m in member_type.__dict__ for m in methods):
[243] Fix | Delete
if '__new__' in classdict:
[244] Fix | Delete
# too late, sabotage
[245] Fix | Delete
_make_class_unpicklable(enum_class)
[246] Fix | Delete
else:
[247] Fix | Delete
# final attempt to verify that pickling would work:
[248] Fix | Delete
# travel mro until __new__ is found, checking for
[249] Fix | Delete
# __reduce__ and friends along the way -- if any of them
[250] Fix | Delete
# are found before/when __new__ is found, pickling should
[251] Fix | Delete
# work
[252] Fix | Delete
sabotage = None
[253] Fix | Delete
for chain in bases:
[254] Fix | Delete
for base in chain.__mro__:
[255] Fix | Delete
if base is object:
[256] Fix | Delete
continue
[257] Fix | Delete
elif any(m in base.__dict__ for m in methods):
[258] Fix | Delete
# found one, we're good
[259] Fix | Delete
sabotage = False
[260] Fix | Delete
break
[261] Fix | Delete
elif '__new__' in base.__dict__:
[262] Fix | Delete
# not good
[263] Fix | Delete
sabotage = True
[264] Fix | Delete
break
[265] Fix | Delete
if sabotage is not None:
[266] Fix | Delete
break
[267] Fix | Delete
if sabotage:
[268] Fix | Delete
_make_class_unpicklable(enum_class)
[269] Fix | Delete
# instantiate them, checking for duplicates as we go
[270] Fix | Delete
# we instantiate first instead of checking for duplicates first in case
[271] Fix | Delete
# a custom __new__ is doing something funky with the values -- such as
[272] Fix | Delete
# auto-numbering ;)
[273] Fix | Delete
for member_name in classdict._member_names:
[274] Fix | Delete
value = enum_members[member_name]
[275] Fix | Delete
if not isinstance(value, tuple):
[276] Fix | Delete
args = (value, )
[277] Fix | Delete
else:
[278] Fix | Delete
args = value
[279] Fix | Delete
if member_type is tuple: # special case for tuple enums
[280] Fix | Delete
args = (args, ) # wrap it one more time
[281] Fix | Delete
if not use_args:
[282] Fix | Delete
enum_member = __new__(enum_class)
[283] Fix | Delete
if not hasattr(enum_member, '_value_'):
[284] Fix | Delete
enum_member._value_ = value
[285] Fix | Delete
else:
[286] Fix | Delete
enum_member = __new__(enum_class, *args)
[287] Fix | Delete
if not hasattr(enum_member, '_value_'):
[288] Fix | Delete
if member_type is object:
[289] Fix | Delete
enum_member._value_ = value
[290] Fix | Delete
else:
[291] Fix | Delete
enum_member._value_ = member_type(*args)
[292] Fix | Delete
value = enum_member._value_
[293] Fix | Delete
enum_member._name_ = member_name
[294] Fix | Delete
enum_member.__objclass__ = enum_class
[295] Fix | Delete
enum_member.__init__(*args)
[296] Fix | Delete
# If another member with the same value was already defined, the
[297] Fix | Delete
# new member becomes an alias to the existing one.
[298] Fix | Delete
for name, canonical_member in enum_class._member_map_.items():
[299] Fix | Delete
if canonical_member._value_ == enum_member._value_:
[300] Fix | Delete
enum_member = canonical_member
[301] Fix | Delete
break
[302] Fix | Delete
else:
[303] Fix | Delete
# Aliases don't appear in member names (only in __members__).
[304] Fix | Delete
enum_class._member_names_.append(member_name)
[305] Fix | Delete
# performance boost for any member that would not shadow
[306] Fix | Delete
# a DynamicClassAttribute
[307] Fix | Delete
if member_name not in dynamic_attributes:
[308] Fix | Delete
setattr(enum_class, member_name, enum_member)
[309] Fix | Delete
# now add to _member_map_
[310] Fix | Delete
enum_class._member_map_[member_name] = enum_member
[311] Fix | Delete
try:
[312] Fix | Delete
# This may fail if value is not hashable. We can't add the value
[313] Fix | Delete
# to the map, and by-value lookups for this value will be
[314] Fix | Delete
# linear.
[315] Fix | Delete
enum_class._value2member_map_[value] = enum_member
[316] Fix | Delete
except TypeError:
[317] Fix | Delete
pass
[318] Fix | Delete
[319] Fix | Delete
# double check that repr and friends are not the mixin's or various
[320] Fix | Delete
# things break (such as pickle)
[321] Fix | Delete
# however, if the method is defined in the Enum itself, don't replace
[322] Fix | Delete
# it
[323] Fix | Delete
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
[324] Fix | Delete
if name in classdict:
[325] Fix | Delete
continue
[326] Fix | Delete
class_method = getattr(enum_class, name)
[327] Fix | Delete
obj_method = getattr(member_type, name, None)
[328] Fix | Delete
enum_method = getattr(first_enum, name, None)
[329] Fix | Delete
if obj_method is not None and obj_method is class_method:
[330] Fix | Delete
setattr(enum_class, name, enum_method)
[331] Fix | Delete
[332] Fix | Delete
# replace any other __new__ with our own (as long as Enum is not None,
[333] Fix | Delete
# anyway) -- again, this is to support pickle
[334] Fix | Delete
if Enum is not None:
[335] Fix | Delete
# if the user defined their own __new__, save it before it gets
[336] Fix | Delete
# clobbered in case they subclass later
[337] Fix | Delete
if save_new:
[338] Fix | Delete
enum_class.__new_member__ = __new__
[339] Fix | Delete
enum_class.__new__ = Enum.__new__
[340] Fix | Delete
[341] Fix | Delete
# py3 support for definition order (helps keep py2/py3 code in sync)
[342] Fix | Delete
if _order_ is not None:
[343] Fix | Delete
if isinstance(_order_, str):
[344] Fix | Delete
_order_ = _order_.replace(',', ' ').split()
[345] Fix | Delete
if _order_ != enum_class._member_names_:
[346] Fix | Delete
raise TypeError('member order does not match _order_')
[347] Fix | Delete
[348] Fix | Delete
return enum_class
[349] Fix | Delete
[350] Fix | Delete
def __bool__(self):
[351] Fix | Delete
"""
[352] Fix | Delete
classes/types should always be True.
[353] Fix | Delete
"""
[354] Fix | Delete
return True
[355] Fix | Delete
[356] Fix | Delete
def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
[357] Fix | Delete
"""
[358] Fix | Delete
Either returns an existing member, or creates a new enum class.
[359] Fix | Delete
[360] Fix | Delete
This method is used both when an enum class is given a value to match
[361] Fix | Delete
to an enumeration member (i.e. Color(3)) and for the functional API
[362] Fix | Delete
(i.e. Color = Enum('Color', names='RED GREEN BLUE')).
[363] Fix | Delete
[364] Fix | Delete
When used for the functional API:
[365] Fix | Delete
[366] Fix | Delete
`value` will be the name of the new class.
[367] Fix | Delete
[368] Fix | Delete
`names` should be either a string of white-space/comma delimited names
[369] Fix | Delete
(values will start at `start`), or an iterator/mapping of name, value pairs.
[370] Fix | Delete
[371] Fix | Delete
`module` should be set to the module this class is being created in;
[372] Fix | Delete
if it is not set, an attempt to find that module will be made, but if
[373] Fix | Delete
it fails the class will not be picklable.
[374] Fix | Delete
[375] Fix | Delete
`qualname` should be set to the actual location this class can be found
[376] Fix | Delete
at in its module; by default it is set to the global scope. If this is
[377] Fix | Delete
not correct, unpickling will fail in some circumstances.
[378] Fix | Delete
[379] Fix | Delete
`type`, if set, will be mixed in as the first base class.
[380] Fix | Delete
"""
[381] Fix | Delete
if names is None: # simple value lookup
[382] Fix | Delete
return cls.__new__(cls, value)
[383] Fix | Delete
# otherwise, functional API: we're creating a new Enum type
[384] Fix | Delete
return cls._create_(
[385] Fix | Delete
value,
[386] Fix | Delete
names,
[387] Fix | Delete
module=module,
[388] Fix | Delete
qualname=qualname,
[389] Fix | Delete
type=type,
[390] Fix | Delete
start=start,
[391] Fix | Delete
)
[392] Fix | Delete
[393] Fix | Delete
def __contains__(cls, member):
[394] Fix | Delete
if not isinstance(member, Enum):
[395] Fix | Delete
raise TypeError(
[396] Fix | Delete
"unsupported operand type(s) for 'in': '%s' and '%s'" % (
[397] Fix | Delete
type(member).__qualname__, cls.__class__.__qualname__))
[398] Fix | Delete
return isinstance(member, cls) and member._name_ in cls._member_map_
[399] Fix | Delete
[400] Fix | Delete
def __delattr__(cls, attr):
[401] Fix | Delete
# nicer error message when someone tries to delete an attribute
[402] Fix | Delete
# (see issue19025).
[403] Fix | Delete
if attr in cls._member_map_:
[404] Fix | Delete
raise AttributeError("%s: cannot delete Enum member." % cls.__name__)
[405] Fix | Delete
super().__delattr__(attr)
[406] Fix | Delete
[407] Fix | Delete
def __dir__(self):
[408] Fix | Delete
return (
[409] Fix | Delete
['__class__', '__doc__', '__members__', '__module__']
[410] Fix | Delete
+ self._member_names_
[411] Fix | Delete
)
[412] Fix | Delete
[413] Fix | Delete
def __getattr__(cls, name):
[414] Fix | Delete
"""
[415] Fix | Delete
Return the enum member matching `name`
[416] Fix | Delete
[417] Fix | Delete
We use __getattr__ instead of descriptors or inserting into the enum
[418] Fix | Delete
class' __dict__ in order to support `name` and `value` being both
[419] Fix | Delete
properties for enum members (which live in the class' __dict__) and
[420] Fix | Delete
enum members themselves.
[421] Fix | Delete
"""
[422] Fix | Delete
if _is_dunder(name):
[423] Fix | Delete
raise AttributeError(name)
[424] Fix | Delete
try:
[425] Fix | Delete
return cls._member_map_[name]
[426] Fix | Delete
except KeyError:
[427] Fix | Delete
raise AttributeError(name) from None
[428] Fix | Delete
[429] Fix | Delete
def __getitem__(cls, name):
[430] Fix | Delete
return cls._member_map_[name]
[431] Fix | Delete
[432] Fix | Delete
def __iter__(cls):
[433] Fix | Delete
"""
[434] Fix | Delete
Returns members in definition order.
[435] Fix | Delete
"""
[436] Fix | Delete
return (cls._member_map_[name] for name in cls._member_names_)
[437] Fix | Delete
[438] Fix | Delete
def __len__(cls):
[439] Fix | Delete
return len(cls._member_names_)
[440] Fix | Delete
[441] Fix | Delete
@property
[442] Fix | Delete
def __members__(cls):
[443] Fix | Delete
"""
[444] Fix | Delete
Returns a mapping of member name->value.
[445] Fix | Delete
[446] Fix | Delete
This mapping lists all enum members, including aliases. Note that this
[447] Fix | Delete
is a read-only view of the internal mapping.
[448] Fix | Delete
"""
[449] Fix | Delete
return MappingProxyType(cls._member_map_)
[450] Fix | Delete
[451] Fix | Delete
def __repr__(cls):
[452] Fix | Delete
return "<enum %r>" % cls.__name__
[453] Fix | Delete
[454] Fix | Delete
def __reversed__(cls):
[455] Fix | Delete
"""
[456] Fix | Delete
Returns members in reverse definition order.
[457] Fix | Delete
"""
[458] Fix | Delete
return (cls._member_map_[name] for name in reversed(cls._member_names_))
[459] Fix | Delete
[460] Fix | Delete
def __setattr__(cls, name, value):
[461] Fix | Delete
"""
[462] Fix | Delete
Block attempts to reassign Enum members.
[463] Fix | Delete
[464] Fix | Delete
A simple assignment to the class namespace only changes one of the
[465] Fix | Delete
several possible ways to get an Enum member from the Enum class,
[466] Fix | Delete
resulting in an inconsistent Enumeration.
[467] Fix | Delete
"""
[468] Fix | Delete
member_map = cls.__dict__.get('_member_map_', {})
[469] Fix | Delete
if name in member_map:
[470] Fix | Delete
raise AttributeError('Cannot reassign members.')
[471] Fix | Delete
super().__setattr__(name, value)
[472] Fix | Delete
[473] Fix | Delete
def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1):
[474] Fix | Delete
"""
[475] Fix | Delete
Convenience method to create a new Enum class.
[476] Fix | Delete
[477] Fix | Delete
`names` can be:
[478] Fix | Delete
[479] Fix | Delete
* A string containing member names, separated either with spaces or
[480] Fix | Delete
commas. Values are incremented by 1 from `start`.
[481] Fix | Delete
* An iterable of member names. Values are incremented by 1 from `start`.
[482] Fix | Delete
* An iterable of (member name, value) pairs.
[483] Fix | Delete
* A mapping of member name -> value pairs.
[484] Fix | Delete
"""
[485] Fix | Delete
metacls = cls.__class__
[486] Fix | Delete
bases = (cls, ) if type is None else (type, cls)
[487] Fix | Delete
_, first_enum = cls._get_mixins_(cls, bases)
[488] Fix | Delete
classdict = metacls.__prepare__(class_name, bases)
[489] Fix | Delete
[490] Fix | Delete
# special processing needed for names?
[491] Fix | Delete
if isinstance(names, str):
[492] Fix | Delete
names = names.replace(',', ' ').split()
[493] Fix | Delete
if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
[494] Fix | Delete
original_names, names = names, []
[495] Fix | Delete
last_values = []
[496] Fix | Delete
for count, name in enumerate(original_names):
[497] Fix | Delete
value = first_enum._generate_next_value_(name, start, count, last_values[:])
[498] Fix | Delete
last_values.append(value)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function