def print_table(key_label, value_label, control, items): key_lengths = [len(key) for key,val in items] val_lengths = [len(val) for key,val in items] lcol = max([len(key_label)]+key_lengths) + 1 rcol = max([len(value_label)]+ val_lengths) + 1 print "%s %s %s" % ("=" * lcol, "=" * rcol, "=" * rcol) print "%s %s %s" % (key_label.ljust(lcol), value_label.ljust(rcol), "± control".rjust(rcol)) print "%s %s %s" % ("=" * lcol, "=" * rcol, "=" * rcol) for key, value in items: diff = Decimal(control) - Decimal(value) diff = unicode(diff) print "%s %s %s" % (key.ljust(lcol), value.rjust(rcol), diff.rjust(rcol)) print "%s %s %s" % ("=" * lcol, "=" * rcol, "=" * rcol)
def convert_percentage(percentage): """ Converts a percentage with possible decimal places into a 0 padded string of length 4. The final string should represent 1 decimal place. All inputs will be rounded to 1 decimal place. e.g. 10 -> 0100, 0.1 -> 0001, 23.4 -> 0234, 29.949 -> 0299, 29.950 -> 0300 """ value = Decimal(percentage) * 10 value = round(value) value = str(value).replace(".", "") return value.rjust(4, "0")