-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_lib.py
More file actions
executable file
·58 lines (48 loc) · 1.23 KB
/
enum_lib.py
File metadata and controls
executable file
·58 lines (48 loc) · 1.23 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
55
56
57
58
"""enum library
"""
import enum as en
# ValueError: duplicate values found in <enum 'Days' >: WWW -> WEN
# @en.unique # decorator for unique values
class Days(en.Enum):
"""Week Days"""
# DAY = en.auto() # set unique and auto value
MON = 0
SUN = 1
TUE = 2
WEN = 3
THE = 4
FRI = 5
SAT = 6
WWW = 3
# SAT = 10 TypeError: Attempted to reuse key: 'SAT'
class IntDay(en.IntEnum):
"""Int Enum Week Days"""
MON = 0
SUN = 1
TUE = 2
WEN = 3
THE = 4
FRI = 5
SAT = 6
# Use variables
print(Days.WEN.name) # WEN
print(Days.WEN.value) # 3
print(repr(Days.FRI)) # <Days.FRI: 5>
print(type(Days.SUN)) # <enum 'Days'>
print(isinstance(Days.SUN, Days)) # True
print(Days(4)) # Days.THE
print(Days.SUN == Days.MON) # False
# TypeError: '>' not supported between instances of 'Days' and 'Days'
# print(Days.FRI > Days.WEN)
print(IntDay.FRI > IntDay.WEN) # True
# Loop
print("All days")
for day in Days:
print(f'{day.name}: {day.value}')
# Variables Features
# Days.WEN = 5 AttributeError: Cannot reassign members.
print(id(Days.WWW) == id(Days.WEN)) # True
print(Days.WWW is Days.WEN) # True
# Ordered Mapping
for day, val in Days.__members__.items():
print(day, val.value)