python:decorator
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
python:decorator [2018/03/15 00:10] – rex8312 | python:decorator [2024/03/23 02:42] (현재) – 바깥 편집 127.0.0.1 | ||
---|---|---|---|
줄 26: | 줄 26: | ||
time.sleep(t) | time.sleep(t) | ||
return True | return True | ||
+ | </ | ||
+ | |||
+ | | ||
+ | ====== Logger wrapping ====== | ||
+ | |||
+ | terminal 창의 마지막 줄에 elapsed time을 계속 표시함 | ||
+ | |||
+ | <code python> | ||
+ | def logger_info_with_state_bar(func): | ||
+ | |||
+ | global start_time | ||
+ | |||
+ | def wrapped(msg): | ||
+ | columns = os.get_terminal_size().columns | ||
+ | print(Fore.RESET + Back.RESET + ' ' * (columns - 1), end=' | ||
+ | func(msg) | ||
+ | status = ' | ||
+ | if len(status) < (columns - 1): | ||
+ | status += ' ' * (columns - len(status) - 1) | ||
+ | print(Back.GREEN + Fore.BLACK + status + Fore.RESET + Back.RESET, end=' | ||
+ | |||
+ | return wrapped | ||
+ | |||
+ | logger.info = logger_info_with_state_bar(logger.info) | ||
+ | </ | ||
+ | | ||
+ | ===== Decorator with parameters ===== | ||
+ | |||
+ | <code python> | ||
+ | def decorator_factory(argument): | ||
+ | def decorator(function): | ||
+ | def wrapper(*args, | ||
+ | funny_stuff() | ||
+ | something_with_argument(argument) | ||
+ | result = function(*args, | ||
+ | more_funny_stuff() | ||
+ | return result | ||
+ | return wrapper | ||
+ | return decorator | ||
+ | | ||
+ | # https:// | ||
</ | </ | ||
python/decorator.1521072639.txt.gz · 마지막으로 수정됨: (바깥 편집)