- enum_accessfrom enum import Enum # 创建 class HttpStatus(Enum): OK = 200 BAD_REQUEST = 400 FORBIDDEN = 403 NOT_FOUND = 404 REQUEST_TIMEOUT = 408 SERVICE_UNAVAILABLE = 500 # value 访问使用元组() print(HttpStatus(200)) # HttpStatus.OK # name 访问使用list[] print(HttpStatus['OK']) # HttpStatus.OK # 赋值给 enum对象 number = HttpStatus.OK print(number) # HttpStatus.OK**import unittest from enum import auto, Enum class TestEnum(unittest.TestCase): def test_auto_number(self): class Color(Enum): red = auto() blue = auto() green = auto() self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) self.assertEqual(Color.red.value, 1) self.assertEqual(Color.blue.value, 2) self.assertEqual(Color.green.value, 3) def test_auto_name(self): class Color(Enum): def _generate_next_value_(self, start, count, last): return self red = auto() blue = auto() green = auto() self.assertEqual(list(Color), [Color.red, Color.blue, Color.green]) self.assertEqual(Color.red.value, 'red') self.assertEqual(Color.blue.value, 'blue') self.assertEqual(Color.green.value, 'green') TestEnum().test_auto_name() TestEnum().test_auto_number() **import unittest from enum import Enum, IntEnum class TestEnum(unittest.TestCase): class Season(IntEnum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4 def test_comparisons(self): season = self.Season self.assertEqual(season.SPRING, 1) class Part(Enum): SPRING = 1 CLIP = 2 BARREL = 3 self.assertNotEqual(Part.SPRING, 1) self.assertNotEqual(Part.SPRING, season.SPRING) TestEnum().test_comparisons()from enum import Enum # 创建 class HttpStatus(Enum): OK = 200 BAD_REQUEST = 400 FORBIDDEN = 403 NOT_FOUND = 404 REQUEST_TIMEOUT = 408 SERVICE_UNAVAILABLE = 500 print('Member: {}'.format(HttpStatus.OK)) # Member: HttpStatus.OK print('Member name: {}'.format(HttpStatus.OK.name)) # Member name: OK print('Member value: {}'.format(HttpStatus.OK.value)) # Member value: 200 print(repr(HttpStatus.OK)) # <enum 'HttpStatus'> print(type(HttpStatus.OK)) # <HttpStatus.OK: 200> print(isinstance(HttpStatus.OK, HttpStatus)) # Trueimport unittest from enum import Enum class EnumExtend(unittest.TestCase): def test_extending(self): class Color(Enum): red = 1 green = 2 blue = 3 # TypeError: Cannot extend enumerations with self.assertRaises(TypeError): class MoreColor(Color): cyan = 4 magenta = 5 yellow = 6 def test_extending2(self): class Shade(Enum): def shade(self): print(self.name) class Color(Shade): red = 1 green = 2 blue = 3 with self.assertRaises(TypeError): class MoreColor(Color): cyan = 4 magenta = 5 yellow = 6 def test_extending3(self): class Shade(Enum): def shade(self): return self.name class Color(Shade): def hex(self): return '%s nice!' % self.value class MoreColor(Color): cyan = 4 magenta = 5 yellow = 6 self.assertEqual(MoreColor.magenta.shade(), 'magenta') self.assertEqual(MoreColor.magenta.hex(), '5 nice!')from enum import Enum class Mood(Enum): FUNKY = (1, "hello") HAPPY = (3, "world") def describe(self): return self.name, self.value def __init__(self, num, nice): self.num = num self.nice = nice def __str__(self): return 'my custom str! {0}'.format(self.value) @classmethod def favorite_mood(cls): return cls.HAPPY @property def testValue(self): return self.nice ':' str(self.num) print(Mood.favorite_mood()) # my custom str! (3, 'world') print(Mood.HAPPY.describe()) # ('HAPPY', (3, 'world')) print(str(Mood.FUNKY)) # my custom str! (1, 'hello') print(Mood.FUNKY.testValue) # hello:1from enum import Enum # 创建 class HttpStatus(Enum): OK = 200 BAD_REQUEST = 400 FORBIDDEN = 403 NOT_FOUND = 404 REQUEST_TIMEOUT = 408 SERVICE_UNAVAILABLE = 500 OTHER = 200 # 迭代 for status in HttpStatus: print('{} : {}'.format(status.name, status.value)) print('n') # 使用 # print([name for name, member in HttpStatus.__members__.items() if member.name != name]) for name, member in HttpStatus.__members__.items(): print('{} : {}'.format(name, member))import unittest from enum import Enum class TestOrder(unittest.TestCase): def test_same_members(self): class Color(Enum): _order_ = 'red green blue' red = 1 green = 2 blue = 3 def test_same_members_with_aliases(self): class Color(Enum): _order_ = 'red green blue' red = 1 green = 2 blue = 3 verde = green def test_same_members_wrong_order(self): with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): class Color(Enum): _order_ = 'red green blue' red = 1 blue = 3 green = 2 def test_order_has_extra_members(self): with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): class Color(Enum): _order_ = 'red green blue purple' red = 1 green = 2 blue = 3 def test_order_has_extra_members_with_aliases(self): with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): class Color(Enum): _order_ = 'red green blue purple' red = 1 green = 2 blue = 3 verde = green def test_enum_has_extra_members(self): with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): class Color(Enum): _order_ = 'red green blue' red = 1 green = 2 blue = 3 purple = 4 def test_enum_has_extra_members_with_aliases(self): with self.assertRaisesRegex(TypeError, 'member order does not match _order_'): class Color(Enum): _order_ = 'red green blue' red = 1 green = 2 blue = 3 purple = 4 verde = greenfrom enum import Enum, unique # 创建 @unique class HttpStatus(Enum): OK = 200 BAD_REQUEST = 400 FORBIDDEN = 403 NOT_FOUND = 404 REQUEST_TIMEOUT = 408 SERVICE_UNAVAILABLE = 500 OTHER = 200 # ValueError: duplicate values found in <enum 'HttpStatus'>: OTHER -> OK print(HttpStatus)
- enum_auto
- enum_compare
- enum_create
- enum_extend
- enum_function
- enum_iterate
- enum_order
- enum_unique
Python 系列文章 —— enum 详解
2022-01-13 10:44:41
浏览数 (1)