11import functools
2- from .base import GetFunctionName , DEFAULT_TAG
2+ from itertools import count
3+
4+ from .base import GetFunctionName , DEFAULT_TAG , END_TAG
35
46from .console import getPPrintStr
57
8+ __all__ = ['chain' , 'sub' ]
9+
10+
11+ class _AutoCounter (object ):
12+ _counter : count
13+ def __init__ (self , * , start : int = 0 , step : int = 1 ):
14+ self ._value = start
15+ self .reset (start = start , step = step )
16+ def __call__ (self , * args , ** kwargs ) -> int :
17+ self ._value = self ._next ()
18+ return self ._value
19+ @property
20+ def value (self ) -> int :
21+ return self ._value
22+ def reset (self , * , start : int = 0 , step : int = 1 ):
23+ self ._counter = count (start = start , step = step )
24+ self ._next = self ._counter .__next__
625
726
27+ _counter = _AutoCounter ()
828
9- __all__ = ['top_debug' , 'sub_level' , ]
1029
11- def print_chain_signature (func : callable , tag : str , level : int or str , signature : bool , * args , ** kwargs ):
30+ def _print_chain_signature (func : callable , tag : str , level : int or str , signature : bool , * args , ** kwargs ):
1231 assert ('{0}' in tag )
1332 name = GetFunctionName (func )
14- print (tag .format (f'{ level } ' ))
33+ print (tag .format (f'{ level } --> { name } ' ))
1534
1635 if signature and (args or kwargs ):
1736 signature = getPPrintStr ({ 'kwargs' : kwargs , 'args' : args , })
1837 print (f"{ name } (\n { signature } \n )" )
1938 result = func (* args , ** kwargs )
2039 print (f"{ name } returned: \n { getPPrintStr (result )} \n " )
2140
22- def top_debug (func : callable , tag : str = DEFAULT_TAG ):
41+
42+
43+ def chain (start_tag : str = DEFAULT_TAG , end_tag : str = END_TAG , start : int = 1 ):
2344 """
2445 Print the function signature and return value
2546
26- :param func: callable function to be debugged .
27- :param tag : a unique string to identify the output in the console window.
47+ :param end_tag: a unique string to identify the ENDING of the chain in the console window .
48+ :param start_tag : a unique string to identify the START of the chain in the console window.
2849 :return:
2950 """
30- name = GetFunctionName (func )
51+ _counter .reset (start = start )
52+ def top (func : callable ):
53+ """
54+ :param func: callable function to be debugged.
55+ :return:
56+ """
57+ name = GetFunctionName (func )
3158
32- @functools .wraps (func )
33- def wrapper_debug (* args , ** kwargs ):
34- print (tag .format (name ))
35- signature = getPPrintStr ({ 'kwargs' : kwargs , 'args' : args , })
36- print (f"{ name } (\n { signature } \n )" )
37- result = func (* args , ** kwargs )
38- print (f"{ name } returned: \n { getPPrintStr (result )} \n " )
59+ @functools .wraps (func )
60+ def wrapper_debug (* args , ** kwargs ):
61+ print (start_tag .format (name ))
62+ signature = getPPrintStr ({ 'kwargs' : kwargs , 'args' : args , })
63+ print (f"{ name } (\n { signature } \n )" )
64+ result = func (* args , ** kwargs )
65+ print (f"{ name } returned: \n { getPPrintStr (result )} \n " )
66+ print (end_tag )
3967
40- return result
41- return wrapper_debug
68+ return result
69+ return wrapper_debug
70+ return top
71+
72+ # class callback(object):
73+ # def __init__(self, func: callable, name: str, tag: str, signature: bool ):
74+ # self._signature = signature
75+ # self._tag = tag
76+ # self._name = name
77+ # self._func = func
78+ # def __call__(self, *args, **kwargs):
79+ # _print_chain_signature(self._func, self._tag, _counter(), self._signature, *args, **kwargs)
80+ # result = self._func(*args, **kwargs)
81+ # print(f"{self._name} returned {result!r}\n")
82+ # return result
4283
43- def sub_level ( level : str or str , * , tag : str = '-------------- level: {0}' , signature : bool = True ):
84+ def sub ( * , tag : str = '-------------- level: {0}' , signature : bool = False ):
4485 """
4586 Print the function signature [Optional] and return value.
4687
47- :param signature: for sub-level method chains, prints it's signature. defaults to true .
88+ :param signature: for sub-level method chains, prints it's signature. defaults to False .
4889 :param level: the call stack level. f() -> g() -> h() -> etc.
4990 :param tag: a unique string to identify the output in the console window. must have one '{0}' for str.format() support.
5091 :return:
5192 """
52- def sub (func : callable ):
93+
94+ def wrapped (func : callable ):
5395 """
5496 :param func: callable function to be debugged.
5597 :return:
@@ -58,10 +100,10 @@ def sub(func: callable):
58100
59101 @functools .wraps (func )
60102 def wrapper_debug (* args , ** kwargs ):
61- print_chain_signature (func , tag , level , signature , * args , ** kwargs )
103+ # return callback(func, name, tag, signature)
104+ _print_chain_signature (func , tag , _counter (), signature , * args , ** kwargs )
62105 result = func (* args , ** kwargs )
63106 print (f"{ name } returned { result !r} \n " )
64-
65107 return result
66108 return wrapper_debug
67- return sub
109+ return wrapped
0 commit comments