import numpy as np
import sys, time

# time.clock() is cpu time of current process
# time.time() is wall time

# to see what this does, try
# for x in progprint_xrange(100):
#     time.sleep(0.01)

# TODO there are probably better progress bar libraries I could use

def progprint_xrange(*args,**kwargs):
    xr = xrange(*args)
    return progprint(xr,total=len(xr),**kwargs)

def progprint(iterator,total=None,perline=25,show_times=True):
    times = []
    idx = 0
    if total is not None:
        numdigits = len('%d' % total)
    for thing in iterator:
        prev_time = time.time()
        yield thing
        times.append(time.time() - prev_time)
        sys.stdout.write('.')
        if (idx+1) % perline == 0:
            if show_times:
                avgtime = np.mean(times)
                if total is not None:
                    sys.stdout.write(('  [ %%%dd/%%%dd, %%7.2fsec avg, %%7.2fsec ETA ]\n' % (numdigits,numdigits)) % (idx+1,total,avgtime,avgtime*(total-(idx+1))))
                else:
                    sys.stdout.write('  [ %d done, %7.2fsec avg ]\n' % (idx+1,avgtime))
            else:
                if total is not None:
                    sys.stdout.write(('  [ %%%dd/%%%dd ]\n' % (numdigits,numdigits) ) % (idx+1,total))
                else:
                    sys.stdout.write('  [ %d ]\n' % (idx+1))
        idx += 1
        sys.stdout.flush()
    print ''
    if show_times:
        print '%7.2fsec avg, %7.2fsec total\n' % (np.mean(times),np.sum(times))

@@ -110,160 +110,503 @@
 %0A%0A# 
-to see what this does, try%0A# for x in progprint_xrange(100
+TODO there are probably better progress bar libraries I could use%0A%0Around = (lambda x: lambda y: int(x(y)))(round)%0A%0A# NOTE: datetime.timedelta.__str__ doesn't allow formatting the number of digits%0Adef sec2str(seconds
 ):%0A
-#
     
- time.sleep(0.01)%0A%0A# TODO there are probably better progress bar libraries I could use
+hours, rem = divmod(seconds,3600)%0A    minutes, seconds = divmod(rem,60)%0A    if hours %3E 0:%0A        return '%2502d:%2502d:%2502d' %25 (hours,minutes,round(seconds))%0A    elif minutes %3E 0:%0A        return '%2502d:%2502d' %25 (minutes,round(seconds))%0A    else:%0A        return '%250.2f' %25 seconds
 %0A%0Ade
@@ -1187,77 +1187,187 @@
     
-sys.stdout.write(('  %5B %25%25%25dd/%25%25%25dd, %25%257.2fsec avg, %25%257.2fsec ETA %5D%5Cn'
+eta = sec2str(avgtime*(total-(idx+1)))%0A                    sys.stdout.write((%0A                        '  %5B %25%25%25dd/%25%25%25dd, %25%257.2fsec avg, ETA %25%25s %5D%5Cn'%0A                               
  %25 (
@@ -1411,39 +1411,19 @@
 avgtime,
-avgtime*(total-(idx+1))
+eta
 ))%0A     
@@ -1835,33 +1835,99 @@
    if show_times
-:
+ and len(times) %3E 0:%0A        total = sec2str(seconds=np.sum(times))
 %0A        print '
@@ -1941,23 +1941,17 @@
 c avg, %25
-7.2fsec
+s
  total%5Cn
@@ -1970,23 +1970,16 @@
 (times),
-np.sum(times)
+total
 )%0A
+%0A

update util/text.py