File size: 1,356 Bytes
87b6e34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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 |