Add EnumE.has_name(), EnumE.get_value()
This commit is contained in:
parent
f0f81930bc
commit
c794e73abd
|
@ -113,13 +113,29 @@ class EnumE(enum.Enum):
|
||||||
return value in set(item.value for item in cls)
|
return value in set(item.value for item in cls)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_name(cls, value: int):
|
def get_name(cls, value: int) -> Optional[str]:
|
||||||
for item in cls:
|
for item in cls:
|
||||||
if item.value == value:
|
if item.value == value:
|
||||||
return item.name
|
return item.name
|
||||||
|
|
||||||
return None
|
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):
|
class PlanEnum(EnumE):
|
||||||
monthly = 2
|
monthly = 2
|
||||||
|
|
|
@ -211,3 +211,9 @@ def test_EnumE():
|
||||||
assert E.get_name(100) == "A"
|
assert E.get_name(100) == "A"
|
||||||
assert E.get_name(200) == "B"
|
assert E.get_name(200) == "B"
|
||||||
assert E.get_name(101) is None
|
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