-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_bar.py
More file actions
44 lines (41 loc) · 2.31 KB
/
progress_bar.py
File metadata and controls
44 lines (41 loc) · 2.31 KB
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
37
38
39
40
41
42
43
44
import math as m
class ProgressionBar:
def __init__(self, n_steps_end, n_steps_start=0, bar_length=50, empty_char=' ', fill_char='\u25AA', first_limit_char='|', last_limit_char='|', progression_separator_str="|", desc=''):
self.n_steps_start = int(n_steps_start)
self.n_steps_end = int(n_steps_end)
self.current_step = self.n_steps_start-1
self.bar_length = max(5, int(bar_length))
self.empty_char = str(empty_char)
self.fill_char = str(fill_char)
self.first_limit_char = str(first_limit_char)
self.last_limit_char = str(last_limit_char)
self.progression_separator_str = str(progression_separator_str)
self.n_empty_chars = self.bar_length
self.n_fill_chars = 0
self.n_progression_ratio = 0.0
self.progression_bar = ''
self.progression_str = ''
self.progression_bar_str = ''
self.desc = str(desc)
self.update_progression()
def __str__(self):
return(self.progression_bar_str)
def update_progression(self, n_progress_steps=1):
if (self.n_steps_end < self.n_steps_start):
min_step = self.n_steps_end
self.n_steps_end = self.n_steps_start
self.n_steps_start = min_step
if (self.current_step < self.n_steps_end):
self.current_step += n_progress_steps
if (self.n_steps_end <= self.n_steps_start):
self.n_progression_ratio = 1.0
else:
self.n_progression_ratio = self.current_step / (self.n_steps_end - self.n_steps_start)
self.n_fill_chars = m.ceil(self.bar_length*self.n_progression_ratio)
self.n_empty_chars = self.bar_length - self.n_fill_chars
self.progression_bar = self.fill_char*self.n_fill_chars + self.empty_char*self.n_empty_chars
self.progression_str = "[{}/{} {} {:.2%}]".format(self.current_step, self.n_steps_end, self.progression_separator_str, self.n_progression_ratio)
if self.desc != '':
self.progression_bar_str = self.desc + ': ' + self.first_limit_char + self.progression_bar + self.last_limit_char + ' ' + self.progression_str
else:
self.progression_bar_str = self.first_limit_char + self.progression_bar + self.last_limit_char + ' ' + self.progression_str