-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.py
155 lines (122 loc) · 5.08 KB
/
Table.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
""" Python
----------------------------------------------------------------------
This Table Class create by M Fadhillah Nursyawal - February 2023
gitHub : https://github.com/Fadilahn
----------------------------------------------------------------------
-- Modul
============================================================
Use:
-----------
# kamu bisa set header dengan method setHeader(), datanya dapat berupa
- list, ex: ['Name', 'Age'] atau
- dictionary, ex: {'Name':'Fadhillah', 'Age':19}
# kamu bisa menambahkan data dengan method addData(), datanya dapat berupa
- list, ex: ['Fadhil', 19]
- dictionary, ex: {'Name':'Fadhillah', 'Age':19}
- list of list, ex: [['Fadhil', 19], ['V', 19]]
- list of dictionary, ex: [{'Name':'Fadhillah', 'Age':19}, {'Name':'V', 'Age':19}]
note : saat menambahkan data berupa dictionary, header dapat otomatis ter setting dengan key dictionary. jadi tidak perlu setHeader() lagi
# menampilkan data dengan method display()
# untuk pemanggilan methodnya, ex:
list_of_dict = [{'Name':'Fadhillah', 'Age':19}, {'Name':'V', 'Age':19}]
table = Table()
table.addData(list_of_dict)
table.display()
or
list_of_list = [['Fadhil', 19], ['V', 19]]
table = Table()
table.setHeader(['Name', 'Age'])
table.addData(list_of_dict)
table.display()
============================================================
Revisi ke-2
---------------------------
version : Table-2
Revision : -
Update : 3 March 2023
- menambahkan constrain pada method addData(), addRow(), dan addHeader();
- menambahkan method __calculateColumnWidths().
---------------------------
"""
class Table:
__header = [] # private class variable to store table header
__data_rows = [] # private class variable to store table data
__column_widths = [] # private class variable to store width of each column
# Constructor to initialize the instance variables
def __init__(self):
self.__header = []
self.__data_rows = []
self.__column_widths = []
# getter for all atribute
def getHeader(self):
return self.__header
def getDataRow(self):
return self.__data_rows
def getColumnWidths(self):
return self.__column_widths
# Method to set table header
def setHeader(self, header):
# check if header is list or dictionary
if isinstance(header, list):
self.__header = header
elif isinstance(header, dict):
self.__header = list(header.keys())
# Method to add a row to the table
def addRow(self, row):
# If row is a dictionary, add values to the table data rows
if isinstance(row, dict):
if not self.__header:
# If header is not set, set header as the keys of the dictionary
self.__header = list(row.keys())
self.__column_widths = [len(column) for column in self.__header]
self.__data_rows.append(list(row.values()))
self.__calculateColumnWidths(list(row.values()))
# If row is a list, add the list as it is to the table data rows
elif isinstance(row, list):
if not self.__header:
# If header is not set, set header as '-'
self.__header = ['-' for i in row]
self.__column_widths = [0 for i in row]
self.__data_rows.append(row)
self.__calculateColumnWidths(row)
else:
print("Error!")
# Private method to calculate width of each column based on its data
def __calculateColumnWidths(self, data):
for i, cell in enumerate(data):
width = len(str(cell))
if width > self.__column_widths[i]:
self.__column_widths[i] = width
# Method to add multiple rows to the table
def addData(self, data):
# If data is a list containing multiple rows, add each row
if isinstance(data, list):
for row in data:
self.addRow(row)
# If data is a dictionary, add the dictionary as a row
elif isinstance(data, dict):
self.addRow(data)
else:
print("Error!")
# Method to display the table
def display(self):
print()
# create border horizontal
border = '+'
for i, width in enumerate(self.__column_widths):
border += '-' * (width + 2) + '+'
# Print table header
print(border)
print('|', end='')
for i, column in enumerate(self.__header):
print(' {:^{width}} |'.format(column, width=self.__column_widths[i]), end='')
print()
print(border)
# Print data table rows
for row in self.__data_rows:
print('|', end='')
for i, cell in enumerate(row):
print(' {:^{width}} |'.format(cell, width=self.__column_widths[i]), end='')
print()
print(border)
print()