I created a very primitive progress bar/wheel in Python which looks like this:
if round(i/len(some_list)*100, 1).is_integer():
for frame in cycle(r'-\|/-\|/'):
print("\r", frame, " ", round(i/len(some_list)*100,1), "%", sep = "", end = "", flush=True)
updated code for easier replication (added as per request in the comments)
from itertools import cycle
import random as rd
rnd_lst = []
for i in range(0,5000):
n = rd.randint(1,30)
rnd_lst.append(n)
def pb_wheel(lst):
tick=0
for frame in cycle(r'-\|/-\|/'):
if tick < len(lst):
print("\r", frame, " ", round(tick/len(lst)*100, 2), "%", sep = "", end = "", flush=True)
tick += 1
else:
print("\r", "100%", flush=True)
print("\n", "Done")
break
pb_wheel(rnd_lst)
It works reasonably well at first with the output looking like expected:
\ 0.0%
But after the initial success, after about 5-6 seconds parts of the pb/pw
get duplicated:
- 0.0% % % 0.0% - 0.0% 0.0%
I assume that the flush is too slow. I tested this by adding a time delay time.sleep(0.01)
which reduces the occurrence of duplications but does not quite eliminate them. As I don't actually want to artificially slow down the code I wanted to ask if there is a solution to this problem.
I am working with Spyder on a Windows OS.