class EmptyChecker:
"""
容器为空判断工具类
"""
@staticmethod
def is_empty(obj):
"""
判断任意对象是否为空
Args:
obj: 要判断的对象
Returns:
bool: 对象是否为空
"""
if obj is None:
return True
if isinstance(obj, str):
return len(obj.strip()) == 0
if isinstance(obj, (bytes, bytearray)):
return len(obj) == 0
if isinstance(obj, (list, tuple, set, dict)):
return len(obj) == 0
if hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
try:
next(iter(obj))
return False
except StopIteration:
return True
return False
@staticmethod
def is_empty_list(lst):
"""
判断列表是否为空
Args:
lst: 要判断的列表
Returns:
bool: 列表是否为空
"""
return lst is None or len(lst) == 0
@staticmethod
def is_empty_dict(dct):
"""
判断字典是否为空
Args:
dct: 要判断的字典
Returns:
bool: 字典是否为空
"""
return dct is None or len(dct) == 0
@staticmethod
def is_empty_set(s):
"""
判断集合是否为空
Args:
s: 要判断的集合
Returns:
bool: 集合是否为空
"""
return s is None or len(s) == 0
@staticmethod
def is_empty_string(s):
"""
判断字符串是否为空(包括空白字符串)
Args:
s: 要判断的字符串
Returns:
bool: 字符串是否为空
"""
return s is None or len(s.strip()) == 0
@staticmethod
def is_empty_tuple(t):
"""
判断元组是否为空
Args:
t: 要判断的元组
Returns:
bool: 元组是否为空
"""
return t is None or len(t) == 0
@staticmethod
def is_empty_bytes(b):
"""
判断字节串是否为空
Args:
b: 要判断的字节串
Returns:
bool: 字节串是否为空
"""
return b is None or len(b) == 0
@staticmethod
def is_empty_bytearray(ba):
"""
判断字节数组是否为空
Args:
ba: 要判断的字节数组
Returns:
bool: 字节数组是否为空
"""
return ba is None or len(ba) == 0
@staticmethod
def is_empty_iterable(iterable):
"""
判断可迭代对象是否为空
Args:
iterable: 要判断的可迭代对象
Returns:
bool: 可迭代对象是否为空
"""
if iterable is None:
return True
if not hasattr(iterable, '__iter__'):
return False
if isinstance(iterable, (str, bytes, bytearray)):
return len(iterable) == 0
try:
next(iter(iterable))
return False
except StopIteration:
return True