You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.4 KiB
54 lines
1.4 KiB
4 years ago
|
import calendar
|
||
|
import datetime
|
||
|
import random
|
||
|
import re
|
||
|
import string
|
||
|
import time
|
||
|
import unicodedata
|
||
|
|
||
|
|
||
|
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
|
||
|
return ''.join(random.SystemRandom().choice(chars) for _ in range(size))
|
||
|
|
||
|
|
||
|
def retry_wrapper(func, max_retry, timeout=5):
|
||
|
result = None
|
||
|
ret_code = 0
|
||
|
n_retry = 0
|
||
|
while n_retry < max_retry:
|
||
|
try:
|
||
|
result = func()
|
||
|
break
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
n_retry += 1
|
||
|
if n_retry == max_retry:
|
||
|
ret_code = -1
|
||
|
time.sleep(timeout)
|
||
|
return ret_code, result
|
||
|
|
||
|
|
||
|
def slugify(value, allow_unicode=True, simple=True):
|
||
|
"""
|
||
|
Modified from Django
|
||
|
Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
|
||
|
Remove characters that aren't alphanumerics, underscores, or hyphens.
|
||
|
Convert to lowercase. Also strip leading and trailing whitespace.
|
||
|
"""
|
||
|
value = str(value)
|
||
|
if allow_unicode:
|
||
|
value = unicodedata.normalize('NFKC', value)
|
||
|
else:
|
||
|
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
|
||
|
if simple:
|
||
|
value = re.sub(r'[/.]', '_', value)
|
||
|
return value
|
||
|
else:
|
||
|
value = re.sub(r'[^\w\s-]', '_', value).strip().lower()
|
||
|
return re.sub(r'[-\s]+', '-', value)
|
||
|
|
||
|
|
||
|
def get_day_of_week(year, month, day):
|
||
|
date = datetime.date(year, month, day)
|
||
|
return calendar.day_name[date.weekday()]
|