forked from pdgillis/LCSExperiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_formater.py
61 lines (55 loc) · 1.86 KB
/
output_formater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
##
## Formats certain output types, namely tables and long strings.
##
def print_table( rows, column_names=[], row_names=[] ):
""" Prints a table with column and row names.
"""
num_rows=len(rows)
if num_rows<0: return
from collections import namedtuple
def pprinttable(rows):
headers = rows[0]._fields
lens = []
ls = lambda x:len(repr(x))
for i in range(len(rows[0])):
lens.append(
ls(max([x[i] for x in rows] + [headers[i]], key=ls)) )
formats = []
hformats = []
for i in range(len(rows[0])):
if isinstance(rows[0][i], int):
formats.append("%%%dd" % lens[i])
else:
formats.append("%%-%ds" % lens[i])
hformats.append("%%-%ds" % lens[i])
pattern = " | ".join(formats)
hpattern = " | ".join(hformats)
separator = "-+-".join(['-' * n for n in lens])
print hpattern % tuple(headers)
print separator
for line in rows:
print pattern % tuple(line)
# Append each element from row_names to the front of each row in rows.
if len(row_names) == num_rows:
newrows = [ [h]+row for (h,row) in zip( row_names, rows ) ]
if len(column_names)+1 == len(rows[0]):
column_names=['_']+column_names
if len(column_names) < len(rows[0]):
n = len(rows[0])-len(column_names)
column_names+=""*n
# Make each row a named tuple.
r = namedtuple('r',column_names)
newrows = rows
pprinttable([ r(*row) for row in newrows ])
def print_longstring( row_size, num_row, s ):
fin,i,out,l = False,0,"",len(s)
for _ in range(num_row):
n = min(i+row_size, l)
out+=s[i:n]+"\n"
i+=n
if n==l:
fin=True
break
if not fin and n>3:
out=out[:-4]+"...\n"
print out,