Skip to content

Instantly share code, notes, and snippets.

@aasmpro
Created April 16, 2020 17:05
Show Gist options
  • Save aasmpro/e961111ca93d174282ddae697b293891 to your computer and use it in GitHub Desktop.
Save aasmpro/e961111ca93d174282ddae697b293891 to your computer and use it in GitHub Desktop.
progress
from time import sleep
def progress(percent=0, width=30):
left = width * percent // 100
right = width - left
print('\r[', '#' * left, ' ' * right, ']',
f' {percent:.0f}%',
sep='', end='', flush=True)
for i in range(101):
progress(i)
sleep(0.1)
from itertools import cycle
from time import sleep
def spin(data, duration=0.2, check=lambda: False):
for frame in cycle(data):
if not check():
print('\r', frame, sep='', end='', flush=True)
sleep(duration)
else:
print('\r', '[OK] task done.', sep='', flush=True)
break
def check():
global time, duration
time += duration
return time >= 3
time = 0
duration = 0.2
spin(['... ',' ... ',' ... ',' ...',' ... ',' ... '], duration=duration, check=check)
print('program completed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment