Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 4, 2021. It is now read-only.

Commit 397d15f

Browse files
committed
updated methods in decorators.py. added chains.py and base.py.
1 parent bbe3c66 commit 397d15f

File tree

5 files changed

+118
-33
lines changed

5 files changed

+118
-33
lines changed

‎src/PyDebug/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .colors import *
77
from .__version__ import *
88

9+
import PyDebug.chains as Chains
910

1011
def get_size(obj, seen=None):
1112
"""Recursively finds size of objects"""

‎src/PyDebug/__version__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22

3-
version = '1.4.0'
3+
version = '1.4.5'

‎src/PyDebug/base.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from .console import getPPrintStr
2+
3+
4+
5+
6+
DEFAULT_TAG = '\n______________________________________________________________\n"{0}"'
7+
8+
def GetFuncModule(func: callable) -> str:
9+
return func.__module__
10+
11+
12+
13+
def GetFunctionName(func: callable) -> str:
14+
if hasattr(func, '__qualname__'):
15+
return func.__qualname__
16+
elif hasattr(func, '__module__'):
17+
return f"{func.__module__}.{func.__qualname__}"
18+
else:
19+
return func.__name__
20+
21+
22+
23+
def print_signature(func: callable, tag: str, *args, **kwargs):
24+
assert ('{0}' in tag)
25+
name = GetFunctionName(func)
26+
print(tag.format(f'{name}'))
27+
28+
if args or kwargs:
29+
# try: args_repr = [repr(a) for a in args] # 1
30+
# except: args_repr = [str(a) for a in args] # 1
31+
#
32+
# kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()] # 2
33+
#
34+
# signature = ", ".join(args_repr + kwargs_repr) # 3
35+
#
36+
# print(f"{name}(\n{signature}\n)")
37+
signature = getPPrintStr({'kwargs': kwargs, 'args': args, })
38+
print(f"{name}(\n {signature}\n )")
39+
result = func(*args, **kwargs)
40+
print(f"{name} returned: \n{getPPrintStr(result)}\n")

‎src/PyDebug/chains.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import functools
2+
from .base import GetFunctionName, DEFAULT_TAG
3+
4+
from .console import getPPrintStr
5+
6+
7+
8+
9+
__all__ = ['top_debug', 'sub_level', ]
10+
11+
def print_chain_signature(func: callable, tag: str, level: int or str, signature: bool, *args, **kwargs):
12+
assert ('{0}' in tag)
13+
name = GetFunctionName(func)
14+
print(tag.format(f'{level}'))
15+
16+
if signature and (args or kwargs):
17+
signature = getPPrintStr({ 'kwargs': kwargs, 'args': args, })
18+
print(f"{name}(\n {signature}\n )")
19+
result = func(*args, **kwargs)
20+
print(f"{name} returned: \n{getPPrintStr(result)}\n")
21+
22+
def top_debug(func: callable, tag: str = DEFAULT_TAG):
23+
"""
24+
Print the function signature and return value
25+
26+
:param func: callable function to be debugged.
27+
:param tag: a unique string to identify the output in the console window.
28+
:return:
29+
"""
30+
name = GetFunctionName(func)
31+
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")
39+
40+
return result
41+
return wrapper_debug
42+
43+
def sub_level(level: str or str, *, tag: str = '-------------- level: {0}', signature: bool = True):
44+
"""
45+
Print the function signature [Optional] and return value.
46+
47+
:param signature: for sub-level method chains, prints it's signature. defaults to true.
48+
:param level: the call stack level. f() -> g() -> h() -> etc.
49+
:param tag: a unique string to identify the output in the console window. must have one '{0}' for str.format() support.
50+
:return:
51+
"""
52+
def sub(func: callable):
53+
"""
54+
:param func: callable function to be debugged.
55+
:return:
56+
"""
57+
name = GetFunctionName(func)
58+
59+
@functools.wraps(func)
60+
def wrapper_debug(*args, **kwargs):
61+
print_chain_signature(func, tag, level, signature, *args, **kwargs)
62+
result = func(*args, **kwargs)
63+
print(f"{name} returned {result!r}\n")
64+
65+
return result
66+
return wrapper_debug
67+
return sub

‎src/PyDebug/decorators.py‎

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
import functools
22
import time
33
from tkinter import Event
4-
4+
from .baseimport*
55
from .console import getPPrintStr, pp
66

77

88

9-
109
__all__ = ['debug', 'class_method_debug', 'check_time', 'debugTkinterEvent', 'pprint_debug']
1110

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-
4011

4112
def class_method_debug(cls: str or type, tag: str = DEFAULT_TAG):
4213
"""
@@ -57,6 +28,7 @@ def debug_inner(func: callable = None):
5728
:return:
5829
"""
5930
name = f"{cls}.{func.__name__}"
31+
6032
@functools.wraps(func)
6133
def wrapper_debug(*args, **kwargs):
6234
print(tag.format(name))
@@ -87,9 +59,10 @@ def debug(func: callable, tag: str = DEFAULT_TAG):
8759
:return:
8860
"""
8961
name = GetFunctionName(func)
62+
9063
@functools.wraps(func)
9164
def wrapper_debug(*args, **kwargs):
92-
_print_signature(func, tag, *args, **kwargs)
65+
print_signature(func, tag, *args, **kwargs)
9366
result = func(*args, **kwargs)
9467
print(f"{name} returned {result!r}\n") # 4
9568

@@ -107,6 +80,7 @@ def pprint_debug(func: callable, tag: str = DEFAULT_TAG):
10780
:return:
10881
"""
10982
name = GetFunctionName(func)
83+
11084
@functools.wraps(func)
11185
def wrapper_debug(*args, **kwargs):
11286
print(tag.format(name))
@@ -134,6 +108,7 @@ def check_cls_time(*, cls: str or type = None, print_signature: bool = True, tag
134108

135109
def timeit(func: callable):
136110
name = GetFunctionName(func)
111+
137112
@functools.wraps(func)
138113
def timed(*args, **kwargs):
139114
print(tag.format(name))
@@ -163,9 +138,10 @@ def timed(*args, **kwargs):
163138

164139
def check_time(func: callable, tag: str = DEFAULT_TAG):
165140
name = GetFunctionName(func)
141+
166142
@functools.wraps(func)
167143
def timed(*args, **kwargs):
168-
_print_signature(func, tag, *args, **kwargs)
144+
print_signature(func, tag, *args, **kwargs)
169145

170146
start_time = time.time()
171147
result = func(*args, **kwargs)
@@ -179,6 +155,7 @@ def timed(*args, **kwargs):
179155

180156
def debugTkinterEvent(func: callable, tag: str = DEFAULT_TAG):
181157
name = GetFunctionName(func)
158+
182159
@functools.wraps(func)
183160
def wrapper_debug(self, event: Event, *args, **kwargs):
184161
print(tag.format(f'{name}'))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /