_functools.py 559 B

1234567891011121314151617181920
  1. import collections
  2. import functools
  3. # from jaraco.functools 4.0.2
  4. def save_method_args(method):
  5. """
  6. Wrap a method such that when it is called, the args and kwargs are
  7. saved on the method.
  8. """
  9. args_and_kwargs = collections.namedtuple('args_and_kwargs', 'args kwargs')
  10. @functools.wraps(method)
  11. def wrapper(self, /, *args, **kwargs):
  12. attr_name = '_saved_' + method.__name__
  13. attr = args_and_kwargs(args, kwargs)
  14. setattr(self, attr_name, attr)
  15. return method(self, *args, **kwargs)
  16. return wrapper