-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.py
116 lines (109 loc) · 3.55 KB
/
tab.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import csv
import itertools
"""Simple function to get a information,
about lenght all value which we are interested
"""
def valLen(val_str):
x = [len(x) for x in val_str]
return x
"""
Simple function to get information,
about dimensions list
"""
def is_list(x):
return type(x) == list
"""
Error info to handle Exception (IndexError)
"""
def inf_err(d, h):
inf = 'Error in data or headers.'
print()
print('_'*len(inf), sep='\n')
print(inf)
print('='*len(inf))
print('Lenght of headers:', len(d), end=" ")
print()
print('Lenght of data:', len(h), end=" ")
print()
print('_'*len(inf), sep='\n')
"""
Main function to get input data like,
@data: list of data (list(), []) one or many dimensions
@headers: list of headers to format table
"""
def helper(a1, sepX=10, sepH=4):
y = len(''.join(a1))
lenA1 = len(a1)
x = sepX * lenA1 + y - sepX + sepH
mainSep = '='*x
return mainSep, sepX, x, lenA1, y, sepH
"""
Function to show data and headers which you will put into two arguments
@data: is holding a nested dict() i.e.
data = { 0: {0: 'value0', 1: 'value1', 2: 'value2', 3: 'value3},
1: {0: 'value0', 1: 'value1', 2: 'value2', 3: 'value3} }
---
@headers: is holding list() i.e.
headers = ["first_head", "second_head"]
"""
def tab(data, headers):
try:
y1 = valLen(headers)
a = helper(headers)
#if any(map(is_list, data)):
# print(data)
print('_'*a[2])
for i, j in enumerate(headers):
print(j, end=" "*a[1])
print()
print(a[0], sep='')
for p_id, p_info in data.items():
print()
for key in p_info:
tmp = y1[key] - len(p_info[key]) + a[1]
print(p_info[key], end=" "*tmp)
b = helper(p_info[key])
print()
print('='*b[2], b[4])
except IndexError:
inf_err(data, headers)
print(data[len(headers)-len(data):])
print(headers[len(data)-len(headers):])
print()
# ----------------------
# Example of using "tab"
# ----------------------
tab({0: {0: '3984', 1: 'Jakub', 2: 'Wo', 3: '30', 4: '0', 5: '374PL374PL374PL374PL374PL374PL374PL374PL374PL374PL'},
1: {0: '4853', 1: 'Krystian', 2: 'Terrenko', 3: '309', 4: '0', 5: '374PL'},
2: {0: '4893', 1: 'Adrian', 2: 'Turbak', 3: '389', 4: '0', 5: '374PL'},
3: {0: '4893', 1: 'Adrian', 2: 'Turbak', 3: '30.01.1989', 4: '0', 5: '374PL'},
4: {0: '4893', 1: 'Adrian', 2: 'Turbak', 3: '309', 4: '0', 5: '374PL'},
5: {0: '4893', 1: 'Adrian', 2: 'Turbak', 3: '30.01.1989', 4: '0', 5: '374PL'}
}, ['ID', 'Name', 'Surname', 'Data urodzenia', 'Encrypted:', 'RIDdsadasdas'])
print()
# -----------------------------------------------------------
# Not finish yet, but is working
# You can open a .csv file and view a content in simple table
# -----------------------------------------------------------
with open('nowy.csv', newline='\n') as file:
reader = csv.reader(file, dialect='excel', delimiter=' ')
headings = next(reader)
for x in headings:
length = sum(len(s) for s in headings)
a = x.split(',')
print(a)
for i in a:
calcS = 8 - len(i)
if calcS == len(i):
print('{0}'.format(i), end='\t')
elif calcS != len(i):
print('{0}'.format(i), end='\t')
print()
print(end='='*5)
for x in reader:
z = x[0].split(',')
for m in range(0, len(z)):
lenDat = len(''.join(z[m]))
if m+1 == len(x):
print()
print(z[m], end='\t')