init
This commit is contained in:
59
spider/normalutils/choices/__init__.py
Normal file
59
spider/normalutils/choices/__init__.py
Normal file
@ -0,0 +1,59 @@
|
||||
# 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"))
|
17
spider/normalutils/choices/htmlchoices.py
Normal file
17
spider/normalutils/choices/htmlchoices.py
Normal file
@ -0,0 +1,17 @@
|
||||
from normalutils.choices import BaseChoices
|
||||
|
||||
|
||||
class HttpMethod(BaseChoices):
|
||||
GET = "GET"
|
||||
POST = "POST"
|
||||
PUT = "PUT"
|
||||
PATCH = "PATCH"
|
||||
DELETE = "DELETE"
|
||||
OPTIONS = "OPOTIONS"
|
||||
|
||||
|
||||
class HtmlContentType(BaseChoices):
|
||||
TEXT_PLAIN = "text/plain"
|
||||
TEXT_HTML = "text/html"
|
||||
TEXT_MARKDOWN = "text/markdown"
|
||||
APPLICATION_JSON = "application/json"
|
Reference in New Issue
Block a user