-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23d06d1
commit 572ea98
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from enum import Enum | ||
from typing import Type | ||
|
||
from .exception import EnumMemberNotExistError | ||
|
||
|
||
def get_enum_by_name(enum_class: Type[Enum], name: str) -> Enum: | ||
"""根据枚举成员名的字符串表示获取枚举成员。 | ||
Parameters | ||
---------- | ||
enum_class : Type[T] | ||
枚举类 | ||
name : str | ||
枚举名称的字符串表示 | ||
Returns | ||
------- | ||
T | ||
枚举成员 | ||
""" | ||
|
||
if name is None or isinstance(name, enum_class): | ||
return name | ||
else: | ||
try: | ||
return enum_class[name.upper()] | ||
except KeyError: | ||
raise EnumMemberNotExistError(enum_class, name) |