prettytable 0.3__tar.gz → 0.4__tar.gz
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-0.3 → prettytable-0.4}/PKG-INFO +2 -2
- {prettytable-0.3 → prettytable-0.4}/prettytable.egg-info/PKG-INFO +2 -2
- prettytable-0.4/prettytable.py +502 -0
- {prettytable-0.3 → prettytable-0.4}/setup.py +2 -1
- prettytable-0.3/prettytable.py +0 -263
- {prettytable-0.3 → prettytable-0.4}/prettytable.egg-info/SOURCES.txt +0 -0
- {prettytable-0.3 → prettytable-0.4}/prettytable.egg-info/dependency_links.txt +0 -0
- {prettytable-0.3 → prettytable-0.4}/prettytable.egg-info/top_level.txt +0 -0
- {prettytable-0.3 → prettytable-0.4}/setup.cfg +0 -0
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 1.0
|
|
2
2
|
Name: prettytable
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4
|
|
4
4
|
Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
|
|
5
5
|
Home-page: http://www.luke.maurits.id.au/software/prettytable
|
|
6
6
|
Author: Luke Maurits
|
|
7
7
|
Author-email: luke@maurits.id.au
|
|
8
|
-
License:
|
|
8
|
+
License: http://www.luke.maurits.id.au/software/bsdlicense.txt
|
|
9
9
|
Description: UNKNOWN
|
|
10
10
|
Platform: UNKNOWN
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 1.0
|
|
2
2
|
Name: prettytable
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4
|
|
4
4
|
Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
|
|
5
5
|
Home-page: http://www.luke.maurits.id.au/software/prettytable
|
|
6
6
|
Author: Luke Maurits
|
|
7
7
|
Author-email: luke@maurits.id.au
|
|
8
|
-
License:
|
|
8
|
+
License: http://www.luke.maurits.id.au/software/bsdlicense.txt
|
|
9
9
|
Description: UNKNOWN
|
|
10
10
|
Platform: UNKNOWN
|
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
# PrettyTable 0.4
|
|
2
|
+
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
3
|
+
# All rights reserved.
|
|
4
|
+
# With contributions from:
|
|
5
|
+
# * Chris Clark
|
|
6
|
+
#
|
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
|
9
|
+
#
|
|
10
|
+
# * Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
# this list of conditions and the following disclaimer.
|
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
# and/or other materials provided with the distribution.
|
|
15
|
+
# * The name of the author may not be used to endorse or promote products
|
|
16
|
+
# derived from this software without specific prior written permission.
|
|
17
|
+
#
|
|
18
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
22
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
|
|
30
|
+
import cgi
|
|
31
|
+
import copy
|
|
32
|
+
import cPickle
|
|
33
|
+
import sys
|
|
34
|
+
|
|
35
|
+
FRAME = 0
|
|
36
|
+
ALL = 1
|
|
37
|
+
NONE = 2
|
|
38
|
+
|
|
39
|
+
class PrettyTable:
|
|
40
|
+
|
|
41
|
+
def __init__(self, fields=None, caching=True, padding_width=1, left_padding=None, right_padding=None):
|
|
42
|
+
|
|
43
|
+
"""Return a new PrettyTable instance
|
|
44
|
+
|
|
45
|
+
Arguments:
|
|
46
|
+
|
|
47
|
+
fields - list or tuple of field names
|
|
48
|
+
caching - boolean value to turn string caching on/off
|
|
49
|
+
padding width - number of spaces between column lines and content"""
|
|
50
|
+
|
|
51
|
+
# Data
|
|
52
|
+
if fields:
|
|
53
|
+
self.set_field_names(fields)
|
|
54
|
+
else:
|
|
55
|
+
self.fields = []
|
|
56
|
+
self.widths = []
|
|
57
|
+
self.aligns = []
|
|
58
|
+
self.set_padding_width(padding_width)
|
|
59
|
+
self.rows = []
|
|
60
|
+
self.cache = {}
|
|
61
|
+
self.html_cache = {}
|
|
62
|
+
|
|
63
|
+
# Options
|
|
64
|
+
self.hrules = FRAME
|
|
65
|
+
self.caching = caching
|
|
66
|
+
self.padding_width = padding_width
|
|
67
|
+
self.left_padding = left_padding
|
|
68
|
+
self.right_padding = right_padding
|
|
69
|
+
self.vertical_char = "|"
|
|
70
|
+
self.horizontal_char = "-"
|
|
71
|
+
self.junction_char = "+"
|
|
72
|
+
|
|
73
|
+
def __getslice__(self, i, j):
|
|
74
|
+
|
|
75
|
+
"""Return a new PrettyTable whose data rows are a slice of this one's
|
|
76
|
+
|
|
77
|
+
Arguments:
|
|
78
|
+
|
|
79
|
+
i - beginning slice index
|
|
80
|
+
j - ending slice index"""
|
|
81
|
+
|
|
82
|
+
newtable = copy.deepcopy(self)
|
|
83
|
+
newtable.rows = self.rows[i:j]
|
|
84
|
+
return newtable
|
|
85
|
+
|
|
86
|
+
def __str__(self):
|
|
87
|
+
|
|
88
|
+
return self.get_string()
|
|
89
|
+
|
|
90
|
+
def set_field_names(self, fields):
|
|
91
|
+
|
|
92
|
+
"""Set the names of the fields
|
|
93
|
+
|
|
94
|
+
Arguments:
|
|
95
|
+
|
|
96
|
+
fields - list or tuple of field names"""
|
|
97
|
+
|
|
98
|
+
self.fields = fields
|
|
99
|
+
self.widths = [len(field) for field in fields]
|
|
100
|
+
self.aligns = len(fields)*["c"]
|
|
101
|
+
self.cache = {}
|
|
102
|
+
self.html_cache = {}
|
|
103
|
+
|
|
104
|
+
def set_field_align(self, fieldname, align):
|
|
105
|
+
|
|
106
|
+
"""Set the alignment of a field by its fieldname
|
|
107
|
+
|
|
108
|
+
Arguments:
|
|
109
|
+
|
|
110
|
+
fieldname - name of the field whose alignment is to be changed
|
|
111
|
+
align - desired alignment - "l" for left, "c" for centre and "r" for right"""
|
|
112
|
+
|
|
113
|
+
if fieldname not in self.fields:
|
|
114
|
+
raise Exception("No field %s exists!" % fieldname)
|
|
115
|
+
if align not in ["l","c","r"]:
|
|
116
|
+
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
117
|
+
self.aligns[self.fields.index(fieldname)] = align
|
|
118
|
+
self.cache = {}
|
|
119
|
+
self.html_cache = {}
|
|
120
|
+
|
|
121
|
+
def set_padding_width(self, padding_width):
|
|
122
|
+
|
|
123
|
+
"""Set the number of empty spaces between a column's edge and its content
|
|
124
|
+
|
|
125
|
+
Arguments:
|
|
126
|
+
|
|
127
|
+
padding_width - number of spaces, must be a positive integer"""
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
assert int(padding_width) >= 0
|
|
131
|
+
except AssertionError:
|
|
132
|
+
raise Exception("Invalid value for padding_width: %s!" % str(padding_width))
|
|
133
|
+
|
|
134
|
+
self.padding_width = padding_width
|
|
135
|
+
self.cache = {}
|
|
136
|
+
self.html_cache = {}
|
|
137
|
+
|
|
138
|
+
def set_left_padding(self, left_padding):
|
|
139
|
+
|
|
140
|
+
"""Set the number of empty spaces between a column's left edge and its content
|
|
141
|
+
|
|
142
|
+
Arguments:
|
|
143
|
+
|
|
144
|
+
left_padding - number of spaces, must be a positive integer"""
|
|
145
|
+
|
|
146
|
+
try:
|
|
147
|
+
assert left_padding == None or int(left_padding) >= 0
|
|
148
|
+
except AssertionError:
|
|
149
|
+
raise Exception("Invalid value for left_padding: %s!" % str(left_padding))
|
|
150
|
+
|
|
151
|
+
self.left_padding = left_padding
|
|
152
|
+
self.cache = {}
|
|
153
|
+
self.html_cache = {}
|
|
154
|
+
|
|
155
|
+
def set_right_padding(self, right_padding):
|
|
156
|
+
|
|
157
|
+
"""Set the number of empty spaces between a column's right edge and its content
|
|
158
|
+
|
|
159
|
+
Arguments:
|
|
160
|
+
|
|
161
|
+
right_padding - number of spaces, must be a positive integer"""
|
|
162
|
+
|
|
163
|
+
try:
|
|
164
|
+
assert right_padding == None or int(right_padding) >= 0
|
|
165
|
+
except AssertionError:
|
|
166
|
+
raise Exception("Invalid value for right_padding: %s!" % str(right_padding))
|
|
167
|
+
|
|
168
|
+
self.right_padding = right_padding
|
|
169
|
+
self.cache = {}
|
|
170
|
+
self.html_cache = {}
|
|
171
|
+
|
|
172
|
+
def set_border_chars(self, vertical="|", horizontal="-", junction="+"):
|
|
173
|
+
|
|
174
|
+
"""Set the characters to use when drawing the table border
|
|
175
|
+
|
|
176
|
+
Arguments:
|
|
177
|
+
|
|
178
|
+
vertical - character used to draw a vertical line segment. Default is |
|
|
179
|
+
horizontal - character used to draw a horizontal line segment. Default is -
|
|
180
|
+
junction - character used to draw a line junction. Default is +"""
|
|
181
|
+
|
|
182
|
+
if len(vertical) > 1 or len(horizontal) > 1 or len(junction) > 1:
|
|
183
|
+
raise Exception("All border characters must be strings of length ONE!")
|
|
184
|
+
self.vertical_char = vertical
|
|
185
|
+
self.horizontal_char = horizontal
|
|
186
|
+
self.junction_char = junction
|
|
187
|
+
self.cache = {}
|
|
188
|
+
|
|
189
|
+
def add_row(self, row):
|
|
190
|
+
|
|
191
|
+
"""Add a row to the table
|
|
192
|
+
|
|
193
|
+
Arguments:
|
|
194
|
+
|
|
195
|
+
row - row of data, should be a list with as many elements as the table
|
|
196
|
+
has fields"""
|
|
197
|
+
|
|
198
|
+
if len(row) != len(self.fields):
|
|
199
|
+
raise Exception("Row has incorrect number of values")
|
|
200
|
+
self.rows.append(row)
|
|
201
|
+
for i in range(0,len(row)):
|
|
202
|
+
if len(str(row[i])) > self.widths[i]:
|
|
203
|
+
self.widths[i] = len(str(row[i]))
|
|
204
|
+
self.html_cache = {}
|
|
205
|
+
|
|
206
|
+
def add_column(self, fieldname, column, align="c"):
|
|
207
|
+
|
|
208
|
+
"""Add a column to the table.
|
|
209
|
+
|
|
210
|
+
Arguments:
|
|
211
|
+
|
|
212
|
+
fieldname - name of the field to contain the new column of data
|
|
213
|
+
column - column of data, should be a list with as many elements as the
|
|
214
|
+
table has rows
|
|
215
|
+
align - desired alignment for this column - "l" for left, "c" for centre and "r" for right"""
|
|
216
|
+
|
|
217
|
+
if len(self.rows) in (0, len(column)):
|
|
218
|
+
if align not in ["l","c","r"]:
|
|
219
|
+
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
220
|
+
self.fields.append(fieldname)
|
|
221
|
+
self.widths.append(len(fieldname))
|
|
222
|
+
self.aligns.append(align)
|
|
223
|
+
for i in range(0, len(column)):
|
|
224
|
+
if len(self.rows) < i+1:
|
|
225
|
+
self.rows.append([])
|
|
226
|
+
self.rows[i].append(column[i])
|
|
227
|
+
if len(str(column[i])) > self.widths[-1]:
|
|
228
|
+
self.widths[-1] = len(str(column[i]))
|
|
229
|
+
else:
|
|
230
|
+
raise Exception("Column length %d does not match number of rows %d!" % (len(column), len(self.rows)))
|
|
231
|
+
|
|
232
|
+
def printt(self, start=0, end=None, fields=None, header=True, border=True, hrules=FRAME, sortby=None, reversesort=False):
|
|
233
|
+
|
|
234
|
+
"""Print table in current state to stdout.
|
|
235
|
+
|
|
236
|
+
Arguments:
|
|
237
|
+
|
|
238
|
+
start - index of first data row to include in output
|
|
239
|
+
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
240
|
+
fields - names of fields (columns) to include
|
|
241
|
+
sortby - name of field to sort rows by
|
|
242
|
+
reversesort - True or False to sort in descending or ascending order
|
|
243
|
+
border - should be True or False to print or not print borders
|
|
244
|
+
hrules - controls printing of horizontal rules after each row. Allowed values: FRAME, ALL, NONE"""
|
|
245
|
+
|
|
246
|
+
print self.get_string(start, end, fields, header, border, hrules, sortby, reversesort)
|
|
247
|
+
|
|
248
|
+
def get_string(self, start=0, end=None, fields=None, header=True, border=True, hrules=FRAME, sortby=None, reversesort=False):
|
|
249
|
+
|
|
250
|
+
"""Return string representation of table in current state.
|
|
251
|
+
|
|
252
|
+
Arguments:
|
|
253
|
+
|
|
254
|
+
start - index of first data row to include in output
|
|
255
|
+
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
256
|
+
fields - names of fields (columns) to include
|
|
257
|
+
sortby - name of field to sort rows by
|
|
258
|
+
reversesort - True or False to sort in descending or ascending order
|
|
259
|
+
border - should be True or False to print or not print borders
|
|
260
|
+
hrules - controls printing of horizontal rules after each row. Allowed values: FRAME, ALL, NONE"""
|
|
261
|
+
|
|
262
|
+
if self.caching:
|
|
263
|
+
key = cPickle.dumps((start, end, fields, header, border, hrules, sortby, reversesort))
|
|
264
|
+
if key in self.cache:
|
|
265
|
+
return self.cache[key]
|
|
266
|
+
|
|
267
|
+
hrule = hrules or self.hrules
|
|
268
|
+
bits = []
|
|
269
|
+
if not self.fields:
|
|
270
|
+
return ""
|
|
271
|
+
if header:
|
|
272
|
+
bits.append(self._stringify_header(fields, border, hrules))
|
|
273
|
+
elif hrules != NONE:
|
|
274
|
+
bits.append(self._stringify_hrule(fields, border))
|
|
275
|
+
if sortby:
|
|
276
|
+
rows = self._get_sorted_rows(start, end, sortby, reversesort)
|
|
277
|
+
else:
|
|
278
|
+
rows = self.rows[start:end]
|
|
279
|
+
for row in rows:
|
|
280
|
+
bits.append(self._stringify_row(row, fields, border, hrule))
|
|
281
|
+
if border and not hrule:
|
|
282
|
+
bits.append(self._stringify_hrule(fields, border))
|
|
283
|
+
string = "\n".join(bits)
|
|
284
|
+
|
|
285
|
+
if self.caching:
|
|
286
|
+
self.cache[key] = string
|
|
287
|
+
|
|
288
|
+
return string
|
|
289
|
+
|
|
290
|
+
def _get_sorted_rows(self, start, end, sortby, reversesort):
|
|
291
|
+
|
|
292
|
+
# Sort rows using the "Decorate, Sort, Undecorate" (DSU) paradigm
|
|
293
|
+
rows = copy.deepcopy(self.rows[start:end])
|
|
294
|
+
sortindex = self.fields.index(sortby)
|
|
295
|
+
# Decorate
|
|
296
|
+
rows = [[row[sortindex]]+row for row in rows]
|
|
297
|
+
# Sort
|
|
298
|
+
rows.sort(reverse=reversesort)
|
|
299
|
+
# Undecorate
|
|
300
|
+
rows = [row[1:] for row in rows]
|
|
301
|
+
return rows
|
|
302
|
+
|
|
303
|
+
def _get_paddings(self):
|
|
304
|
+
|
|
305
|
+
if self.left_padding is not None:
|
|
306
|
+
lpad = self.left_padding
|
|
307
|
+
else:
|
|
308
|
+
lpad = self.padding_width
|
|
309
|
+
if self.right_padding is not None:
|
|
310
|
+
rpad = self.right_padding
|
|
311
|
+
else:
|
|
312
|
+
rpad = self.padding_width
|
|
313
|
+
return lpad, rpad
|
|
314
|
+
|
|
315
|
+
def _stringify_hrule(self, fields=None, border=True):
|
|
316
|
+
|
|
317
|
+
if not border:
|
|
318
|
+
return ""
|
|
319
|
+
lpad, rpad = self._get_paddings()
|
|
320
|
+
padding_width = lpad+rpad
|
|
321
|
+
bits = [self.junction_char]
|
|
322
|
+
for field, width in zip(self.fields, self.widths):
|
|
323
|
+
if fields and field not in fields:
|
|
324
|
+
continue
|
|
325
|
+
bits.append((width+padding_width)*self.horizontal_char)
|
|
326
|
+
bits.append(self.junction_char)
|
|
327
|
+
return "".join(bits)
|
|
328
|
+
|
|
329
|
+
def _stringify_header(self, fields=None, border=True, hrules=FRAME):
|
|
330
|
+
|
|
331
|
+
lpad, rpad = self._get_paddings()
|
|
332
|
+
bits = []
|
|
333
|
+
if border:
|
|
334
|
+
if hrules != NONE:
|
|
335
|
+
bits.append(self._stringify_hrule(fields, border))
|
|
336
|
+
bits.append("\n")
|
|
337
|
+
bits.append(self.vertical_char)
|
|
338
|
+
for field, width in zip(self.fields, self.widths):
|
|
339
|
+
if fields and field not in fields:
|
|
340
|
+
continue
|
|
341
|
+
bits.append(" " * lpad + field.center(width) + " " * rpad)
|
|
342
|
+
if border:
|
|
343
|
+
bits.append(self.vertical_char)
|
|
344
|
+
if border and hrules != NONE:
|
|
345
|
+
bits.append("\n")
|
|
346
|
+
bits.append(self._stringify_hrule(fields, border))
|
|
347
|
+
return "".join(bits)
|
|
348
|
+
|
|
349
|
+
def _stringify_row(self, row, fields=None, border=True, hrule=False):
|
|
350
|
+
|
|
351
|
+
lpad, rpad = self._get_paddings()
|
|
352
|
+
bits = []
|
|
353
|
+
if border:
|
|
354
|
+
bits.append(self.vertical_char)
|
|
355
|
+
for field, value, width, align in zip(self.fields, row, self.widths, self.aligns):
|
|
356
|
+
if fields and field not in fields:
|
|
357
|
+
continue
|
|
358
|
+
if align == "l":
|
|
359
|
+
bits.append(" " * lpad + str(value).ljust(width) + " " * rpad)
|
|
360
|
+
elif align == "r":
|
|
361
|
+
bits.append(" " * lpad + str(value).rjust(width) + " " * rpad)
|
|
362
|
+
else:
|
|
363
|
+
bits.append(" " * lpad + str(value).center(width) + " " * rpad)
|
|
364
|
+
if border:
|
|
365
|
+
bits.append(self.vertical_char)
|
|
366
|
+
if border and hrule == ALL:
|
|
367
|
+
bits.append("\n")
|
|
368
|
+
bits.append(self._stringify_hrule(fields, border))
|
|
369
|
+
return "".join(bits)
|
|
370
|
+
|
|
371
|
+
def print_html(self, start=0, end=None, fields=None, header=True, sortby=None, reversesort=False):
|
|
372
|
+
|
|
373
|
+
"""Print HTML formatted version of table in current state to stdout.
|
|
374
|
+
|
|
375
|
+
Arguments:
|
|
376
|
+
|
|
377
|
+
start - index of first data row to include in output
|
|
378
|
+
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
379
|
+
fields - names of fields (columns) to include
|
|
380
|
+
border - should be True or False to print or not print borders
|
|
381
|
+
hrules - include horizontal rule after each row"""
|
|
382
|
+
|
|
383
|
+
print self.get_html_string(start, end, fields, header, sortby, reversesort)
|
|
384
|
+
|
|
385
|
+
def get_html_string(self, start=0, end=None, fields=None, header=True, sortby=None, reversesort=False):
|
|
386
|
+
|
|
387
|
+
"""Return string representation of HTML formatted version of table in current state.
|
|
388
|
+
|
|
389
|
+
Arguments:
|
|
390
|
+
|
|
391
|
+
start - index of first data row to include in output
|
|
392
|
+
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
393
|
+
fields - names of fields (columns) to include
|
|
394
|
+
border - should be True or False to print or not print borders
|
|
395
|
+
hrules - include horizontal rule after each row"""
|
|
396
|
+
|
|
397
|
+
if self.caching:
|
|
398
|
+
key = cPickle.dumps((start, end, fields, header, sortby, reversesort))
|
|
399
|
+
if key in self.html_cache:
|
|
400
|
+
return self.html_cache[key]
|
|
401
|
+
|
|
402
|
+
bits = []
|
|
403
|
+
bits.append("<table>")
|
|
404
|
+
# Headers
|
|
405
|
+
if header:
|
|
406
|
+
bits.append(" <tr>")
|
|
407
|
+
for field in self.fields:
|
|
408
|
+
if fields and field not in fields:
|
|
409
|
+
continue
|
|
410
|
+
bits.append(" <th>%s</th>" % cgi.escape(field))
|
|
411
|
+
bits.append(" </tr>")
|
|
412
|
+
# Data
|
|
413
|
+
if sortby:
|
|
414
|
+
rows = self._get_sorted_rows(stard, end, sortby, reversesort)
|
|
415
|
+
else:
|
|
416
|
+
rows = self.rows
|
|
417
|
+
for row in self.rows:
|
|
418
|
+
bits.append(" <tr>")
|
|
419
|
+
for field, datum in zip(self.fields, row):
|
|
420
|
+
if fields and field in fields:
|
|
421
|
+
continue
|
|
422
|
+
bits.append(" <td>%s</td>" % cgi.escape(str(datum)))
|
|
423
|
+
bits.append(" </tr>")
|
|
424
|
+
bits.append("</table>")
|
|
425
|
+
string = "\n".join(bits)
|
|
426
|
+
|
|
427
|
+
if self.caching:
|
|
428
|
+
self.html_cache[key] = string
|
|
429
|
+
|
|
430
|
+
return string
|
|
431
|
+
|
|
432
|
+
def main():
|
|
433
|
+
|
|
434
|
+
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
|
|
435
|
+
x.set_field_align("City name", "l") # Left align city names
|
|
436
|
+
x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
437
|
+
x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
438
|
+
x.add_row(["Darwin", 112, 120900, 1714.7])
|
|
439
|
+
x.add_row(["Hobart", 1357, 205556, 619.5])
|
|
440
|
+
x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
441
|
+
x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
442
|
+
x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
443
|
+
print x
|
|
444
|
+
|
|
445
|
+
if len(sys.argv) > 1 and sys.argv[1] == "test":
|
|
446
|
+
|
|
447
|
+
# This "test suite" is hideous and provides poor, arbitrary coverage.
|
|
448
|
+
# I'll replace it with some proper unit tests Sometime Soon (TM).
|
|
449
|
+
# Promise.
|
|
450
|
+
print "Testing field subset selection:"
|
|
451
|
+
x.printt(fields=["City name","Population"])
|
|
452
|
+
print "Testing row subset selection:"
|
|
453
|
+
x.printt(start=2, end=5)
|
|
454
|
+
print "Testing hrules settings:"
|
|
455
|
+
print "FRAME:"
|
|
456
|
+
x.printt(hrules=FRAME)
|
|
457
|
+
print "ALL:"
|
|
458
|
+
x.printt(hrules=ALL)
|
|
459
|
+
print "NONE:"
|
|
460
|
+
x.printt(hrules=NONE)
|
|
461
|
+
print "Testing lack of headers:"
|
|
462
|
+
x.printt(header=False)
|
|
463
|
+
print "Testing lack of borders:"
|
|
464
|
+
x.printt(border=False)
|
|
465
|
+
print "Testing sorting:"
|
|
466
|
+
x.printt(sortby="City name")
|
|
467
|
+
x.printt(sortby="Annual Rainfall")
|
|
468
|
+
x.printt(sortby="Annual Rainfall", reversesort=True)
|
|
469
|
+
print "Testing padding parameter:"
|
|
470
|
+
x.set_padding_width(0)
|
|
471
|
+
x.printt()
|
|
472
|
+
x.set_padding_width(5)
|
|
473
|
+
x.printt()
|
|
474
|
+
x.set_left_padding(5)
|
|
475
|
+
x.set_right_padding(0)
|
|
476
|
+
x.printt()
|
|
477
|
+
x.set_right_padding(20)
|
|
478
|
+
x.printt()
|
|
479
|
+
x.set_left_padding(None)
|
|
480
|
+
x.set_right_padding(None)
|
|
481
|
+
x.set_padding_width(2)
|
|
482
|
+
print "Testing changing characters"
|
|
483
|
+
x.set_border_chars("*","*","*")
|
|
484
|
+
x.printt()
|
|
485
|
+
x.set_border_chars("!","~","o")
|
|
486
|
+
x.printt()
|
|
487
|
+
x.set_border_chars("|","-","+")
|
|
488
|
+
print "Testing everything at once:"
|
|
489
|
+
x.printt(start=2, end=5, fields=["City name","Population"], border=False, hrules=True)
|
|
490
|
+
print "Rebuilding by columns:"
|
|
491
|
+
x = PrettyTable()
|
|
492
|
+
x.add_column("City name", ["Adelaide", "Brisbane", "Darwin", "Hobart", "Sydney", "Melbourne", "Perth"])
|
|
493
|
+
x.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5385])
|
|
494
|
+
x.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
|
|
495
|
+
x.add_column("Annual Rainfall", [600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
|
|
496
|
+
x.printt()
|
|
497
|
+
print "Testing HTML:"
|
|
498
|
+
x.print_html()
|
|
499
|
+
x.print_html(header=False)
|
|
500
|
+
|
|
501
|
+
if __name__ == "__main__":
|
|
502
|
+
main()
|
|
@@ -3,10 +3,11 @@ from setuptools import setup
|
|
|
3
3
|
|
|
4
4
|
setup(
|
|
5
5
|
name='prettytable',
|
|
6
|
-
version='0.
|
|
6
|
+
version='0.4',
|
|
7
7
|
description='A simple Python library for easily displaying tabular data in a visually appealing ASCII table format',
|
|
8
8
|
author='Luke Maurits',
|
|
9
9
|
author_email='luke@maurits.id.au',
|
|
10
10
|
url='http://www.luke.maurits.id.au/software/prettytable',
|
|
11
|
+
license='http://www.luke.maurits.id.au/software/bsdlicense.txt',
|
|
11
12
|
py_modules=['prettytable']
|
|
12
13
|
)
|
prettytable-0.3/prettytable.py
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
# PrettyTable 0.3
|
|
2
|
-
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
3
|
-
# All rights reserved.
|
|
4
|
-
# With contributions from:
|
|
5
|
-
# * Chris Clark
|
|
6
|
-
#
|
|
7
|
-
# Redistribution and use in source and binary forms, with or without
|
|
8
|
-
# modification, are permitted provided that the following conditions are met:
|
|
9
|
-
#
|
|
10
|
-
# * Redistributions of source code must retain the above copyright notice,
|
|
11
|
-
# this list of conditions and the following disclaimer.
|
|
12
|
-
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
# this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
# and/or other materials provided with the distribution.
|
|
15
|
-
# * The name of the author may not be used to endorse or promote products
|
|
16
|
-
# derived from this software without specific prior written permission.
|
|
17
|
-
#
|
|
18
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
-
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
22
|
-
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
-
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
-
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
-
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
-
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
-
# POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
-
|
|
30
|
-
import copy
|
|
31
|
-
import cPickle
|
|
32
|
-
import sys
|
|
33
|
-
|
|
34
|
-
class PrettyTable:
|
|
35
|
-
|
|
36
|
-
def __init__(self, fields=None, caching=True, padding_width=1):
|
|
37
|
-
|
|
38
|
-
"""Return a new PrettyTable instance
|
|
39
|
-
|
|
40
|
-
Arguments:
|
|
41
|
-
|
|
42
|
-
fields - list or tuple of field names
|
|
43
|
-
caching - boolean value to turn string caching on/off
|
|
44
|
-
padding width - number of spaces between column lines and content"""
|
|
45
|
-
|
|
46
|
-
# Check params
|
|
47
|
-
try:
|
|
48
|
-
assert int(padding_width) >= 0
|
|
49
|
-
except AssertionError:
|
|
50
|
-
raise Exception("Invalid value for padding_width: %s!" % str(padding_width))
|
|
51
|
-
|
|
52
|
-
# Data
|
|
53
|
-
if fields:
|
|
54
|
-
self.set_field_names(fields)
|
|
55
|
-
else:
|
|
56
|
-
self.fields = []
|
|
57
|
-
self.widths = []
|
|
58
|
-
self.aligns = []
|
|
59
|
-
self.rows = []
|
|
60
|
-
self.cache = {}
|
|
61
|
-
|
|
62
|
-
# Options
|
|
63
|
-
self.hrules = False
|
|
64
|
-
self.caching = caching
|
|
65
|
-
self.padding_width = padding_width
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def __getslice__(self, i, j):
|
|
69
|
-
|
|
70
|
-
"""Return a new PrettyTable whose data rows are a slice of this one's
|
|
71
|
-
|
|
72
|
-
Arguments:
|
|
73
|
-
|
|
74
|
-
i - beginning slice index
|
|
75
|
-
j - ending slice index"""
|
|
76
|
-
|
|
77
|
-
newtable = copy.deepcopy(self)
|
|
78
|
-
newtable.rows = self.rows[i:j]
|
|
79
|
-
return newtable
|
|
80
|
-
|
|
81
|
-
def __str__(self):
|
|
82
|
-
|
|
83
|
-
return self.get_string()
|
|
84
|
-
|
|
85
|
-
def set_field_names(self, fields):
|
|
86
|
-
|
|
87
|
-
"""Set the alignment of a field by its fieldname
|
|
88
|
-
|
|
89
|
-
Arguments:
|
|
90
|
-
|
|
91
|
-
fields - list or tuple of field names"""
|
|
92
|
-
|
|
93
|
-
self.fields = fields
|
|
94
|
-
self.widths = [len(field) for field in fields]
|
|
95
|
-
self.aligns = len(fields)*["c"]
|
|
96
|
-
self.cache = {}
|
|
97
|
-
|
|
98
|
-
def set_field_align(self, fieldname, align):
|
|
99
|
-
|
|
100
|
-
"""Set the alignment of a field by its fieldname
|
|
101
|
-
|
|
102
|
-
Arguments:
|
|
103
|
-
|
|
104
|
-
fieldname - name of the field whose alignment is to be changed
|
|
105
|
-
align - desired alignment - "l" for left, "c" for centre and "r" for right"""
|
|
106
|
-
|
|
107
|
-
if fieldname not in self.fields:
|
|
108
|
-
raise Exception("No field %s exists!" % fieldname)
|
|
109
|
-
if align not in ["l","c","r"]:
|
|
110
|
-
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
111
|
-
self.aligns[self.fields.index(fieldname)] = align
|
|
112
|
-
self.cache = {}
|
|
113
|
-
|
|
114
|
-
def set_padding_width(self, padding_width):
|
|
115
|
-
|
|
116
|
-
"""Set the number of empty spaces between a column's edge and its content
|
|
117
|
-
|
|
118
|
-
Arguments:
|
|
119
|
-
|
|
120
|
-
padding_width - number of spaces, must be a positive integer"""
|
|
121
|
-
|
|
122
|
-
try:
|
|
123
|
-
assert int(padding_width) >= 0
|
|
124
|
-
except AssertionError:
|
|
125
|
-
raise Exception("Invalid value for padding_width: %s!" % str(padding_width))
|
|
126
|
-
|
|
127
|
-
self.padding_width = padding_width
|
|
128
|
-
self.cache = {}
|
|
129
|
-
|
|
130
|
-
def add_row(self, row):
|
|
131
|
-
|
|
132
|
-
"""Add a row to the table
|
|
133
|
-
|
|
134
|
-
Arguments:
|
|
135
|
-
|
|
136
|
-
row - row of data, should be a list with as many elements as the table
|
|
137
|
-
has fields"""
|
|
138
|
-
|
|
139
|
-
if len(row) != len(self.fields):
|
|
140
|
-
raise Exception("Row has incorrect number of values")
|
|
141
|
-
self.rows.append(row)
|
|
142
|
-
for i in range(0,len(row)):
|
|
143
|
-
if len(str(row[i])) > self.widths[i]:
|
|
144
|
-
self.widths[i] = len(str(row[i]))
|
|
145
|
-
self.cache = {}
|
|
146
|
-
|
|
147
|
-
def get_string(self, start=0, end=None, fields=None, hrules=None):
|
|
148
|
-
|
|
149
|
-
"""Return string representation of table in current state.
|
|
150
|
-
|
|
151
|
-
Arguments:
|
|
152
|
-
|
|
153
|
-
start - index of first data row to include in output
|
|
154
|
-
end - index of last data row to include in output PLUS ONE (list slice style)"""
|
|
155
|
-
|
|
156
|
-
if self.caching:
|
|
157
|
-
key = cPickle.dumps((start, end, fields, hrules))
|
|
158
|
-
if key in self.cache:
|
|
159
|
-
return self.cache[key]
|
|
160
|
-
|
|
161
|
-
bits = []
|
|
162
|
-
if not self.fields:
|
|
163
|
-
return ""
|
|
164
|
-
bits.append(self._stringify_header(fields))
|
|
165
|
-
hrule = hrules or self.hrules
|
|
166
|
-
for row in self.rows[start:end]:
|
|
167
|
-
bits.append(self._stringify_row(row, fields, hrule))
|
|
168
|
-
if not hrule:
|
|
169
|
-
bits.append(self._stringify_hrule(fields))
|
|
170
|
-
string = "\n".join(bits)
|
|
171
|
-
|
|
172
|
-
if self.caching:
|
|
173
|
-
self.cache[key] = string
|
|
174
|
-
|
|
175
|
-
return string
|
|
176
|
-
|
|
177
|
-
def printt(self, start=0, end=None, fields=None, hrules=None):
|
|
178
|
-
|
|
179
|
-
"""Print table in current state to stdout.
|
|
180
|
-
|
|
181
|
-
Arguments:
|
|
182
|
-
|
|
183
|
-
start - index of first data row to include in output
|
|
184
|
-
end - index of last data row to include in output PLUS ONE (list slice style)"""
|
|
185
|
-
|
|
186
|
-
print self.get_string(start, end, fields, hrules)
|
|
187
|
-
|
|
188
|
-
def _stringify_hrule(self, fields=None):
|
|
189
|
-
|
|
190
|
-
padding_width=self.padding_width
|
|
191
|
-
bits = ["+"]
|
|
192
|
-
for field, width in zip(self.fields, self.widths):
|
|
193
|
-
if fields and field not in fields:
|
|
194
|
-
continue
|
|
195
|
-
bits.append((width+2*padding_width)*"-")
|
|
196
|
-
bits.append("+")
|
|
197
|
-
return "".join(bits)
|
|
198
|
-
|
|
199
|
-
def _stringify_header(self, fields=None):
|
|
200
|
-
|
|
201
|
-
padding_width = self.padding_width
|
|
202
|
-
bits = [self._stringify_hrule(fields)]
|
|
203
|
-
bits.append("\n")
|
|
204
|
-
bits.append("|")
|
|
205
|
-
for field, width in zip(self.fields, self.widths):
|
|
206
|
-
if fields and field not in fields:
|
|
207
|
-
continue
|
|
208
|
-
bits.append(field.center(width+2*padding_width))
|
|
209
|
-
bits.append("|")
|
|
210
|
-
bits.append("\n")
|
|
211
|
-
bits.append(self._stringify_hrule(fields))
|
|
212
|
-
return "".join(bits)
|
|
213
|
-
|
|
214
|
-
def _stringify_row(self, row, fields, hrule):
|
|
215
|
-
|
|
216
|
-
padding_width = self.padding_width
|
|
217
|
-
bits = ["|"]
|
|
218
|
-
for field, value, width, align in zip(self.fields, row, self.widths, self.aligns):
|
|
219
|
-
if fields and field not in fields:
|
|
220
|
-
continue
|
|
221
|
-
if align == "l":
|
|
222
|
-
bits.append(str(value).ljust(width).center(width+2*padding_width))
|
|
223
|
-
elif align == "r":
|
|
224
|
-
bits.append(str(value).rjust(width).center(width+2*padding_width))
|
|
225
|
-
else:
|
|
226
|
-
bits.append(str(value).center(width+2*padding_width))
|
|
227
|
-
bits.append("|")
|
|
228
|
-
if hrule:
|
|
229
|
-
bits.append("\n")
|
|
230
|
-
bits.append(self._stringify_hrule(fields))
|
|
231
|
-
return "".join(bits)
|
|
232
|
-
|
|
233
|
-
def main():
|
|
234
|
-
|
|
235
|
-
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
|
|
236
|
-
x.set_field_align("City name", "l") # Left align city names
|
|
237
|
-
x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
238
|
-
x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
239
|
-
x.add_row(["Darwin", 112, 120900, 1714.7])
|
|
240
|
-
x.add_row(["Hobart", 1357, 205556, 619.5])
|
|
241
|
-
x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
242
|
-
x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
243
|
-
x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
244
|
-
print x
|
|
245
|
-
|
|
246
|
-
if len(sys.argv) > 1 and sys.argv[1] == "test":
|
|
247
|
-
print "Testing field subset selection:"
|
|
248
|
-
x.printt(fields=["City name","Population"])
|
|
249
|
-
print "Testing row subset selection:"
|
|
250
|
-
x.printt(start=2, end=5)
|
|
251
|
-
print "Testing inter-line hrules:"
|
|
252
|
-
x.printt(hrules=True)
|
|
253
|
-
print "Testing padding parameter:"
|
|
254
|
-
x.set_padding_width(0)
|
|
255
|
-
x.printt()
|
|
256
|
-
x.set_padding_width(5)
|
|
257
|
-
x.printt()
|
|
258
|
-
x.set_padding_width(2)
|
|
259
|
-
print "Testing everything at once:"
|
|
260
|
-
x.printt(start=2, end=5, fields=["City name","Population"], hrules=True)
|
|
261
|
-
|
|
262
|
-
if __name__ == "__main__":
|
|
263
|
-
main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|