{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
324 lines (256 loc) · 9.86 KB
/
Copy pathprogress.py
File metadata and controls
324 lines (256 loc) · 9.86 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import contextlib
import logging
import math
import sys
import time
from typing import Optional
from .mathing import remap_range
LOGGER = logging.getLogger(__name__)
class ProgressBar:
"""
A simple progress bar displayed in the given stream.
The recommended usage is to ``start()`` the progress, then call ``add_progress()``
during the measured process, and finished by calling ``end()``.
You can also create an "infinite" progress bar by not calling ``add_progress`` or
``set_progress`` and just calling ``update()`` instead.
Tips: you can add coloring by adding the color special character in the
prefix and suffix strings.
**Text Formatting**
Prefix and suffix are formatted with the following tokens :
* ``bar_min``: minimal value of the bar specified by user
* ``bar_max``: maximal value of the bar specified by user
* ``bar_index``: the current value of the bar
* ``elapsed_time``: amount of time since first progress call, in seconds.
String are formatted as usual with the ``str.format`` function.
Example::
suffix="[{bar_index:<2n}/{bar_max}] elapsed {elapsed_time:.2f}s"
**Changing style**
To change the bar style you can subclass it and override the class attributes.
Example::
class MyProgressBar(ProgressBar):
bar_fill = "#"
**Future improvement**
* Missing time estimation features.
Args:
min_value: value that is considered the start of the progress bar
max_value: value that is considered the end of the progress bar
width: width of the progress bar in number of characters, excluding contextual info
prefix:
string to format and add before the progress bar. see formatting documentation.
suffix:
string to format and add after the progress bar. see formatting documentation.
stream: IO object to write the progress bar to
hide_cursor: hide blinking cursor in stdout while the progress bar is displaying
"""
bar_before = " |"
bar_after = "| "
# use only one character long string else unexpected behavior could occur.
bar_fill = "█"
bar_empty = " "
def __init__(
self,
min_value: float = 0.0,
max_value: float = 1.0,
width: int = 32,
prefix: str = "",
suffix: str = "",
stream=sys.stdout,
hide_cursor: bool = True,
):
# those can be set at any time
self.prefix: str = prefix
self.suffix: str = suffix
self._width: int = width
self._user_index: float = min_value
self._user_min: float = min_value
self._user_max: float = max_value
self._stream = stream
self._is_ended: bool = False
self._had_progress: bool = False
self._start_time: Optional[float] = None
self._hide_cursor: bool = hide_cursor
self._is_cursor_visible: bool = True
self._update_number: int = 0
def __del__(self):
# safety to ensure the cursor is set back to visible once the object is removed
self.set_cursor_visibility(visible=True)
@property
def max_value(self) -> float:
return self._user_max
@max_value.setter
def max_value(self, new_max_value: float):
self._user_max = new_max_value
if self._had_progress:
self.update()
@property
def min_value(self) -> float:
return self._user_min
@min_value.setter
def min_value(self, new_min_value: float):
self._user_min = new_min_value
if self._had_progress:
self.update()
@property
def _bar_width(self) -> int:
"""
Returns:
current width in characters units of the bar
"""
bar_index = remap_range(
value=self._user_index,
source_min=self._user_min,
source_max=self._user_max,
target_min=0.0,
target_max=self._width,
)
return math.floor(bar_index)
@property
def _time_elapsed(self) -> float:
"""
Returns:
time elapsed since start, in seconds
"""
if self._start_time is None:
return 0.0
return time.time() - self._start_time
def set_progress(self, total_progress: float, new_maximum: Optional[float] = None):
"""
Make the bar move up to the given value.
Value is expressed in the same unit as given during instancing to the
max value. As such the value given can't be bigger than the max.
Args:
total_progress: same unit as maximum value
new_maximum: optionally change the maximum value of the progress bar
"""
self._had_progress = True
if new_maximum is not None:
self._user_max = new_maximum
self._user_index = min(total_progress, self._user_max)
self.update()
def add_progress(self, progress_amount: float, new_maximum: Optional[float] = None):
"""
Make the bar progress by the given amount.
Amount is expressed in the same unit as given during instancing to the
max value. As such amount given can't be bigger than this value.
Args:
progress_amount: same unit as maximum value
new_maximum: optionally change the maximum value of the progress bar
"""
self._had_progress = True
if new_maximum is not None:
self._user_max = new_maximum
self._user_index = min(self._user_index + progress_amount, self._user_max)
self.update()
def start(self):
"""
Display the progress bar for the first time.
Not mandatory to be called.
"""
self.update()
def end(self):
"""
Stop the progress bar, so it doesn't update anymore.
Recommended to always be called.
"""
self._is_ended = True
# just add a new line
print(file=self._stream)
self.set_cursor_visibility(visible=True)
def set_cursor_visibility(self, visible: bool):
"""
Add or remove a special character that make the stream cursor visible or not.
"""
if visible and not self._is_cursor_visible:
print("\x1b[?25h", end="", file=self._stream)
self._is_cursor_visible = True
elif not visible and self._is_cursor_visible:
print("\x1b[?25l", end="", file=self._stream)
self._is_cursor_visible = False
def _format_text(self, text: str) -> str:
return text.format(
elapsed_time=self._time_elapsed,
bar_min=self._user_min,
bar_max=self._user_max,
bar_index=self._user_index,
)
def update(self):
"""
Visually update the progress bar.
Called automatically but can be manually called to increase refresh rate.
"""
if self._is_ended:
return
if self._start_time is None:
self._start_time = time.time()
if self._hide_cursor:
self.set_cursor_visibility(visible=False)
if self._had_progress:
bar_fill_width: int = self._bar_width
bar_empty_width: int = self._width - bar_fill_width
bar_text: str = (
self.bar_fill * bar_fill_width + self.bar_empty * bar_empty_width
)
bar_text = f"{self.bar_before}{bar_text}{self.bar_after}"
else:
expected_length = len(self.bar_before) + len(self.bar_after) + self._width
bar_text = "." * (self._update_number % 3 + 1)
bar_text = f" {bar_text}".ljust(expected_length)
suffix_text = self._format_text(self.suffix)
prefix_text = self._format_text(self.prefix)
out_text = f"{prefix_text}{bar_text}{suffix_text} "
print("\r" + out_text, end="", file=self._stream)
self._stream.flush()
self._update_number += 1
class DownloadProgressBar(ProgressBar):
"""
A progress bar for downloads, expressed in MB units.
Recommended usage with :any:`pythonning.web.download.download_file`.
Note the ``prefix`` and ``suffix`` argument are already consumed.
"""
byte_to_MB = 9.5367e-7
def __init__(self, *args, **kwargs):
super().__init__(
prefix="downloading",
suffix="[{bar_index:<2.1f}MB/{bar_max:.1f}MB] elapsed {elapsed_time:.2f}s",
*args,
**kwargs,
)
def show_progress(self, block_number, block_size, total_size):
"""
To be used as callback during a download operation.
Args:
block_number: current block being downloaded, variable over time.
block_size: size of each download block, static over time.
total_size:
total size of all the block to download, static over time.
might not be provided which correspond to a value < 1.
"""
if total_size < 1:
self.update()
else:
downloaded = block_number * block_size
self.set_progress(
total_progress=downloaded * self.byte_to_MB,
new_maximum=total_size * self.byte_to_MB,
)
@contextlib.contextmanager
def catch_download_progress(**kwargs):
"""
A context manager to display a Download progress bar and handle its cleaning.
Make sure any stdout call (print, loggers) are done after the context manager has exit.
Example::
print("starting download")
with catch_progress() as progressbar:
download_file(step_callback=progressbar.show_progress)
print("downloading finished")
Args:
kwargs:
kwargs passed to DownloadProgressBar
(``prefix`` and ``suffix`` are already consumed).
"""
progress_bar = DownloadProgressBar(**kwargs)
progress_bar.start()
try:
yield progress_bar
finally:
progress_bar.end()
You can’t perform that action at this time.
