151 lines
3.5 KiB
Python
151 lines
3.5 KiB
Python
import re
|
||||
|
|
from datetime import datetime
|
|||
|
|
from functools import reduce
|
|||
|
|
from time import time
|
|||
|
|
from uuid import uuid4, UUID
|
|||
|
|
|
|||
|
|
import pytz as pytz
|
|||
|
|
import shortuuid
|
|||
|
|
from fastapi.requests import Request
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_time() -> datetime:
|
|||
|
|
"""
|
|||
|
|
Returns the current time.
|
|||
|
|
Returns:
|
|||
|
|
"""
|
|||
|
|
return datetime.fromtimestamp(time(), tz=pytz.timezone('Asia/Tehran'))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_uuid() -> UUID:
|
|||
|
|
"""Returns an unique UUID (UUID4)"""
|
|||
|
|
return uuid4()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_numeric(_input) -> bool:
|
|||
|
|
"""Returns True if the input is a number."""
|
|||
|
|
try:
|
|||
|
|
val = int(_input)
|
|||
|
|
return True
|
|||
|
|
except ValueError:
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_v_id(prefix: str, prefix_len=2, source_id: UUID = None) -> str:
|
|||
|
|
main_id = source_id if source_id is not None else uuid4()
|
|||
|
|
short_id = shortuuid.encode(main_id)
|
|||
|
|
return str(prefix)[0:prefix_len] + '.' + short_id[:7]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_qr_v_id(source_id: UUID = None) -> str:
|
|||
|
|
main_id = source_id if source_id is not None else uuid4()
|
|||
|
|
short_id = shortuuid.encode(main_id)
|
|||
|
|
return short_id[:8]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_valid_uuid(uuid_to_test, version=4):
|
|||
|
|
"""
|
|||
|
|
Check if uuid_to_test is a valid UUID.
|
|||
|
|
|
|||
|
|
Parameters
|
|||
|
|
----------
|
|||
|
|
uuid_to_test : str
|
|||
|
|
version : {1, 2, 3, 4}
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
`True` if uuid_to_test is a valid UUID, otherwise `False`.
|
|||
|
|
|
|||
|
|
Examples
|
|||
|
|
--------
|
|||
|
|
>>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
|
|||
|
|
True
|
|||
|
|
>>> is_valid_uuid('c9bf9e58')
|
|||
|
|
False
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
uuid_obj = UUID(str(uuid_to_test), version=version)
|
|||
|
|
except ValueError:
|
|||
|
|
return False
|
|||
|
|
return str(uuid_obj) == str(uuid_to_test)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_base_url(request: Request) -> str:
|
|||
|
|
"""
|
|||
|
|
Returns the base url of the request.
|
|||
|
|
|
|||
|
|
Parameters
|
|||
|
|
----------
|
|||
|
|
request : Request
|
|||
|
|
|
|||
|
|
Returns
|
|||
|
|
-------
|
|||
|
|
str
|
|||
|
|
"""
|
|||
|
|
return '{0}://{1}{2}/'.format(request.url.scheme,
|
|||
|
|
request.url.hostname,
|
|||
|
|
':' + request.url.port.__str__() if request.url.port else '')
|
|||
|
|
|
|||
|
|
|
|||
|
|
def p2e_numbers(input: str) -> str:
|
|||
|
|
"""
|
|||
|
|
convert persian numbers to english numbers.
|
|||
|
|
"""
|
|||
|
|
_map = {
|
|||
|
|
'۱': '1',
|
|||
|
|
'۲': '2',
|
|||
|
|
'۳': '3',
|
|||
|
|
'۴': '4',
|
|||
|
|
'۵': '5',
|
|||
|
|
'۶': '6',
|
|||
|
|
'۷': '7',
|
|||
|
|
'۸': '8',
|
|||
|
|
'۹': '9',
|
|||
|
|
'۰': '0'
|
|||
|
|
}
|
|||
|
|
a = map(lambda ch: _map[ch] if ch in _map else ch, input)
|
|||
|
|
return ''.join(list(a))
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_list_of_strings(lst):
|
|||
|
|
return bool(lst) and isinstance(lst, list) and all(isinstance(elem, str) for elem in lst)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def smart_cast(_input):
|
|||
|
|
if is_numeric(_input):
|
|||
|
|
return int(_input)
|
|||
|
|
if is_valid_uuid(_input):
|
|||
|
|
return UUID(str(_input))
|
|||
|
|
return _input
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_mobile_number(mobile: str):
|
|||
|
|
regex = r"^\+?[0-0][0-9]{10}$"
|
|||
|
|
if mobile and not re.search(regex, mobile, re.I):
|
|||
|
|
return False
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_email(email: str):
|
|||
|
|
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
|||
|
|
if email and not re.search(regex, email, re.I):
|
|||
|
|
return False
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def deep_get(dictionary, keys, default=None):
|
|||
|
|
return reduce(lambda d, key: d.get(key, default) if isinstance(d, dict) else default, keys.split("."), dictionary)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_tag_strips(tag_name: str, content: str):
|
|||
|
|
try:
|
|||
|
|
r_string = fr'\[{tag_name}\](.|\n)*?\[\/{tag_name}\]'
|
|||
|
|
matches = re.search(r_string, str(content), re.MULTILINE)
|
|||
|
|
raw = matches.group(0)
|
|||
|
|
raw = raw.replace(f'[{tag_name}]', '')
|
|||
|
|
raw = raw.replace(f'[/{tag_name}]', '')
|
|||
|
|
return raw
|
|||
|
|
except BaseException:
|
|||
|
|
return False
|