Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Decorators - Exercise
- ============================================================================
- #01. Logged
- def logged(func_ref):
- def wrapper(*args):
- func_result = func_ref(*args)
- return f'you called {func_ref.__name__}{args}\nit returned {func_result}'
- return wrapper
- @logged
- def func(*args):
- return 3 + len(args)
- print(func(4, 4, 4))
- @logged
- def sum_func(a, b):
- return a + b
- print(sum_func(1, 4))
- ============================================================================
- # 02. Even Parameters
- def even_parameters(func_ref):
- def wrapper(*args):
- for arg in args:
- if not isinstance(arg, int) or arg % 2 != 0:
- return "Please use only even numbers!"
- return func_ref(*args)
- return wrapper
- @even_parameters
- def add(a, b):
- return a + b
- print(add(2, 4))
- print(add("Peter", 1))
- @even_parameters
- def multiply(*nums):
- result = 1
- for num in nums:
- result *= num
- return result
- ============================================================================
- # 03. Bold, Italic, Underline
- def make_underline(func_ref):
- def wrapper(*args):
- func_result = func_ref(*args)
- return f'<u>{func_result}</u>'
- return wrapper
- def make_italic(func_ref):
- def wrapper(*args):
- func_result = func_ref(*args)
- return f'<i>{func_result}</i>'
- return wrapper
- def make_bold(func_ref):
- def wrapper(*args):
- func_result = func_ref(*args)
- return f'<b>{func_result}</b>'
- return wrapper
- @make_bold
- @make_italic
- @make_underline
- def greet(name):
- return f"Hello, {name}"
- print(greet("Peter"))
- @make_bold
- @make_italic
- @make_underline
- def greet_all(*args):
- return f"Hello, {', '.join(args)}"
- ============================================================================
- # 04. Type Check
- def type_check(type_):
- def decorator(func_ref):
- def wrapper(param):
- if not isinstance(param, type_):
- return 'Bad Type'
- return func_ref(param)
- return wrapper
- return decorator
- @type_check(int)
- def times2(num):
- return num * 2
- print(times2(2))
- print(times2('Not A Number'))
- @type_check(str)
- def first_letter(word):
- return word[0]
- ============================================================================
- # 05. Cache
- def cache(func_ref):
- log = {}
- def wrapper(num):
- if num in log:
- return log[num]
- result = func_ref(num)
- log[num] = result
- return result
- wrapper.log = log
- return wrapper
- @cache
- def fibonacci(n):
- if n < 2:
- return n
- return fibonacci(n - 1) + fibonacci(n - 2)
- ============================================================================
- # 06. HTML Tags
- def tags(tag_name):
- def decorator(func_ref):
- def wrapper(*args):
- func_res = func_ref(*args)
- return f'<{tag_name}>{func_res}</{tag_name}>'
- return wrapper
- return decorator
- @tags('p')
- def join_strings(*args):
- return "".join(args)
- print(join_strings("Hello", " you!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement