/
elder247
/
aaa-python-advanced
Обзор
Документация
Войти
/
elder247
/
aaa-python-advanced
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
hw_3/task_2.py
39 строк
1 KB
Anton Trofimuk
add task_1.py, task_2.py, task_3.py
02 дек 2024, 20:44
02 дек 2024, 20:44
9dd77f4
Код
Авторство
О чём код?
import sys from datetime import datetime from functools import wraps from typing import Callable, NoReturn def timed_output(function: Callable) -> Callable: """ Decorator for transform sys.stdout.write Add timestamp at the beginning of each function output string """ @wraps(function) def wrapper(*args, **kwargs): original_write = sys.stdout.write def my_write(string_text: str) -> NoReturn: timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]: ") # Don't add timestamp to '\n' symbol caused by print function if string_text.strip(): string_text = timestamp + string_text original_write(string_text) sys.stdout.write = my_write # Do function and return sys.stdout.write to original_write try: return function(*args, **kwargs) finally: sys.stdout.write = original_write return wrapper @timed_output def print_greeting(name): print(f'Hello, {name}!') if __name__ == '__main__': print_greeting("Nikita")