diff --git a/lab-spinner/spinner_await.py b/lab-spinner/spinner_await.py index f4d481e..49a43ea 100644 --- a/lab-spinner/spinner_await.py +++ b/lab-spinner/spinner_await.py @@ -8,21 +8,17 @@ import asyncio import itertools -import sys async def spin(msg): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/—\\'): status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) + print('\r' + status, end='', flush=True) try: await asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + print('\r', end='', flush=True) async def slow_function(): # <4> diff --git a/lab-spinner/spinner_curio.py b/lab-spinner/spinner_curio.py index 0af06d8..f1b4f65 100755 --- a/lab-spinner/spinner_curio.py +++ b/lab-spinner/spinner_curio.py @@ -11,21 +11,17 @@ import curio import itertools -import sys async def spin(msg): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) + print('\r' + status, end='', flush=True) try: await curio.sleep(.1) # <2> except curio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + print('\r', end='') async def slow_function(): # <4> diff --git a/lab-spinner/spinner_thread.py b/lab-spinner/spinner_thread.py index 9935bc5..d8dd923 100755 --- a/lab-spinner/spinner_thread.py +++ b/lab-spinner/spinner_thread.py @@ -9,19 +9,15 @@ import threading import itertools import time -import sys def spin(msg, done): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) + print('\r' + status, end='', flush=True) if done.wait(.1): # <2> break # <3> - write(' ' * len(status) + '\x08' * len(status)) + print('\r', end='') def slow_function(): # <4> diff --git a/lab-spinner/spinner_yield.py b/lab-spinner/spinner_yield.py index c6f94fc..009ab03 100755 --- a/lab-spinner/spinner_yield.py +++ b/lab-spinner/spinner_yield.py @@ -8,22 +8,18 @@ import asyncio import itertools -import sys @asyncio.coroutine # <1> def spin(msg): - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\x08' * len(status)) + print('\r' + status, end='', flush=True) try: yield from asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + print('\r', end='') @asyncio.coroutine # <4>