-
Notifications
You must be signed in to change notification settings - Fork 12
/
progressBar.py
132 lines (102 loc) · 5.11 KB
/
progressBar.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
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
# -*- coding: utf-8 -*-
import sys
import os
def printProgressBar(iteration, total, prefix='', suffix='', decimals=1, length=100,
fill='=', empty=' ', tip='>', begin='[', end=']', done="[DONE]", clear=True):
"""
Print iterations progress.
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 [int]
length - Optional : character length of bar [int]
fill - Optional : bar fill character [str] (ex: 'â– ', 'â–ˆ', '#', '=')
empty - Optional : not filled bar character [str] (ex: '-', ' ', '•')
tip - Optional : character at the end of the fill bar [str] (ex: '>', '')
begin - Optional : starting bar character [str] (ex: '|', 'â–•', '[')
end - Optional : ending bar character [str] (ex: '|', 'â–', ']')
done - Optional : display message when 100% is reached [str] (ex: "[DONE]")
clear - Optional : display completion message or leave as is [str]
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength
if iteration != total:
bar = bar + tip
bar = bar + empty * (length - filledLength - len(tip))
display = '\r{prefix}{begin}{bar}{end} {percent}%{suffix}' \
.format(prefix=prefix, begin=begin, bar=bar, end=end, percent=percent, suffix=suffix)
print(display, end=''), # comma after print() required for python 2
if iteration == total: # print with newline on complete
if clear: # display given complete message with spaces to 'erase' previous progress bar
finish = '\r{prefix}{done}'.format(prefix=prefix, done=done)
if hasattr(str, 'decode'): # handle python 2 non-unicode strings for proper length measure
finish = finish.decode('utf-8')
display = display.decode('utf-8')
clear = ' ' * max(len(display) - len(finish), 0)
print(finish + clear)
else:
print('')
def verbose(verboseLevel, requiredLevel, printFunc=print, *printArgs, **kwPrintArgs):
"""
Calls `printFunc` passing it `printArgs` and `kwPrintArgs`
only if `verboseLevel` meets the `requiredLevel` of verbosity.
Following forms are supported:
> verbose(1, 0, "message")
>> message
> verbose(1, 0, "message1", "message2")
>> message1 message2
> verbose(1, 2, "message")
>> <nothing since verbosity level not high enough>
> verbose(1, 1, lambda x: print('MSG: ' + x), 'message')
>> MSG: message
> def myprint(x, y="msg_y", z=True): print('MSG_Y: ' + y) if z else print('MSG_X: ' + x)
> verbose(1, 1, myprint, "msg_x", "msg_y")
>> MSG_Y: msg_y
> verbose(1, 1, myprint, "msg_x", "msg_Y!", z=True)
>> MSG_Y: msg_Y!
> verbose(1, 1, myprint, "msg_x", z=False)
>> MSG_X: msg_x
> verbose(1, 1, myprint, "msg_x", z=True)
>> MSG_Y: msg_y
"""
if verboseLevel >= requiredLevel:
# handle cases when no additional arguments are provided (default print nothing)
printArgs = printArgs if printArgs is not None else tuple([''])
# handle cases when verbose is called directly with the object (ex: str) to print
if not hasattr(printFunc, '__call__'):
printArgs = tuple([printFunc]) + printArgs
printFunc = print
printFunc(*printArgs, **kwPrintArgs)
def print_flush(txt=''):
print(txt)
sys.stdout.flush()
if os.name == 'nt':
import msvcrt
import ctypes
class _CursorInfo(ctypes.Structure):
_fields_ = [("size", ctypes.c_int),
("visible", ctypes.c_byte)]
def hide_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = False
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25l")
sys.stdout.flush()
def show_cursor():
if os.name == 'nt':
ci = _CursorInfo()
handle = ctypes.windll.kernel32.GetStdHandle(-11)
ctypes.windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(ci))
ci.visible = True
ctypes.windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(ci))
elif os.name == 'posix':
sys.stdout.write("\033[?25h")
sys.stdout.flush()