mirror of
https://github.com/simple-login/app.git
synced 2025-02-25 00:03:03 +08:00
Add EnumE.has_name(), EnumE.get_value()
This commit is contained in:
parent
f0f81930bc
commit
c794e73abd
2 changed files with 23 additions and 1 deletions
|
@ -113,13 +113,29 @@ class EnumE(enum.Enum):
|
|||
return value in set(item.value for item in cls)
|
||||
|
||||
@classmethod
|
||||
def get_name(cls, value: int):
|
||||
def get_name(cls, value: int) -> Optional[str]:
|
||||
for item in cls:
|
||||
if item.value == value:
|
||||
return item.name
|
||||
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def has_name(cls, name: str) -> bool:
|
||||
for item in cls:
|
||||
if item.name == name:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def get_value(cls, name: str) -> Optional[int]:
|
||||
for item in cls:
|
||||
if item.name == name:
|
||||
return item.value
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class PlanEnum(EnumE):
|
||||
monthly = 2
|
||||
|
|
|
@ -211,3 +211,9 @@ def test_EnumE():
|
|||
assert E.get_name(100) == "A"
|
||||
assert E.get_name(200) == "B"
|
||||
assert E.get_name(101) is None
|
||||
|
||||
assert E.has_name("A")
|
||||
assert not E.has_name("Not existent")
|
||||
|
||||
assert E.get_value("A") == 100
|
||||
assert E.get_value("Not existent") is None
|
||||
|
|
Loading…
Reference in a new issue