Lines 50
##### Keywords
iteration (1) percent (1) print (6)
##### Permissions
Viewable by Everyone
Editable by All Siafoo Users
Stay up to dateembedded code automagically updates, each snippet and article has a feed

# Print Percent-Done Statements at Regular Intervals 0

 In Brief A better way to report how far done your iterative process is. You can report percent-done statements at regular frequencies (in time), intervals (in percent), or just at the start and end of iterations.... more
 Language Python
# 's
` 1# Copyright 2007 Regents of the University of California 2# Written by David Isaacson at the University of California 3# BSD License 4 5import time 6 7class PercentPrint: 8  """ 9  A simple helper to print out percentage-done statements every so often.10  Call on every iteration, will only print when necessary.11  """12  def __init__(self, total, interval=10, frequency=0, indent=""):13    """14    The only required argument is 'total', the number of iterations15    you will be doing. You may also specify an interval (in percent)16    and/or a frequency (in time) that you would like to see print-outs.17    Setting either to 0 will disable that mode.  Setting both to 0 will18    print only 0% and 100% statements. 'indent' will be prepended to19    every print statement."20    """21    self.total = total22    self.interval = interval23    self.frequency = frequency24    self.indent = indent25    self.Reset()26    27  def __call__(self,value):28    """29    Checks the value of the current iteration to see if print-outs are30    needed.  Prints if necessary.31    """32    current = value / self.total * 10033    if self.frequency: clock = time.clock()3435    if numpy.round(2*current - self.last,12) >= 100.0: print self.indent + "100% done."36    elif (self.interval and current//self.interval > self.previous//self.interval) \37      or (self.frequency and (clock - self.prevtime) > self.frequency) \38      or current == 0:39      40      print self.indent + "%d%% done." %(numpy.round(current))41      42      if self.interval: self.previous = current43      if self.frequency: self.prevtime = clock44      45    self.last = current46    47  def Reset(self):48    """49    Resets the class.  Call this to start over.50    """51    self.previous = 0.052    self.prevtime = 0.053    self.last = 0.0`

A better way to report how far done your iterative process is. You can report percent-done statements at regular frequencies (in time), intervals (in percent), or just at the start and end of iterations.

Initialize this class with the total number of iterations, and call your object with the current iteration count whenever you think you might want to print something out (usually every iteration). For more information, check out the docstrings.