init
This commit is contained in:
0
spider/normalutils/utils/__init__.py
Normal file
0
spider/normalutils/utils/__init__.py
Normal file
21
spider/normalutils/utils/contenttohtml.py
Normal file
21
spider/normalutils/utils/contenttohtml.py
Normal file
@ -0,0 +1,21 @@
|
||||
from markdown import markdown
|
||||
import html
|
||||
|
||||
from normalutils.choices.htmlchoices import HtmlContentType
|
||||
|
||||
|
||||
def content_to_html(content: str, content_type=HtmlContentType.TEXT_MARKDOWN, title=None):
|
||||
if content_type == HtmlContentType.TEXT_MARKDOWN:
|
||||
content = markdown(content)
|
||||
return content
|
||||
elif content_type == HtmlContentType.TEXT_PLAIN:
|
||||
content = html.escape(content)
|
||||
content = content.split('\n')
|
||||
ret = ''
|
||||
for sentence in content:
|
||||
ret += ''.join(["<p>", sentence, "</p>\n"])
|
||||
# add title
|
||||
if title is not None:
|
||||
title = html.escape(title)
|
||||
return ''.join(['<h1>', title, '</h1>\n', ret])
|
||||
return ret
|
72
spider/normalutils/utils/random.py
Normal file
72
spider/normalutils/utils/random.py
Normal file
@ -0,0 +1,72 @@
|
||||
from typing import Callable
|
||||
import random
|
||||
from functools import wraps
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
def random_str(typename: str, randomlength: int = 16) -> Callable[[None], str]:
|
||||
"""Parameter:
|
||||
----------
|
||||
type: 'common' [A-Za-z0-9]; 'lower' [a-z0-9]"""
|
||||
common = "AaBbCcDdEeFfGgHhJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"
|
||||
lower = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
if typename == 'common':
|
||||
chars = common
|
||||
elif typename == 'lower':
|
||||
chars = lower
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
def _do() -> str:
|
||||
length = len(chars) - 1
|
||||
ret = "".join([chars[random.randint(0, length)] for _ in range(randomlength)])
|
||||
return ret
|
||||
|
||||
return _do
|
||||
|
||||
|
||||
def create_random_unique_str(rand_func: Callable[[None], str]):
|
||||
time_string = None
|
||||
list_string = []
|
||||
|
||||
def create_random(rand_func: Callable[[None], str]):
|
||||
timestr = timezone.now().timestamp()
|
||||
timestr = str(int(timestr))
|
||||
ranstr = rand_func()
|
||||
ret = timestr + ranstr
|
||||
return ret, timestr
|
||||
|
||||
def get_unique_str():
|
||||
nonlocal time_string
|
||||
nonlocal list_string
|
||||
while True:
|
||||
ret, timestr = create_random(rand_func)
|
||||
if time_string != timestr:
|
||||
time_string = timestr
|
||||
list_string = [ret]
|
||||
return ret
|
||||
else:
|
||||
if ret not in list_string:
|
||||
list_string.append(ret)
|
||||
return ret
|
||||
|
||||
def decrator_func(func):
|
||||
@wraps(func)
|
||||
def _do():
|
||||
return get_unique_str()
|
||||
return _do
|
||||
|
||||
return decrator_func
|
||||
# return get_unique_str
|
||||
|
||||
|
||||
@create_random_unique_str(random_str('common', 2))
|
||||
def default_nickname() -> str:
|
||||
pass
|
||||
|
||||
|
||||
@create_random_unique_str(random_str('lower', 1))
|
||||
def default_version_unique_id() -> str:
|
||||
pass
|
14
spider/normalutils/utils/timeit.py
Normal file
14
spider/normalutils/utils/timeit.py
Normal file
@ -0,0 +1,14 @@
|
||||
from time import time
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def timeit(func):
|
||||
@wraps(func)
|
||||
def _totime(*args, **kwargs):
|
||||
st = time()
|
||||
ans = func(*args, **kwargs)
|
||||
end = time()
|
||||
print("'{}' use time: {}".format(func.__name__, end - st))
|
||||
return ans
|
||||
|
||||
return _totime
|
13
spider/normalutils/utils/validation.py
Normal file
13
spider/normalutils/utils/validation.py
Normal file
@ -0,0 +1,13 @@
|
||||
from validator import ValidationError
|
||||
|
||||
|
||||
def validate_lenth(value: str, max_length: int, min_length: int = 4):
|
||||
length = len(value)
|
||||
if length > max_length:
|
||||
raise ValidationError(
|
||||
"Length is {}. It is longer than {}".format(length, max_length)
|
||||
)
|
||||
elif length < min_length:
|
||||
raise ValidationError(
|
||||
"Length is {}. It is shorter than {}".format(length, min_length)
|
||||
)
|
Reference in New Issue
Block a user