1) 1ая задача
Create a decorator function time_decorator which has to calculate the decorated function execution time and put this time value in the execution_time dictionary where the key is the decorated function name. The value is this function's execution time. For example:
@time_decorator
def func_add(a, b):
sleep(0.2)
return a + b
>>> func_add(10, 20)
30
>>> execution_time['func_add']
0.212341254
2) 2ая задача
Write a decorator that logs information about calls of decorated functions, the values of its arguments, keyword arguments, and execution time. The log should be written to a file.
@log
def foo(a, b, c):
...
foo(1, 2, c=3)
...
foo; args: a=1, b=2; kwargs: c=3; execution time: 0.12 sec.
...
3) 3я задача
Create decorator validate, which validates arguments in the set_pixel function. All function parameters should be between 0(int) and 256(int) inclusive.
If all parameters are valid, the set_pixel function should return a "Pixel created!" message. Otherwise, it should return the "Function call is not valid!" message.
Use functools.wraps where necessary.
Don't forget about doc strings.
Examples
>>> set_pixel(0, 127, 300)
Function call is not valid!
>>> set_pixel(0,127,250)
Pixel created!
4) 4я задача
Decorators FactoryCreate a decorator factory (decorator itself). The factory accepts a function (lambda) as an input and returns a decorator that will return the result of the function as the first argument. The result of the decorated function is passed. The function that the factory accepts (in the example below, it is a lambda) can only take one positional parameter.
For example:
>>> @decorator_apply(lambda user_id: user_id + 1)
>>> def return_user_id(num: int):
return num
>>> return_user_id(42)
>>> 43
Гарантия на работу | 1 год |
Средний балл | 4.96 |
Стоимость | Назначаете сами |
Эксперт | Выбираете сами |
Уникальность работы | от 70% |