# coding:utf-8 class ProcessBar(object): def __init__ ( self, number=50, decimal=2): """ :param number: # 号的个数 :param decimal: 你保留的保留小数位 """ self.decimal = decimal self.number = number def __call__ ( self, now, total ): # 1. 获取当前的百分比数 percentage = self.percentage_number(now, total) # print percentage, now, total # 2. 根据 现在百分比计算 well_num = int(percentage * self.number) # 3. 打印字符进度条 progress_bar_num = self.progress_bar(well_num) # 4. 完成的进度条 result = '{0} {1:>5.0%}'.format(progress_bar_num, percentage) print result def percentage_number ( self, now, total ): """ 计算百分比 :param now: 现在的数 :param total: 总数 :return: 根据指定的精度返回进度 """ return round(now / total , 2) def progress_bar ( self, num ): """ 显示进度条位置 :param num: 拼接的 “#” 号的 :return: 返回的结果当前的进度条 """ # 1. "#" 号个数 well_num = "#" * num # 2. 空格的个数 space_num = " " * (self.number - num) return '[%s%s]' % (well_num, space_num) def Chunk( self, total): """ 返回每个#代表的所代表的大小 :param total: 全部大小 :return: 每个#的进度量 """ chunk = round(total /self.number, self.decimal) return chunk if __name__ == '__main__': # 设置进度条的#个数为100个,每个#代表实际数据精度 bar = ProcessBar(100,2) # print bar.number,bar.decimal # 进度条代表的数值 total = 10000.0 # 每个#代表实际数据 chunkbar = bar.Chunk(total) # 已完成部分loaded,累积的增量数据