當前位置: 首頁>>代碼示例>>Python>>正文


Python Enum.__new__方法代碼示例

本文整理匯總了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],
            ) 
開發者ID:typeintandem,項目名稱:tandem,代碼行數:22,代碼來源:test.py

示例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]) 
開發者ID:typeintandem,項目名稱:tandem,代碼行數:20,代碼來源:test.py

示例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]) 
開發者ID:aliyun,項目名稱:oss-ftp,代碼行數:20,代碼來源:test_enum.py

示例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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:21,代碼來源:test_enum.py

示例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) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_enum.py

示例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 = () 
開發者ID:typeintandem,項目名稱:tandem,代碼行數:39,代碼來源:test.py

示例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 
開發者ID:typeintandem,項目名稱:tandem,代碼行數:17,代碼來源:test.py

示例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) 
開發者ID:typeintandem,項目名稱:tandem,代碼行數:17,代碼來源:test.py

示例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) 
開發者ID:aliyun,項目名稱:oss-ftp,代碼行數:17,代碼來源:test_enum.py

示例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 = ... 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:37,代碼來源:test_enum.py

示例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]) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:17,代碼來源:test_enum.py

示例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]) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:15,代碼來源:test_enum.py


注:本文中的enum.Enum.__new__方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。