-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_enum.py
More file actions
54 lines (41 loc) · 1.55 KB
/
Copy pathstr_enum.py
File metadata and controls
54 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from enum import StrEnum
from typing import Any, Self
class BaseStrEnum(StrEnum):
_label: str
def __new__(cls, *values: Any) -> Self:
"""
Called only at class-definition time with:
- (value,) → no label
- (value, label) → with label
Lookup at runtime (one-arg) never reaches here.
"""
if len(values) not in (1, 2):
raise TypeError(f"Invalid arguments for str enum: {values!r}")
value: str = values[0]
if not isinstance(value, str):
raise TypeError(f"{value!r} is not an string")
label: str = values[1] if len(values) == 2 else ""
if not isinstance(label, str):
raise TypeError(f"Enum label must be a string, not {label!r}")
member = str.__new__(cls, value)
member._value_, member._label = value, label
return member
def get_display(self) -> str:
return self._label
@property
def display(self) -> str:
return self._label
class Color(BaseStrEnum):
RED = "red", "Red Like Blood"
GREEN = "green"
BLUE = "blue", "Love Is Blue"
print(Color.__members__)
print(Color._member_map_)
print(Color._value2member_map_)
print(Color._member_names_)
print()
print(Color.BLUE, Color.BLUE.name, Color.BLUE.value, Color.BLUE.display, Color.BLUE.get_display(), sep=", ")
print(Color("blue"), Color["BLUE"], sep=", ")
print()
print(Color.GREEN, Color.GREEN.name, Color.GREEN.value, Color.GREEN.display, Color.GREEN.get_display(), sep=", ")
print(Color("green"), Color["GREEN"], sep=", ")