59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Self
|
|
from re import Pattern as REPattern
|
|
from json import loads as json_decode
|
|
from Utils.Patterns import RE
|
|
|
|
class Check:
|
|
|
|
@staticmethod
|
|
def is_string(item:Any|None) -> bool:
|
|
return isinstance(item, str)
|
|
|
|
@staticmethod
|
|
def is_binary(item:Any|None) -> bool:
|
|
return isinstance(item, bytes)
|
|
|
|
@classmethod
|
|
def is_key(cls:type[Self], item:Any|None) -> bool:
|
|
return isinstance(item, str) and RE.KEY.match(item) is not None
|
|
|
|
@staticmethod
|
|
def is_array(item:Any|None) -> bool:
|
|
return isinstance(item, (list, tuple))
|
|
|
|
@staticmethod
|
|
def is_dictionary(item:Any|None) -> bool:
|
|
return isinstance(item, dict)
|
|
|
|
@staticmethod
|
|
def is_boolean(item:Any|None) -> bool:
|
|
return isinstance(item, bool)
|
|
|
|
@staticmethod
|
|
def is_integer(item:Any|None) -> bool:
|
|
return isinstance(item, int)
|
|
|
|
@staticmethod
|
|
def is_function(item:Any|None) -> bool:
|
|
return callable(item)
|
|
|
|
@staticmethod
|
|
def is_regular_expression(item:Any|None) -> bool:
|
|
return isinstance(item, REPattern)
|
|
|
|
@staticmethod
|
|
def is_json_data(item:Any|None, full:bool = False) -> bool:
|
|
if Check.is_dictionary(item) or Check.is_array(item):
|
|
return True
|
|
if Check.is_string(item) and len(item = item.strip()) >= 2 and item[0] + item[-1] in ("{}", "[]"):
|
|
if not full:
|
|
return True
|
|
try:
|
|
if json_decode(item) is not None:
|
|
return True
|
|
except Exception as _:
|
|
pass
|
|
return False |