forked from sgkruk/Apress-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableutils.py
73 lines (71 loc) · 1.99 KB
/
tableutils.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
62
63
64
65
66
67
68
69
70
71
72
73
import copy
def flatten(l):
return [e for s in l for e in s]
def set2string(S):
s='{ '
if S is not None:
for i in range(len(S)):
s=s+str(S[i])
if i < len(S)-1:
s=s+'; '
else:
s=s+' '
s=s+'}'
return s
def wrapmat(M,left,header):
T=copy.deepcopy(M)
m,n=len(T),len(T[0])
for i in range(len(left)):
T[i].insert(0,left[i]);
if header != None:
if len(header) < len(T[0]):
T.insert(0,['']+header)
else:
T.insert(0,header)
return T
def formatmat(M,zeroes=False,decimals=4):
T=copy.deepcopy(M)
for i in range(len(M)):
for j in range(len(M[i])):
el = T[i][j]
if type(el)==int:
if el or zeroes:
el = '{0:4d}'.format(el)
else:
el = ''
T[i][j] = el
elif type(el)==float:
if el or zeroes :
if decimals==4:
el = '{0:.4f}'.format(el)
elif decimals==3:
el = '{0:.3f}'.format(el)
elif decimals==2:
el = '${0:.2f}'.format(el)
elif decimals==1:
el = '{0:.1f}'.format(el)
elif decimals==0:
el = '{0:.0f}'.format(el)
else:
el = ''
T[i][j] = el
elif el is None:
el = ''
T[i][j] = el
return T
def printmat(M,zeroes=False,decimals=4):
T = formatmat(M,zeroes,decimals)
output = []
for row in T:
l=''
for i in range(len(row)):
l = l+str(row[i])
if i < len(row)-1:
l=l+','
print(l)
def splitwrapmat(M,left,header):
T=wrapmat(M,left,None)
if len(T) % 2:
T.append([])
T2 = [T[i]+T[i+1] for i in range(0,len(T),2)]
return wrapmat(T2,[],header+header)