-
Notifications
You must be signed in to change notification settings - Fork 0
/
reports.py
51 lines (41 loc) · 1.62 KB
/
reports.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
import sys
class cmpr:
def __lt__(self,o): return type(o.v) == type(self.v) and self.v < o.v
def __init__(self,v): self.v = v
def multisort(tbl,l): return tbl if l==[] else \
sorted(multisort(tbl,l[1:]),key=lambda x:cmpr(x[abs(l[0])-1]),reverse=l[0]<0)
# Pardon wrong / unsuitable format strings
def fmt(fmts,d):
try: return fmts % d
except Exception: return d
def printTbl(tbl,
title=None,
proj=None, # 1 based columns to project in tbl
sort=[], # 1 based column no w/ -sign to indicate reverse in tbl
colnames=None,
formaters={}, # 1 based keys : formatstr in tbl
file=sys.stdout
):
if title:
print(title,file=file)
print('='*len(title),'\n',file=file)
stbl = multisort(tbl,sort)
effproj = [c-1 for c in proj] if proj else range(len(tbl[0]))
projl = [ [ fmt(formaters.get(i+1,'%s'),r[i]) for i in effproj] for r in stbl ]
projc = [colnames]+projl if colnames else projl
widths = [ max([len(r[c]) for r in projc]) for c in range(len(projc[0])) ]
ul = ['-'*widths[i] for i,t in enumerate(colnames)] if colnames else None
projcul = [projc[0]] + [ul] + projc[1:] + [ul] if colnames else projc
print('\n'.join(
'\t'.join( c.rjust(widths[i]) for i,c in enumerate(r) ) for r in projcul
),'\n',file=file)
# Test driver
if __name__=='__main__':
tbl = [
(1,2,3,4,5),
(5,4,3,2,1),
(6,7,8,9,10),
(10,9,8,7,6),
]
printTbl(tbl,'test',proj=[0,4],sort=[2,-3],colnames=['abc','bcde'],formaters={0:'%05d'})
printTbl(tbl,'test',proj=[0,4],sort=[2,-3],formaters={0:'%05d'})