Last active
August 23, 2021 16:15
-
-
Save FFY00/f479773809a80fe35a93474c7e7dc961 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
def get_caller(level=1): | |
return inspect.stack()[level+1][3] | |
def log_call(func): | |
parameter_names = [ | |
parameter.name for parameter in inspect.signature(func).parameters.values() | |
] | |
def small_repr(value): | |
value_repr = repr(value) | |
if len(value_repr) > 128: | |
value_repr = value_repr[:128] + '...' | |
return value_repr | |
def print_arg(name, value): | |
print(f'\t{name} = {small_repr(value)} {type(value)}', file=sys.stderr) | |
def new_func(*args, **kwargs): | |
print(f'> {func.__qualname__} (called by {get_caller()})', file=sys.stderr) | |
for i, arg in enumerate(args): | |
print_arg(parameter_names[i], arg) | |
for name, value in kwargs.items(): | |
print_arg(name, value) | |
ret = func(*args, **kwargs) | |
print(f'< {func.__qualname__} => return {small_repr(ret)}', file=sys.stderr) | |
return ret | |
return new_func |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rich import print | |
import inspect | |
def get_caller(level=1): | |
return inspect.stack()[level+1][3] | |
def log_call(func): | |
parameter_names = [ | |
parameter.name for parameter in inspect.signature(func).parameters.values() | |
] | |
def small_repr(value): | |
value_repr = repr(value) | |
if len(value_repr) > 128: | |
value_repr = value_repr[:128] + '...' | |
return value_repr | |
def print_arg(name, value): | |
print(f'\t{name} = {small_repr(value)} {type(value)}', file=sys.stderr) | |
def new_func(*args, **kwargs): | |
print(f'[bright_black]> {func.__qualname__} (called by [bold]{get_caller()}[/bold])', file=sys.stderr) | |
for i, arg in enumerate(args): | |
print_arg(parameter_names[i], arg) | |
for name, value in kwargs.items(): | |
print_arg(name, value) | |
ret = func(*args, **kwargs) | |
print(f'[bright_black]< {func.__qualname__} => return', small_repr(ret), file=sys.stderr) | |
return ret | |
return new_func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment