22import time
33from tkinter import Event
44
5- from .console import PRINT , getPPrintStr
5+ from .console import getPPrintStr , pp
66
77
88
99
1010__all__ = ['debug' , 'class_method_debug' , 'check_time' , 'debugTkinterEvent' , 'pprint_debug' ]
1111
12- DEFAULT_TAG = '\n _______________________________ "{0}" _______________________________'
12+ DEFAULT_TAG = '\n ______________________________________________________________\n "{0}"'
13+
14+ def GetFuncModule (func : callable ) -> str :
15+ return func .__module__
16+ def GetFunctionName (func : callable ) -> str :
17+ if hasattr (func , '__qualname__' ):
18+ return func .__qualname__
19+ elif hasattr (func , '__module__' ):
20+ return f"{ func .__module__ } .{ func .__qualname__ } "
21+ else :
22+ return func .__name__
23+
24+
25+
26+ def _print_signature (func , tag , * args , ** kwargs ):
27+ name = GetFunctionName (func )
28+ print (tag .format (f'{ name } ' ))
29+
30+ if args or kwargs :
31+ try : args_repr = [repr (a ) for a in args ] # 1
32+ except : args_repr = [str (a ) for a in args ] # 1
33+
34+ kwargs_repr = [f"{ k } ={ v !r} " for k , v in kwargs .items ()] # 2
35+
36+ signature = ", " .join (args_repr + kwargs_repr ) # 3
37+
38+ print (f"{ name } (\n { signature } \n )" )
39+
1340
1441def class_method_debug (cls : str or type , tag : str = DEFAULT_TAG ):
1542 """
@@ -29,18 +56,21 @@ def debug_inner(func: callable = None):
2956 :param func: callable function to be debugged.
3057 :return:
3158 """
59+ name = f"{ cls } .{ func .__name__ } "
3260 @functools .wraps (func )
3361 def wrapper_debug (* args , ** kwargs ):
34- try : args_repr = [repr (a ) for a in args ] # 1
35- except : args_repr = [str (a ) for a in args ] # 1
62+ print (tag .format (name ))
63+ if args or kwargs :
64+ try : args_repr = [repr (a ) for a in args ] # 1
65+ except : args_repr = [str (a ) for a in args ] # 1
3666
37- kwargs_repr = [f"{ k } ={ v !r} " for k , v in kwargs .items ()] # 2
67+ kwargs_repr = [f"{ k } ={ v !r} " for k , v in kwargs .items ()] # 2
3868
39- signature = ", " .join (args_repr + kwargs_repr ) # 3
69+ signature = ", " .join (args_repr + kwargs_repr ) # 3
4070
41- print (f"{ tag . format ( f' { func . __module__ } . { func . __qualname__ } ' ) } \n { cls } . { func . __name__ } (\n { signature } \n )" )
71+ print (f"{ name } (\n { signature } \n )" )
4272 result = func (* args , ** kwargs )
43- print (f"{ func . __name__ } returned { result !r} \n " ) # 4
73+ print (f"{ name } returned { result !r} \n " ) # 4
4474
4575 return result
4676 return wrapper_debug
@@ -56,18 +86,12 @@ def debug(func: callable, tag: str = DEFAULT_TAG):
5686 :param tag: a unique string to identify the output in the console window.
5787 :return:
5888 """
89+ name = GetFunctionName (func )
5990 @functools .wraps (func )
6091 def wrapper_debug (* args , ** kwargs ):
61- try : args_repr = [repr (a ) for a in args ] # 1
62- except : args_repr = [str (a ) for a in args ] # 1
63-
64- kwargs_repr = [f"{ k } ={ v !r} " for k , v in kwargs .items ()] # 2
65-
66- signature = ", " .join (args_repr + kwargs_repr ) # 3
67-
68- print (f"{ tag .format (f'{ func .__module__ } .{ func .__qualname__ } ' )} \n { func .__qualname__ } (\n { signature } \n )" )
92+ _print_signature (func , tag , * args , ** kwargs )
6993 result = func (* args , ** kwargs )
70- print (f"{ func . __name__ } returned { result !r} \n " ) # 4
94+ print (f"{ name } returned { result !r} \n " ) # 4
7195
7296 return result
7397 return wrapper_debug
@@ -82,19 +106,21 @@ def pprint_debug(func: callable, tag: str = DEFAULT_TAG):
82106 :param tag: a unique string to identify the output in the console window.
83107 :return:
84108 """
109+ name = GetFunctionName (func )
85110 @functools .wraps (func )
86111 def wrapper_debug (* args , ** kwargs ):
112+ print (tag .format (name ))
87113 signature = getPPrintStr ({'kwargs' : kwargs , 'args' : args , })
88- print (f"{ tag . format ( f' { func . __module__ } . { func . __qualname__ } ' ) } \n { func . __qualname__ } (\n { signature } \n )" )
114+ print (f"{ name } (\n { signature } \n )" )
89115 result = func (* args , ** kwargs )
90- print (f"{ func . __name__ } returned { result !r } \n " )
116+ print (f"{ name } returned: \n { getPPrintStr ( result ) } \n " )
91117
92118 return result
93119 return wrapper_debug
94120
95121
96122
97- def check_time (* , cls : str or type = None , print_signature : bool = True , tag : str = DEFAULT_TAG ):
123+ def check_cls_time (* , cls : str or type = None , print_signature : bool = True , tag : str = DEFAULT_TAG ):
98124 """
99125 Print the function signature and return value
100126
@@ -107,9 +133,10 @@ def check_time(*, cls: str or type = None, print_signature: bool = True, tag: st
107133 cls = cls .__name__
108134
109135 def timeit (func : callable ):
136+ name = GetFunctionName (func )
110137 @functools .wraps (func )
111138 def timed (* args , ** kwargs ):
112- print (tag )
139+ print (tag . format ( name ) )
113140 if print_signature :
114141 try : args_repr = [repr (a ) for a in args ] # 1
115142 except : args_repr = [str (a ) for a in args ] # 1
@@ -125,21 +152,41 @@ def timed(*args, **kwargs):
125152
126153 start_time = time .time ()
127154 result = func (* args , ** kwargs )
128- print (f'{ func . __name__ } took { time .time () - start_time } ' )
129- print (f"{ func . __name__ } returned { result !r} \n " ) # 4
155+ print (f'{ name } took { time .time () - start_time } ' )
156+ print (f"{ name } returned { result !r} \n " ) # 4
130157 return result
131158
132159 return timed
133160 return timeit
134161
135162
136163
164+ def check_time (func : callable , tag : str = DEFAULT_TAG ):
165+ name = GetFunctionName (func )
166+ @functools .wraps (func )
167+ def timed (* args , ** kwargs ):
168+ _print_signature (func , tag , * args , ** kwargs )
169+
170+ start_time = time .time ()
171+ result = func (* args , ** kwargs )
172+ print (f'{ name } took { time .time () - start_time } ' )
173+ print (f"{ name } returned { result !r} \n " )
174+ return result
175+
176+ return timed
177+
178+
179+
137180def debugTkinterEvent (func : callable , tag : str = DEFAULT_TAG ):
181+ name = GetFunctionName (func )
138182 @functools .wraps (func )
139183 def wrapper_debug (self , event : Event , * args , ** kwargs ):
140- PRINT (f'{ tag .format ("TkinterEvent" )} \n { func .__class__ } .{ func .__name__ } .{ Event .__class__ } ' , event .__dict__ )
184+ print (tag .format (f'{ name } ' ))
185+ print (f'{ name } .{ event .__class__ } ' )
186+ pp .pprint (event .__dict__ )
141187
142188 result = func (self , event , * args , ** kwargs )
189+ print (f"{ name } returned { result !r} \n " )
143190
144191 return result
145192
0 commit comments