PrettyTableX 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
PrettyTable/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .core import PrettyTable
|
PrettyTable/core.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
class PrettyTable:
|
|
2
|
+
|
|
3
|
+
class TooLongError(Exception):
|
|
4
|
+
def __init__(self, msg="A row was to long."):
|
|
5
|
+
super().__init__(msg)
|
|
6
|
+
|
|
7
|
+
class TooShortError(Exception):
|
|
8
|
+
def __init__(self, msg="A row was to short."):
|
|
9
|
+
super().__init__(msg)
|
|
10
|
+
|
|
11
|
+
class NotEnoughIndexes(Exception):
|
|
12
|
+
def __init__(self, msg="There aren't enough indexes for each row."):
|
|
13
|
+
super().__init__(msg)
|
|
14
|
+
|
|
15
|
+
class NotAnArrayError(Exception):
|
|
16
|
+
def __init__(self, msg="This isn't a array."):
|
|
17
|
+
super().__init__(msg)
|
|
18
|
+
|
|
19
|
+
def __init__(self, ar, header="", row_index:bool = True, row_indexes:list = None, column_indexes:list=None):
|
|
20
|
+
self.ar = ar
|
|
21
|
+
self.header = header
|
|
22
|
+
self.pretty_table = False
|
|
23
|
+
self.row_index = row_index
|
|
24
|
+
self.column_indexes = column_indexes
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if column_indexes is None:
|
|
28
|
+
self.column_index = False
|
|
29
|
+
else:
|
|
30
|
+
self.column_index = True
|
|
31
|
+
|
|
32
|
+
if row_indexes is None:
|
|
33
|
+
self.row_indexes = [i + 1 for i in range(len(ar))]
|
|
34
|
+
else:
|
|
35
|
+
self.row_indexes = row_indexes
|
|
36
|
+
|
|
37
|
+
def __str__(self):
|
|
38
|
+
if self.pretty_table:
|
|
39
|
+
return self.pretty_table
|
|
40
|
+
else:
|
|
41
|
+
a = ""
|
|
42
|
+
for i in self.ar:
|
|
43
|
+
a += str(i) + "\n"
|
|
44
|
+
|
|
45
|
+
return a
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def check(self):
|
|
49
|
+
num1 = len(self.ar[0])
|
|
50
|
+
for i in self.ar:
|
|
51
|
+
try:
|
|
52
|
+
num1 != len(i)
|
|
53
|
+
except TypeError:
|
|
54
|
+
raise self.NotAnArrayError(f"You have to turn the {type(self.ar)} into a array using our predefined functions.")
|
|
55
|
+
if num1 != len(i):
|
|
56
|
+
if len(i) > num1:
|
|
57
|
+
raise self.TooLongError(f"Row {self.ar.index(i) + 1} is to long.")
|
|
58
|
+
elif len(i) < num1:
|
|
59
|
+
raise self.TooShortError(f"Row {self.ar.index(i) + 1} is to short.")
|
|
60
|
+
else:
|
|
61
|
+
raise Exception("Something happened...")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def make_table_pretty(self):
|
|
65
|
+
"""This function turns your array into a better looking table.
|
|
66
|
+
THE ARRAY MUST BE A LIST. IF IT ISN'T, USE dict_to_array or tup_to_array to fix it."""
|
|
67
|
+
num1 = len(self.ar[0])
|
|
68
|
+
num2 = 0
|
|
69
|
+
res = []
|
|
70
|
+
|
|
71
|
+
self.check()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
for i in self.ar:
|
|
76
|
+
for n in i:
|
|
77
|
+
if len(str(n) + "\t" + str(num1)) > num2:
|
|
78
|
+
num2 = len(str(n) + "\t" + str(num1))
|
|
79
|
+
|
|
80
|
+
if self.column_index:
|
|
81
|
+
for n in self.column_indexes:
|
|
82
|
+
if len(str(n) + "\t" + str(num1)) > num2:
|
|
83
|
+
num2 = len(str(n) + "\t" + str(num1))
|
|
84
|
+
|
|
85
|
+
if self.row_index:
|
|
86
|
+
lila = -1
|
|
87
|
+
for i in self.row_indexes:
|
|
88
|
+
if len(str(i)) > lila:
|
|
89
|
+
lila = len(str(i)) + 2
|
|
90
|
+
res.append("+")
|
|
91
|
+
res.append("-" * (lila))
|
|
92
|
+
for i in range(num1):
|
|
93
|
+
res.append("+")
|
|
94
|
+
res.append("-" * num2)
|
|
95
|
+
res.append("+")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
res = "".join(res)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
if self.header:
|
|
104
|
+
self.pretty_table = (
|
|
105
|
+
self.header.center(len(res)).replace(
|
|
106
|
+
self.header,
|
|
107
|
+
f"\033[1;4m{self.header}\033[0m"
|
|
108
|
+
) + "\n"
|
|
109
|
+
)
|
|
110
|
+
else:
|
|
111
|
+
self.pretty_table = ""
|
|
112
|
+
|
|
113
|
+
if self.column_index:
|
|
114
|
+
a = self.column_indexes[:]
|
|
115
|
+
|
|
116
|
+
self.pretty_table += res + "\n"
|
|
117
|
+
|
|
118
|
+
if self.row_index:
|
|
119
|
+
self.pretty_table += "|" + " "*lila
|
|
120
|
+
for i in range(num1):
|
|
121
|
+
try:
|
|
122
|
+
self.pretty_table += "|" + str(self.column_indexes[0].center(num2))
|
|
123
|
+
del self.column_indexes[0]
|
|
124
|
+
except IndexError:
|
|
125
|
+
raise self.NotEnoughIndexes("There aren't as many indexes as columns.")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
self.column_indexes = a[:]
|
|
129
|
+
del a
|
|
130
|
+
|
|
131
|
+
self.pretty_table += "|\n"
|
|
132
|
+
|
|
133
|
+
for i in self.ar:
|
|
134
|
+
self.pretty_table += str(res) + "\n"
|
|
135
|
+
if self.row_index:
|
|
136
|
+
a = self.row_indexes[:]
|
|
137
|
+
self.pretty_table += "|"
|
|
138
|
+
try:
|
|
139
|
+
self.pretty_table += str(self.row_indexes[0]).center(lila)
|
|
140
|
+
except IndexError:
|
|
141
|
+
raise self.NotEnoughIndexes("There aren't as many indexes as rows.")
|
|
142
|
+
del self.row_indexes[0]
|
|
143
|
+
for r in i:
|
|
144
|
+
self.pretty_table += "|" + str(r).center(num2)
|
|
145
|
+
self.pretty_table += "|\n"
|
|
146
|
+
|
|
147
|
+
if self.row_index:
|
|
148
|
+
self.row_indexes = a[:]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
self.pretty_table += res
|
|
152
|
+
|
|
153
|
+
def dict_to_array(self):
|
|
154
|
+
"""If your array is a dictionary. This will turn it into the array you need."""
|
|
155
|
+
|
|
156
|
+
self.ar = list(self.ar.values())
|
|
157
|
+
|
|
158
|
+
def tup_to_array(self):
|
|
159
|
+
"""If your array is a tuple. This will turn it into the array you need."""
|
|
160
|
+
|
|
161
|
+
self.ar = [list(i) for i in self.ar]
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
PrettyTable/__init__.py,sha256=EEhpxJfJPWffocXOwbkpa6_ACAp_pAY3hZ9oGc0bMyM,29
|
|
2
|
+
PrettyTable/core.py,sha256=enS5G_4NwuYFK9Aj5P3MqGCwEnpcEHJhl43AShSO7Nk,5068
|
|
3
|
+
prettytablex-0.1.0.dist-info/METADATA,sha256=m1SaEts7hdps_PBXIDudJeNZZhAoQjv5qP_IsEjkYQA,163
|
|
4
|
+
prettytablex-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
prettytablex-0.1.0.dist-info/top_level.txt,sha256=RINsTc1XQD-eISDS_lPXo0nYIhfguGHRTj_UVxUUqDI,12
|
|
6
|
+
prettytablex-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
PrettyTable
|