Skip to content

Instantly share code, notes, and snippets.

@quanon
Last active November 16, 2017 11:19
Show Gist options
  • Save quanon/05a50ef26dcf9d681f73296ffc74d359 to your computer and use it in GitHub Desktop.
Save quanon/05a50ef26dcf9d681f73296ffc74d359 to your computer and use it in GitHub Desktop.
@contextlib.contextmanager を使ってみよう!
import contextlib
@contextlib.contextmanager
def overwriting():
import sys
original_write = sys.stdout.write
def overwrite(text):
original_write('\033[K') # カーソル位置から行末までを消す。
original_write('\r') # カーソルを行頭に移動する。
original_write(text.rstrip('\n'))
sys.stdout.write = overwrite
try:
yield
except BaseException:
raise
finally:
sys.stdout.write = original_write
print('※ sys.stdout.write を元に戻しました。')
if __name__ == '__main__':
import time
with overwriting():
print('カウントダウン開始!')
time.sleep(0.4)
for i in range(10, -1, -1):
# print(3628800 / i)
print(i)
time.sleep(0.4)
print('🎉')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment