-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtqdm-batch.py
More file actions
39 lines (33 loc) · 1 KB
/
tqdm-batch.py
File metadata and controls
39 lines (33 loc) · 1 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
#!/bin/env python3
"""
A wrapper around the tqdm CLI, more suitable for
logging to a file in a long-running batch program.
Monkey-batches tqdm to print each update on a new
line instead of clearing the line.
"""
import tqdm
import tqdm._main
#
# https://github.com/tqdm/tqdm/issues/750#issuecomment-537558122
#
@staticmethod
def status_printer(file):
"""
Manage the printing and in-place updating of a line of characters.
Note that if the string is longer than a line, then in-place
updating may not work (it will print a new line at each refresh).
"""
fp = file
fp_flush = getattr(fp, 'flush', lambda: None) # pragma: no cover
def fp_write(s):
fp.write(str(s))
fp_flush()
last_len = [0]
def print_status(s):
len_s = len(s)
fp_write('\n' + s + (' ' * max(last_len[0] - len_s, 0)))
last_len[0] = len_s
return print_status
# Monkey-patch status_printer to use \n instead of \r
tqdm.tqdm.status_printer = status_printer
tqdm._main.main()