Skip to content

Instantly share code, notes, and snippets.

@d3rp
Created August 20, 2019 06:16
Show Gist options
  • Save d3rp/b722b097efb7d3790f47fb4dee6de272 to your computer and use it in GitHub Desktop.
Save d3rp/b722b097efb7d3790f47fb4dee6de272 to your computer and use it in GitHub Desktop.
self reporting class wrapper
import inspect
def dec(f, *args, **kwargs):
def inner(*args, **kwargs):
# TODO: default params from Parameter
# https://docs.python.org/3/library/inspect.html#inspect.Parameter
print(f'{args[0].__class__.__name__}.{f.__name__}(', end='')
if len(args) > 1:
params = list(inspect.signature(f).parameters.keys())[1:]
for p, a in zip(params[:-1], args[1:-1]):
print(f'{p}={a}', end=', ')
print(f'{params[-1]}={args[-1]}', end='')
print(')')
f(*args, **kwargs)
return inner
def class_dec(cls):
for key, attr in cls.__dict__.items():
if not key.startswith('_'):
setattr(cls, key, dec(attr))
return cls
@class_dec
class Boo:
def m(self, x=1):
print('inside method')
pass
b = Boo()
b.m()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment