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
|
class process_bar(object): ''' #号进度条 用法: bar = process_bar() total = 100 for i in range(total + 1): print(bar(i, total), end='') ''' def __init__(self, number=50, decimal=2): ''' INPUT -> "#"号的个数, 保留的小数位 ''' self.decimal = decimal self.number = number self.a = 100/number def __call__(self, now, total): percentage = self.percentage_number(now, total) well_num = int(percentage / self.a) progress_bar_num = self.progress_bar(well_num) result = "\r%s %s" % (progress_bar_num, percentage) return result def percentage_number(self, now, total): ''' 计算百分比 INPUT -> 现在的数, 总数 OUTPUT -> 百分比 ''' return round(now / total * 100, self.decimal) def progress_bar(self, num): ''' 显示进度条位置 INPUT -> 拼接的"#"号的数量 OUTPUT -> 当前的进度条 ''' well_num = "#" * num space_num = " " * (self.number - num) return '[%s%s]' % (well_num, space_num)
|