MilanM commited on
Commit
98b83f7
·
verified ·
1 Parent(s): eb926bf

Upload debug_helper_functions.py

Browse files
Files changed (1) hide show
  1. debug_helper_functions.py +39 -0
debug_helper_functions.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def debug_element(obj):
2
+ """Get all attributes and their string representations from an object using dir()."""
3
+ import copy
4
+ try:
5
+ # Create a deep copy of the object if possible
6
+ obj_copy = copy.deepcopy(obj)
7
+ except:
8
+ try:
9
+ # If deepcopy fails, try shallow copy
10
+ obj_copy = copy.copy(obj)
11
+ except:
12
+ # If copying fails completely, use the original object
13
+ obj_copy = obj
14
+
15
+ attributes = dir(obj_copy)
16
+ results = []
17
+
18
+ for attr in attributes:
19
+ try:
20
+ # Get the attribute value from the copy
21
+ value = getattr(obj_copy, attr)
22
+
23
+ # Handle callable attributes
24
+ if callable(value):
25
+ try:
26
+ # Try to call the method without arguments
27
+ result = value()
28
+ str_value = f"<callable result: {str(result)}>"
29
+ except Exception as call_error:
30
+ # If calling fails, just record it's a callable
31
+ str_value = f"<callable: {type(value).__name__} - error when called: {str(call_error)}>"
32
+ else:
33
+ str_value = str(value)
34
+
35
+ results.append(f"{attr}: {str_value}")
36
+ except Exception as e:
37
+ results.append(f"{attr}: <error accessing: {str(e)}>")
38
+
39
+ return results