functools.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import absolute_import, print_function, unicode_literals
  2. import functools
  3. import collections
  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. >>> class MyClass(object):
  9. ... @save_method_args
  10. ... def method(self, a, b):
  11. ... print(a, b)
  12. >>> my_ob = MyClass()
  13. >>> my_ob.method(1, 2)
  14. 1 2
  15. >>> my_ob._saved_method.args
  16. (1, 2)
  17. >>> my_ob._saved_method.kwargs
  18. {}
  19. >>> my_ob.method(a=3, b='foo')
  20. 3 foo
  21. >>> my_ob._saved_method.args
  22. ()
  23. >>> my_ob._saved_method.kwargs == dict(a=3, b='foo')
  24. True
  25. The arguments are stored on the instance, allowing for
  26. different instance to save different args.
  27. >>> your_ob = MyClass()
  28. >>> your_ob.method({str('x'): 3}, b=[4])
  29. {'x': 3} [4]
  30. >>> your_ob._saved_method.args
  31. ({'x': 3},)
  32. >>> my_ob._saved_method.args
  33. ()
  34. """
  35. args_and_kwargs = collections.namedtuple('args_and_kwargs', 'args kwargs')
  36. @functools.wraps(method)
  37. def wrapper(self, *args, **kwargs):
  38. attr_name = '_saved_' + method.__name__
  39. attr = args_and_kwargs(args, kwargs)
  40. setattr(self, attr_name, attr)
  41. return method(self, *args, **kwargs)
  42. return wrapper