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
| class ProgressBar:
"""Gère une barre de progression"""
def __init__ (self,printer,msg=''):
"""printer : prend en paramètre un message et un flottant entre 0 et 1"""
self.reset( printer,msg )
def __default_printer (self , str , progress ):
print '%s %.3g%%' %( str,progress*100 )
def reset(self,printer=None,msg=None):
self.ratio = [1.0]
self.loop_count = 100
self.loop_index = 0
self.loop_inc = 1.0/self.loop_count
self.progress = 0.0
self.printer = self.__default_printer
if msg: self.msg = msg
if printer: self.printer = printer
return self
def set_message (self,msg):
self.msg = msg
def enter (self,ratio):
self.ratio.append( ratio )
return self
def exit (self):
self.ratio.pop()
return self
def loops (self,nb):
self.loop_count = nb
self.loop_index = 0
if nb > 0 : self.loop_inc = 1.0/self.loop_count
else : self.loop_inc = 1
return self
def loop (self):
self.loop_index += 1
prod = reduce( lambda a,b : a*b , self.ratio )
self.progress += prod*self.loop_inc
self.printer( self.msg,self.progress )
return self |
Partager