-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasybar.py
36 lines (32 loc) · 1.43 KB
/
easybar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""A simple and easily customizable (in code) progress bar
You can cut and paste this exampe to ipython to see how it works:
from entity_extractor.utils import easybar
import time
my_list = range(100)
total = len(my_list)
for iteration in range(total):
my_operation = str(my_list[iteration])
time.sleep(0.1)
easybar.print_progress(iteration, total-1, prefix='Progress')
"""
import sys
# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
str_format = "{0:." + str(decimals) + "f}"
percents = str_format.format(100 * (iteration / float(total)))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '█' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()