Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2. Напишите параметризованный декоратор для классов, который будет считать и выводить время работы методов класса,
- # имена которых переданы в параметрах декоратора.
- # Пример:
- # @time_methods('inspect', 'finalize')
- # class Spam:
- # def __init__(self, s):
- # self.s = s
- # def inspect(self):
- # sleep(self.s)
- # def foo(self):
- # return self.s
- #
- # a = Spam(2)
- # a.inspect() # должно вывести сообщение о времени работы
- # a.foo() # ничего не выводить
- import time
- def time_methods(*args):
- def inner_decorator(specific_class):
- def wrapper(*other_args, **other_kwargs):
- # if название передаваемого метода in args:
- t = time.perf_counter()
- # вызов метода, что передали
- print("Среднее время работы: {:.0f} мс.".format(time.perf_counter() - t * 1000))
- return specific_class
- return wrapper
- return inner_decorator
- @time_methods('inspect', 'finalize')
- class Spam:
- def __init__(self, s):
- self.s = s
- def inspect(self):
- time.sleep(self.s)
- def foo(self):
- return self.s
- a = Spam(2)
- a.inspect() # должно вывести сообщение о времени работы
- a.foo() # ничего не выводить
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement