Advertisement
GeorgiLukanov87

Decorators - Exercise

Dec 9th, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. # Decorators - Exercise
  2.  
  3. ============================================================================
  4.  
  5. #01. Logged
  6.  
  7. def logged(func_ref):
  8.     def wrapper(*args):
  9.         func_result = func_ref(*args)
  10.         return f'you called {func_ref.__name__}{args}\nit returned {func_result}'
  11.  
  12.     return wrapper
  13.  
  14.  
  15. @logged
  16. def func(*args):
  17.     return 3 + len(args)
  18.  
  19.  
  20. print(func(4, 4, 4))
  21.  
  22.  
  23. @logged
  24. def sum_func(a, b):
  25.     return a + b
  26.  
  27.  
  28. print(sum_func(1, 4))
  29. ============================================================================
  30.  
  31. # 02. Even Parameters
  32.  
  33. def even_parameters(func_ref):
  34.     def wrapper(*args):
  35.         for arg in args:
  36.             if not isinstance(arg, int) or arg % 2 != 0:
  37.                 return "Please use only even numbers!"
  38.         return func_ref(*args)
  39.  
  40.     return wrapper
  41.  
  42.  
  43. @even_parameters
  44. def add(a, b):
  45.     return a + b
  46.  
  47.  
  48. print(add(2, 4))
  49. print(add("Peter", 1))
  50.  
  51.  
  52. @even_parameters
  53. def multiply(*nums):
  54.     result = 1
  55.     for num in nums:
  56.         result *= num
  57.     return result
  58. ============================================================================
  59.  
  60. # 03. Bold, Italic, Underline
  61.  
  62. def make_underline(func_ref):
  63.     def wrapper(*args):
  64.         func_result = func_ref(*args)
  65.         return f'<u>{func_result}</u>'
  66.     return wrapper
  67.  
  68.  
  69. def make_italic(func_ref):
  70.     def wrapper(*args):
  71.         func_result = func_ref(*args)
  72.         return f'<i>{func_result}</i>'
  73.     return wrapper
  74.  
  75.  
  76. def make_bold(func_ref):
  77.     def wrapper(*args):
  78.         func_result = func_ref(*args)
  79.         return f'<b>{func_result}</b>'
  80.     return wrapper
  81.  
  82.  
  83. @make_bold
  84. @make_italic
  85. @make_underline
  86. def greet(name):
  87.     return f"Hello, {name}"
  88.  
  89.  
  90. print(greet("Peter"))
  91.  
  92.  
  93. @make_bold
  94. @make_italic
  95. @make_underline
  96. def greet_all(*args):
  97.     return f"Hello, {', '.join(args)}"
  98. ============================================================================
  99.  
  100. # 04. Type Check
  101.  
  102. def type_check(type_):
  103.     def decorator(func_ref):
  104.         def wrapper(param):
  105.             if not isinstance(param, type_):
  106.                 return 'Bad Type'
  107.             return func_ref(param)
  108.  
  109.         return wrapper
  110.  
  111.     return decorator
  112.  
  113.  
  114. @type_check(int)
  115. def times2(num):
  116.     return num * 2
  117.  
  118.  
  119. print(times2(2))
  120. print(times2('Not A Number'))
  121.  
  122.  
  123. @type_check(str)
  124. def first_letter(word):
  125.     return word[0]
  126. ============================================================================
  127. # 05. Cache
  128.  
  129. def cache(func_ref):
  130.     log = {}
  131.  
  132.     def wrapper(num):
  133.         if num in log:
  134.             return log[num]
  135.         result = func_ref(num)
  136.         log[num] = result
  137.  
  138.         return result
  139.  
  140.     wrapper.log = log
  141.     return wrapper
  142.  
  143.  
  144. @cache
  145. def fibonacci(n):
  146.     if n < 2:
  147.         return n
  148.     return fibonacci(n - 1) + fibonacci(n - 2)
  149. ============================================================================
  150.  
  151. # 06. HTML Tags
  152.  
  153. def tags(tag_name):
  154.     def decorator(func_ref):
  155.         def wrapper(*args):
  156.             func_res = func_ref(*args)
  157.             return f'<{tag_name}>{func_res}</{tag_name}>'
  158.  
  159.         return wrapper
  160.  
  161.     return decorator
  162.  
  163.  
  164. @tags('p')
  165. def join_strings(*args):
  166.     return "".join(args)
  167.  
  168.  
  169. print(join_strings("Hello", " you!"))
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement