2023-05-22 07:02:17 +00:00
|
|
|
def hasattr_(obj, attr: str):
|
2023-05-24 02:26:46 +00:00
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Check whether the object has the multi sublevel attr
|
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to check
|
|
|
|
attr (str): The multi level attr to check
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs:
|
|
|
|
try:
|
|
|
|
obj = getattr(obj, a)
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2023-05-24 02:26:46 +00:00
|
|
|
|
|
|
|
def setattr_(obj, attr: str, value, ignore: bool = False):
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Set the object's multi sublevel attr to value, if ignore, ignore when it doesn't exist
|
|
|
|
|
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to set
|
|
|
|
attr (str): The multi level attr to set
|
|
|
|
value (Any): The value to set
|
|
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs[:-1]:
|
|
|
|
try:
|
|
|
|
obj = getattr(obj, a)
|
|
|
|
except AttributeError:
|
|
|
|
if ignore:
|
2023-05-24 02:26:46 +00:00
|
|
|
return
|
2023-05-22 07:02:17 +00:00
|
|
|
raise AttributeError(f"Object {obj} has no attribute {attr}")
|
|
|
|
setattr(obj, attrs[-1], value)
|
|
|
|
|
2023-05-24 02:26:46 +00:00
|
|
|
|
|
|
|
def getattr_(obj, attr: str, ignore: bool = None):
|
|
|
|
r"""
|
2023-05-22 07:02:17 +00:00
|
|
|
Get the object's multi sublevel attr
|
2023-05-24 02:26:46 +00:00
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
Args:
|
2023-05-24 02:26:46 +00:00
|
|
|
obj (object): The object to set
|
|
|
|
attr (str): The multi level attr to set
|
|
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
attrs = attr.split('.')
|
|
|
|
for a in attrs:
|
|
|
|
try:
|
|
|
|
obj = getattr(obj, a)
|
|
|
|
except AttributeError:
|
|
|
|
if ignore:
|
|
|
|
return None
|
|
|
|
raise AttributeError(f"Object {obj} has no attribute {attr}")
|
2023-05-24 02:26:46 +00:00
|
|
|
return obj
|