本文整理汇总了Python中enum.IntEnum方法的典型用法代码示例。如果您正苦于以下问题:Python enum.IntEnum方法的具体用法?Python enum.IntEnum怎么用?Python enum.IntEnum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类enum
的用法示例。
在下文中一共展示了enum.IntEnum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_programatic_function_type_from_subclass
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_programatic_function_type_from_subclass(self):
SummerMonth = IntEnum('SummerMonth', 'june july august')
lst = list(SummerMonth)
self.assertEqual(len(lst), len(SummerMonth))
self.assertEqual(len(SummerMonth), 3, SummerMonth)
self.assertEqual(
[SummerMonth.june, SummerMonth.july, SummerMonth.august],
lst,
)
for i, month in enumerate('june july august'.split()):
i += 1
e = SummerMonth(i)
self.assertEqual(e, i)
self.assertEqual(e.name, month)
self.assertTrue(e in SummerMonth)
self.assertTrue(type(e) is SummerMonth)
示例2: test_programatic_function_unicode_type_from_subclass
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_programatic_function_unicode_type_from_subclass(self):
SummerMonth = IntEnum('SummerMonth', unicode('june july august'))
lst = list(SummerMonth)
self.assertEqual(len(lst), len(SummerMonth))
self.assertEqual(len(SummerMonth), 3, SummerMonth)
self.assertEqual(
[SummerMonth.june, SummerMonth.july, SummerMonth.august],
lst,
)
for i, month in enumerate(unicode('june july august').split()):
i += 1
e = SummerMonth(i)
self.assertEqual(e, i)
self.assertEqual(e.name, month)
self.assertTrue(e in SummerMonth)
self.assertTrue(type(e) is SummerMonth)
示例3: test_introspection
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_introspection(self):
class Number(IntEnum):
one = 100
two = 200
self.assertTrue(Number.one._member_type_ is int)
self.assertTrue(Number._member_type_ is int)
class String(str, Enum):
yarn = 'soft'
rope = 'rough'
wire = 'hard'
self.assertTrue(String.yarn._member_type_ is str)
self.assertTrue(String._member_type_ is str)
class Plain(Enum):
vanilla = 'white'
one = 1
self.assertTrue(Plain.vanilla._member_type_ is object)
self.assertTrue(Plain._member_type_ is object)
示例4: check_net_address
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def check_net_address(addr, family):
"""Check a net address validity. Supported families are IPv4,
IPv6 and MAC addresses.
"""
import ipaddress # python >= 3.3 / requires "pip install ipaddress"
if enum and PY3:
assert isinstance(family, enum.IntEnum), family
if family == socket.AF_INET:
octs = [int(x) for x in addr.split('.')]
assert len(octs) == 4, addr
for num in octs:
assert 0 <= num <= 255, addr
if not PY3:
addr = unicode(addr)
ipaddress.IPv4Address(addr)
elif family == socket.AF_INET6:
assert isinstance(addr, str), addr
if not PY3:
addr = unicode(addr)
ipaddress.IPv6Address(addr)
elif family == psutil.AF_LINK:
assert re.match(r'([a-fA-F0-9]{2}[:|\-]?){6}', addr) is not None, addr
else:
raise ValueError("unknown family %r", family)
示例5: allValues
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def allValues(ctype):
if issubclass(ctype, IntEnum):
return list(ctype)
if issubclass(ctype, Structure):
names, types = zip(*ctype._fields)
values = [allValues(t) for t in types]
def make(vals):
obj = ctype()
for name, val in zip(names, vals):
setattr(obj, name, val)
return obj
return [make(vals) for vals in product(*values)]
# TODO: handle bounded ints via _fields
# TODO: handle arrays
raise TypeError("Unsupported type %s" % ctype)
示例6: get_class
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def get_class(self):
"""Return a Code class based on current ErrorType value.
Returns:
enum.IntEnum: class referenced by current error type.
"""
classes = {'OFPET_HELLO_FAILED': HelloFailedCode,
'OFPET_BAD_REQUEST': BadRequestCode,
'OFPET_BAD_ACTION': BadActionCode,
'OFPET_BAD_INSTRUCTION': BadInstructionCode,
'OFPET_BAD_MATCH': BadMatchCode,
'OFPET_FLOW_MOD_FAILED': FlowModFailedCode,
'OFPET_GROUP_MOD_FAILED': GroupModFailedCode,
'OFPET_PORT_MOD_FAILED': PortModFailedCode,
'OFPET_QUEUE_OP_FAILED': QueueOpFailedCode,
'OFPET_SWITCH_CONFIG_FAILED': SwitchConfigFailedCode,
'OFPET_ROLE_REQUEST_FAILED': RoleRequestFailedCode,
'OFPET_METER_MOD_FAILED': MeterModFailedCode,
'OFPET_TABLE_MOD_FAILED': TableModFailedCode,
'OFPET_TABLE_FEATURES_FAILED': TableFeaturesFailedCode}
return classes.get(self.name, GenericFailedCode)
示例7: test_introspection
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_introspection(self):
class Number(IntEnum):
one = 100
two = 200
self.assertIs(Number.one._member_type_, int)
self.assertIs(Number._member_type_, int)
class String(str, Enum):
yarn = 'soft'
rope = 'rough'
wire = 'hard'
self.assertIs(String.yarn._member_type_, str)
self.assertIs(String._member_type_, str)
class Plain(Enum):
vanilla = 'white'
one = 1
self.assertIs(Plain.vanilla._member_type_, object)
self.assertIs(Plain._member_type_, object)
示例8: payload_as_string
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def payload_as_string(v):
if isinstance(v, dict):
return json.dumps(v)
elif isinstance(v, IntEnum):
return v.value
elif not isinstance(v, list):
return str(v)
elif len(v) == 1: # single-element array should be presented as single value
return payload_as_string(v[0])
else:
return json.dumps(v)
示例9: setUp
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def setUp(self):
class Season(Enum):
SPRING = 1
SUMMER = 2
AUTUMN = 3
WINTER = 4
self.Season = Season
class Konstants(float, Enum):
E = 2.7182818
PI = 3.1415926
TAU = 2 * PI
self.Konstants = Konstants
class Grades(IntEnum):
A = 5
B = 4
C = 3
D = 2
F = 0
self.Grades = Grades
class Directional(str, Enum):
EAST = 'east'
WEST = 'west'
NORTH = 'north'
SOUTH = 'south'
self.Directional = Directional
from datetime import date
class Holiday(date, Enum):
NEW_YEAR = 2013, 1, 1
IDES_OF_MARCH = 2013, 3, 15
self.Holiday = Holiday
示例10: test_intenum_inherited
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_intenum_inherited(self):
class IntEnum(int, Enum):
pass
class phy(IntEnum):
pi = 3
tau = 2 * pi
self.assertTrue(phy.pi < phy.tau)
示例11: test_intenum
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_intenum(self):
class WeekDay(IntEnum):
SUNDAY = 1
MONDAY = 2
TUESDAY = 3
WEDNESDAY = 4
THURSDAY = 5
FRIDAY = 6
SATURDAY = 7
self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c')
self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2])
lst = list(WeekDay)
self.assertEqual(len(lst), len(WeekDay))
self.assertEqual(len(WeekDay), 7)
target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY'
target = target.split()
for i, weekday in enumerate(target):
i += 1
e = WeekDay(i)
self.assertEqual(e, i)
self.assertEqual(int(e), i)
self.assertEqual(e.name, weekday)
self.assertTrue(e in WeekDay)
self.assertEqual(lst.index(e)+1, i)
self.assertTrue(0 < e < 8)
self.assertTrue(type(e) is WeekDay)
self.assertTrue(isinstance(e, int))
self.assertTrue(isinstance(e, Enum))
示例12: test_intenum_duplicates
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_intenum_duplicates(self):
class WeekDay(IntEnum):
__order__ = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY'
SUNDAY = 1
MONDAY = 2
TUESDAY = TEUSDAY = 3
WEDNESDAY = 4
THURSDAY = 5
FRIDAY = 6
SATURDAY = 7
self.assertTrue(WeekDay.TEUSDAY is WeekDay.TUESDAY)
self.assertEqual(WeekDay(3).name, 'TUESDAY')
self.assertEqual([k for k,v in WeekDay.__members__.items()
if v.name != k], ['TEUSDAY', ])
示例13: test_intenum_transitivity
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_intenum_transitivity(self):
class number(IntEnum):
one = 1
two = 2
three = 3
class numero(IntEnum):
uno = 1
dos = 2
tres = 3
self.assertEqual(number.one, numero.uno)
self.assertEqual(number.two, numero.dos)
self.assertEqual(number.three, numero.tres)
示例14: test_wrong_enum_in_mixed_call
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_wrong_enum_in_mixed_call(self):
class Monochrome(IntEnum):
black = 0
white = 1
class Gender(Enum):
male = 0
female = 1
self.assertRaises(ValueError, Monochrome, Gender.male)
示例15: test_mixed_enum_in_call_1
# 需要导入模块: import enum [as 别名]
# 或者: from enum import IntEnum [as 别名]
def test_mixed_enum_in_call_1(self):
class Monochrome(IntEnum):
black = 0
white = 1
class Gender(IntEnum):
male = 0
female = 1
self.assertTrue(Monochrome(Gender.female) is Monochrome.white)