def table_convert(original, result, table_type): if type(result) == str: # an error message, not a number return result if table_type == 'bits': return '{0:.4f}'.format(result / original * 8) elif table_type == 'per': return '{0:.1f}%'.format(result / original * 100) elif table_type == 'size': return general.human_readable_size(result) else: assert(False)
i = 0 for compressor_name in compressors: results[compressor_name] = {} for fname in files: results[compressor_name][fname] = result_list[i] i += 1 fieldnames = ['File', 'Size'] + compressors if csv_fname: with open(csv_fname, 'w') as out: writer = csv.DictWriter(out, fieldnames=fieldnames) writer.writeheader() for fname in files: row = {'File': fname, 'Size': results['Size'][fname]} for compressor in compressors: row[compressor] = results[compressor][fname] writer.writerow(row) if table_type: tableResults = {} tableResults['File'] = files tableResults['Size'] = [general.human_readable_size(results['Size'][fname]) for fname in files] for compressor in compressors: tableResults[compressor] = [] for fname in files: res = table_convert(results['Size'][fname], results[compressor][fname], table_type) tableResults[compressor].append(res) asciitable.write(tableResults, sys.stdout, names=fieldnames, Writer=asciitable.FixedWidth)