MilanM's picture
Upload 3 files
87b6e34 verified
def debug_element(obj):
"""Get all attributes and their string representations from an object using dir()."""
import copy
try:
# Create a deep copy of the object if possible
obj_copy = copy.deepcopy(obj)
except:
try:
# If deepcopy fails, try shallow copy
obj_copy = copy.copy(obj)
except:
# If copying fails completely, use the original object
obj_copy = obj
attributes = dir(obj_copy)
results = []
for attr in attributes:
try:
# Get the attribute value from the copy
value = getattr(obj_copy, attr)
# Handle callable attributes
if callable(value):
try:
# Try to call the method without arguments
result = value()
str_value = f"<callable result: {str(result)}>"
except Exception as call_error:
# If calling fails, just record it's a callable
str_value = f"<callable: {type(value).__name__} - error when called: {str(call_error)}>"
else:
str_value = str(value)
results.append(f"{attr}: {str_value}")
except Exception as e:
results.append(f"{attr}: <error accessing: {str(e)}>")
return results