60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
# from userscontent.models import ContentType
|
|
import enum
|
|
from typing import Iterator, Union
|
|
|
|
|
|
class BaseChoices(enum.Enum):
|
|
@classmethod
|
|
def is_valid(cls, value: str, raise_exception: bool = False) -> bool:
|
|
answer = isinstance(value, str)
|
|
if not answer:
|
|
if raise_exception:
|
|
raise TypeError("The type of 'value' is wrong.")
|
|
else:
|
|
return False
|
|
answer = value in cls.choices()
|
|
if not raise_exception or answer:
|
|
return answer
|
|
else:
|
|
raise ValueError(
|
|
"The class '{}' does not have {}".format(cls.__name__, value)
|
|
)
|
|
|
|
@classmethod
|
|
def choices(cls, iter=False) -> Union[tuple, Iterator]:
|
|
choices = tuple(cls)
|
|
iterator_obj = map(lambda choice: choice.value, choices)
|
|
if iter:
|
|
return iterator_obj
|
|
else:
|
|
return tuple(iterator_obj)
|
|
|
|
|
|
class WordPropertyType(BaseChoices):
|
|
NOUN = "n."
|
|
PRONOUN = "pron." # 代词
|
|
ADJECTIVE = "adj."
|
|
ADVERB = "adv."
|
|
VERB = "v."
|
|
NUMBERAL = "num."
|
|
ARTICLE = "art."
|
|
PREPOTION = "prep."
|
|
CONJUNCTION = "conj."
|
|
INTERJECTION = "interj."
|
|
ABBREVIATION = "abbr."
|
|
COMBINATION = "comb."
|
|
SUFFIX = "suff." # 后缀
|
|
|
|
|
|
class StateType(BaseChoices):
|
|
REFUSED = "rf"
|
|
CHECKING = "ck"
|
|
PUBLISHED = "pb"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(list(StateType.choices()))
|
|
print(StateType.is_valid(1))
|
|
print(StateType.is_valid("refuse"))
|
|
print(StateType.is_valid("rf"))
|