本文整理汇总了Python中enum.Enum.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python Enum.__new__方法的具体用法?Python Enum.__new__怎么用?Python Enum.__new__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类enum.Enum
的用法示例。
在下文中一共展示了Enum.__new__方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_duplicate_values_give_unique_enum_items
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_duplicate_values_give_unique_enum_items(self):
class AutoNumber(Enum):
__order__ = 'enum_m enum_d enum_y'
enum_m = ()
enum_d = ()
enum_y = ()
def __new__(cls):
value = len(cls.__members__) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
def __int__(self):
return int(self._value_)
self.assertEqual(int(AutoNumber.enum_d), 2)
self.assertEqual(AutoNumber.enum_y.value, 3)
self.assertTrue(AutoNumber(1) is AutoNumber.enum_m)
self.assertEqual(
list(AutoNumber),
[AutoNumber.enum_m, AutoNumber.enum_d, AutoNumber.enum_y],
)
示例2: test_inherited_new_from_enhanced_enum
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_inherited_new_from_enhanced_enum(self):
class AutoNumber2(Enum):
def __new__(cls):
value = len(cls.__members__) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
def __int__(self):
return int(self._value_)
class Color(AutoNumber2):
_order_ = 'red green blue'
red = ()
green = ()
blue = ()
self.assertEqual(len(Color), 3, "wrong number of elements: %d (should be %d)" % (len(Color), 3))
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
if pyver >= 3.0:
self.assertEqual(list(map(int, Color)), [1, 2, 3])
示例3: test_inherited_new_from_enhanced_enum
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_inherited_new_from_enhanced_enum(self):
class AutoNumber2(Enum):
def __new__(cls):
value = len(cls.__members__) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
def __int__(self):
return int(self._value_)
class Color(AutoNumber2):
__order__ = 'red green blue'
red = ()
green = ()
blue = ()
self.assertEqual(len(Color), 3, "wrong number of elements: %d (should be %d)" % (len(Color), 3))
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
if pyver >= 3.0:
self.assertEqual(list(map(int, Color)), [1, 2, 3])
示例4: test_duplicate_values_give_unique_enum_items
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_duplicate_values_give_unique_enum_items(self):
class AutoNumber(Enum):
first = ()
second = ()
third = ()
def __new__(cls):
value = len(cls.__members__) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
def __int__(self):
return int(self._value_)
self.assertEqual(
list(AutoNumber),
[AutoNumber.first, AutoNumber.second, AutoNumber.third],
)
self.assertEqual(int(AutoNumber.second), 2)
self.assertEqual(AutoNumber.third.value, 3)
self.assertIs(AutoNumber(1), AutoNumber.first)
示例5: test_nonhash_value
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_nonhash_value(self):
class AutoNumberInAList(Enum):
def __new__(cls):
value = [len(cls.__members__) + 1]
obj = object.__new__(cls)
obj._value_ = value
return obj
class ColorInAList(AutoNumberInAList):
red = ()
green = ()
blue = ()
self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue])
for enum, value in zip(ColorInAList, range(3)):
value += 1
self.assertEqual(enum.value, [value])
self.assertIs(ColorInAList([value]), enum)
示例6: test_multiple_mixin_mro
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_multiple_mixin_mro(self):
class auto_enum(EnumMeta):
def __new__(metacls, cls, bases, classdict):
original_dict = classdict
classdict = enum._EnumDict()
for k, v in original_dict.items():
classdict[k] = v
temp = type(classdict)()
names = set(classdict._member_names)
i = 0
for k in classdict._member_names:
v = classdict[k]
if v == ():
v = i
else:
i = v
i += 1
temp[k] = v
for k, v in classdict.items():
if k not in names:
temp[k] = v
return super(auto_enum, metacls).__new__(
metacls, cls, bases, temp)
AutoNumberedEnum = auto_enum('AutoNumberedEnum', (Enum,), {})
AutoIntEnum = auto_enum('AutoIntEnum', (IntEnum,), {})
class TestAutoNumber(AutoNumberedEnum):
a = ()
b = 3
c = ()
class TestAutoInt(AutoIntEnum):
a = ()
b = 3
c = ()
示例7: test_inherited_new_from_mixed_enum
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_inherited_new_from_mixed_enum(self):
class AutoNumber3(IntEnum):
def __new__(cls):
value = len(cls.__members__) + 1
obj = int.__new__(cls, value)
obj._value_ = value
return obj
class Color(AutoNumber3):
red = ()
green = ()
blue = ()
self.assertEqual(len(Color), 3, "wrong number of elements: %d (should be %d)" % (len(Color), 3))
Color.red
Color.green
Color.blue
示例8: test_nonhash_value
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_nonhash_value(self):
class AutoNumberInAList(Enum):
def __new__(cls):
value = [len(cls.__members__) + 1]
obj = object.__new__(cls)
obj._value_ = value
return obj
class ColorInAList(AutoNumberInAList):
_order_ = 'red green blue'
red = ()
green = ()
blue = ()
self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue])
self.assertEqual(ColorInAList.red.value, [1])
self.assertEqual(ColorInAList([1]), ColorInAList.red)
示例9: test_nonhash_value
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_nonhash_value(self):
class AutoNumberInAList(Enum):
def __new__(cls):
value = [len(cls.__members__) + 1]
obj = object.__new__(cls)
obj._value_ = value
return obj
class ColorInAList(AutoNumberInAList):
__order__ = 'red green blue'
red = ()
green = ()
blue = ()
self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue])
self.assertEqual(ColorInAList.red.value, [1])
self.assertEqual(ColorInAList([1]), ColorInAList.red)
示例10: test_multiple_mixin_mro
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_multiple_mixin_mro(self):
class auto_enum(type(Enum)):
def __new__(metacls, cls, bases, classdict):
temp = type(classdict)()
names = set(classdict._member_names)
i = 0
for k in classdict._member_names:
v = classdict[k]
if v is Ellipsis:
v = i
else:
i = v
i += 1
temp[k] = v
for k, v in classdict.items():
if k not in names:
temp[k] = v
return super(auto_enum, metacls).__new__(
metacls, cls, bases, temp)
class AutoNumberedEnum(Enum, metaclass=auto_enum):
pass
class AutoIntEnum(IntEnum, metaclass=auto_enum):
pass
class TestAutoNumber(AutoNumberedEnum):
a = ...
b = 3
c = ...
class TestAutoInt(AutoIntEnum):
a = ...
b = 3
c = ...
示例11: test_inherited_new_from_enhanced_enum
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_inherited_new_from_enhanced_enum(self):
class AutoNumber(Enum):
def __new__(cls):
value = len(cls.__members__) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
def __int__(self):
return int(self._value_)
class Color(AutoNumber):
red = ()
green = ()
blue = ()
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
self.assertEqual(list(map(int, Color)), [1, 2, 3])
示例12: test_inherited_new_from_mixed_enum
# 需要导入模块: from enum import Enum [as 别名]
# 或者: from enum.Enum import __new__ [as 别名]
def test_inherited_new_from_mixed_enum(self):
class AutoNumber(IntEnum):
def __new__(cls):
value = len(cls.__members__) + 1
obj = int.__new__(cls, value)
obj._value_ = value
return obj
class Color(AutoNumber):
red = ()
green = ()
blue = ()
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
self.assertEqual(list(map(int, Color)), [1, 2, 3])