2023-01-06 20:27:25 +03:00
|
|
|
import random
|
|
|
|
import string
|
2023-01-08 09:48:45 +03:00
|
|
|
from io import BytesIO
|
2023-01-06 20:27:25 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
# noinspection PyPackageRequirements
|
|
|
|
from PIL import Image
|
|
|
|
pil_imported = True
|
2023-01-08 09:49:27 +03:00
|
|
|
except ImportError:
|
2023-01-06 20:27:25 +03:00
|
|
|
pil_imported = False
|
|
|
|
|
|
|
|
|
|
|
|
def is_string(var) -> bool:
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a string.
|
|
|
|
"""
|
|
|
|
return isinstance(var, str)
|
|
|
|
|
|
|
|
|
|
|
|
def is_dict(var) -> bool:
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a dictionary.
|
|
|
|
|
|
|
|
:param var: object to be checked
|
|
|
|
:type var: :obj:`object`
|
|
|
|
|
|
|
|
:return: True if the given object is a dictionary.
|
|
|
|
:rtype: :obj:`bool`
|
|
|
|
"""
|
|
|
|
return isinstance(var, dict)
|
|
|
|
|
|
|
|
|
|
|
|
def is_bytes(var) -> bool:
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a bytes object.
|
|
|
|
|
|
|
|
:param var: object to be checked
|
|
|
|
:type var: :obj:`object`
|
|
|
|
|
|
|
|
:return: True if the given object is a bytes object.
|
|
|
|
:rtype: :obj:`bool`
|
|
|
|
"""
|
|
|
|
return isinstance(var, bytes)
|
|
|
|
|
|
|
|
|
|
|
|
def is_pil_image(var) -> bool:
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a PIL.Image.Image object.
|
|
|
|
|
|
|
|
:param var: object to be checked
|
|
|
|
:type var: :obj:`object`
|
|
|
|
|
|
|
|
:return: True if the given object is a PIL.Image.Image object.
|
|
|
|
:rtype: :obj:`bool`
|
|
|
|
"""
|
|
|
|
return pil_imported and isinstance(var, Image.Image)
|
|
|
|
|
|
|
|
|
|
|
|
def pil_image_to_file(image, extension='JPEG', quality='web_low'):
|
|
|
|
if pil_imported:
|
|
|
|
photoBuffer = BytesIO()
|
|
|
|
image.convert('RGB').save(photoBuffer, extension, quality=quality)
|
|
|
|
photoBuffer.seek(0)
|
|
|
|
|
|
|
|
return photoBuffer
|
|
|
|
else:
|
|
|
|
raise RuntimeError('PIL module is not imported')
|
|
|
|
|
|
|
|
|
|
|
|
def chunks(lst, n):
|
|
|
|
"""Yield successive n-sized chunks from lst."""
|
|
|
|
# https://stackoverflow.com/a/312464/9935473
|
|
|
|
for i in range(0, len(lst), n):
|
|
|
|
yield lst[i:i + n]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_random_token() -> str:
|
|
|
|
"""
|
|
|
|
Generates a random token consisting of letters and digits, 16 characters long.
|
|
|
|
|
|
|
|
:return: a random token
|
|
|
|
:rtype: :obj:`str`
|
|
|
|
"""
|
|
|
|
return ''.join(random.sample(string.ascii_letters, 16))
|