Skip to content

Commit 21ed7c6

Browse files
committed
cache enum value
1 parent b73e4ae commit 21ed7c6

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

module_enum/enum_get_by_value.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import enum
2+
3+
CACHE_CODE_TO_CATEGORY: dict = {}
4+
5+
6+
class Category(enum.Enum):
7+
HISTORY = {
8+
'code': 10001,
9+
'name': 'history',
10+
}
11+
MATH = {
12+
'code': 10002,
13+
'name': 'math',
14+
}
15+
PHYSICS = {
16+
'code': 10003,
17+
'name': 'physics',
18+
}
19+
20+
def __init__(self, value: dict):
21+
self.code = value['code']
22+
self.category_name = value['name']
23+
CACHE_CODE_TO_CATEGORY[self.code] = self
24+
25+
@classmethod
26+
def get_by_code(cls, code: int):
27+
return CACHE_CODE_TO_CATEGORY.get(code)
28+
29+
30+
if __name__ == '__main__':
31+
print(Category.get_by_code(10001))
32+
print(Category.get_by_code(10002))
33+

module_enum/enum_intenum.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ class BugStatus(enum.IntEnum):
1414
print('Ordered by value: ')
1515
print('\n'.join(' ' + s.name for s in sorted(BugStatus)))
1616

17+
print(BugStatus.new == 7)
18+
print(BugStatus.incomplete == 6)
19+

0 commit comments

Comments
 (0)