13
#TODO: split helper functions into a helper-functions module.
16
""" THIS IS NOT A DECORATOR,
17
like py3k's print function, but controllable like logging. """
26
#TODO: import some memoising decorators
12
27
def only_once(func):
24
def run(*args, **kwargs):
39
def call(*args, **kwargs):
27
42
func.retval = func(*args, **kwargs)
47
#TODO: write guard decorator
48
def debug_exceptions(func):
49
"""Useful if some calling function is ignoring exceptions you raised
50
(eg threads, mainloops)"""
51
def call(*args, **kwargs):
53
return func(*args, **kwargs)
59
#TODO: write an "only for functions" decorator, to trivialise this.
60
def debug_method(func, prefix='', ignore_self=False):
61
if not isinstance(func, (types.FunctionType, types.MethodType)):
62
return func #untouched, as it's not a func
64
def call(*args, **kwargs):
65
callstr = (prefix + func.func_name + '(' + ', '.join(
66
list(map(repr, args[ignore_self:])) +
67
[k+'='+repr(v) for k,v in kwargs.items()]
69
dprint(' '*debug_method.depth, callstr)
70
debug_method.depth += 1
71
retval = debug_exceptions(func)(*args, **kwargs)
72
debug_method.depth -= 1
73
dprint(' '*debug_method.depth, '->', retval)
76
debug_method.depth = 0
78
def debug_class(cls, prefix='', exclude=['Get']):
79
prefix = prefix + cls.__name__ + '.'
81
if name.strip('_') == name and name not in exclude: #not private or magic
82
attr = getattr(cls, name)
83
attr = debug_method(attr, prefix, True)
84
setattr(cls, name, attr)
b'\\ No newline at end of file'