Guest User

Untitled

a guest
May 17th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """Basis of Python Decorator"""
  5.  
  6. def decorate(function):
  7. """Decolator function:
  8. This decorator overrides original 'function' and
  9. returns 'wrap_function'.
  10. """
  11. def wrap_function(*args, **kwargs):
  12. """Wrapper function:
  13. This wrapper receives arguments of 'function' and
  14. returns additional messeage with original 'function'.
  15. """
  16. print 'Hello from decorated %s.' % function.__name__
  17. obj = function(*args, **kwargs)
  18. return obj
  19. return wrap_function
  20.  
  21. def decorate_with_arg(str):
  22. """Decorator function with arguments:
  23. This decorator receives an argument 'str' and
  24. returns 'decorate' function like as mentioned above.
  25. """
  26. def decorate(function):
  27. """Decorator function:
  28. This decorator overrides original 'function' and
  29. returns 'wrap_function'.
  30. """
  31. def wrap_function(*args, **kwargs):
  32. """Wrapper function:
  33. This wrapper receives arguments of 'function' and
  34. returns additional messeage including decorator's
  35. argument with original 'function'.
  36. """
  37. print 'Hi %s from decorated %s' % (str, function.__name__)
  38. obj = function(*args, **kwargs)
  39. return obj
  40. return wrap_function
  41. return decorate
  42.  
  43. @decorate
  44. def test_decorator(name):
  45. """Decorated function:
  46. This function just shows a message with the argument.
  47. **Recommended way to be decorated**
  48. """
  49. print 'Hey %s from decorating %s.' % (name, test_decorator.__name__)
  50.  
  51. def test_alt_decorator(name):
  52. """Function:
  53. This function just shows a message with the argument.
  54. **Followed code is obsolete way to decorate this function**
  55. """
  56. print 'Hey %s from decorating %s.' % (name, test_alt_decorator.__name__)
  57. test_alt_decorator = decorate(test_alt_decorator)
  58.  
  59. @decorate
  60. @decorate_with_arg('Anonymous')
  61. def test_decorators():
  62. """Decorated function by 2 decorators:
  63. This function just shows a messeage.
  64. **Recommended way to be decorated by several decorators and
  65. decorators having arguments**
  66. """
  67. print 'Hiya %s.' % test_decorators.__name__
  68.  
  69. def test_alt_decorators():
  70. """Function:
  71. This function just shows a messeage.
  72. **Followed codes are obsolete way to decorate this function
  73. with several decorators having arguments**
  74. """
  75. print 'Hiya %s.' % test_alt_decorators.__name__
  76. wrapper = decorate_with_arg('Doe')
  77. test_alt_decorators = decorate(wrapper(test_alt_decorators))
  78.  
  79. def start_test():
  80. """Function:
  81. This function just tests decorator functions and
  82. decorated functions as declared above.
  83. """
  84. # Run decorated functions
  85. print '#1'
  86. test_decorator('John')
  87. print '#2'
  88. test_alt_decorator('Coward')
  89. print '#3'
  90. test_decorators()
  91. print '#4'
  92. test_alt_decorators()
  93.  
  94. if __name__ == '__main__':
  95. start_test()
Add Comment
Please, Sign In to add comment