+ return int(new_min)
+ return int(rate)
+
+# Get optimum 1k exponent to represent a number of bytes
+def optimum_k_exp(num_bytes):
+ global const_1k
+ if num_bytes == 0:
+ return 0
+ return long(math.log(num_bytes, const_1k))
+
+# Get optimum representation of number of bytes
+def format_bytes(num_bytes):
+ global const_1k
+ try:
+ exp = optimum_k_exp(num_bytes)
+ suffix = 'bkMGTPEZY'[exp]
+ if exp == 0:
+ return '%s%s' % (num_bytes, suffix)
+ converted = float(num_bytes) / float(const_1k**exp)
+ return '%.2f%s' % (converted, suffix)
+ except IndexError:
+ sys.exit('Error: internal error formatting number of bytes.')
+
+# Calculate ETA and return it in string format as MM:SS
+def calc_eta(start, now, total, current):
+ dif = now - start
+ if current == 0 or dif < const_epsilon:
+ return '--:--'
+ rate = float(current) / dif
+ eta = long((total - current) / rate)
+ eta_mins = eta / 60
+ eta_secs = eta % 60
+ if eta_mins > 99:
+ return '--:--'
+ return '%02d:%02d' % (eta_mins, eta_secs)
+
+# Calculate speed and return it in string format
+def calc_speed(start, now, bytes):
+ dif = now - start
+ if bytes == 0 or dif < const_epsilon:
+ return 'N/A b'
+ return format_bytes(float(bytes) / dif)