prettytable 0.4__zip → 0.5__zip
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.4 → prettytable-0.5}/PKG-INFO +1 -1
- {prettytable-0.4 → prettytable-0.5}/prettytable.egg-info/PKG-INFO +1 -1
- {prettytable-0.4 → prettytable-0.5}/prettytable.py +236 -113
- {prettytable-0.4 → prettytable-0.5}/setup.py +1 -1
- {prettytable-0.4 → prettytable-0.5}/prettytable.egg-info/SOURCES.txt +0 -0
- {prettytable-0.4 → prettytable-0.5}/prettytable.egg-info/dependency_links.txt +0 -0
- {prettytable-0.4 → prettytable-0.5}/prettytable.egg-info/top_level.txt +0 -0
- {prettytable-0.4 → prettytable-0.5}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 1.0
|
|
2
2
|
Name: prettytable
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5
|
|
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
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 1.0
|
|
2
2
|
Name: prettytable
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5
|
|
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
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#
|
|
3
|
+
# PrettyTable 0.5
|
|
2
4
|
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
3
5
|
# All rights reserved.
|
|
4
6
|
# With contributions from:
|
|
@@ -39,20 +41,20 @@ NONE = 2
|
|
|
39
41
|
class PrettyTable:
|
|
40
42
|
|
|
41
43
|
def __init__(self, fields=None, caching=True, padding_width=1, left_padding=None, right_padding=None):
|
|
42
|
-
|
|
44
|
+
|
|
43
45
|
"""Return a new PrettyTable instance
|
|
44
46
|
|
|
45
47
|
Arguments:
|
|
46
|
-
|
|
48
|
+
|
|
47
49
|
fields - list or tuple of field names
|
|
48
50
|
caching - boolean value to turn string caching on/off
|
|
49
51
|
padding width - number of spaces between column lines and content"""
|
|
50
52
|
|
|
51
53
|
# Data
|
|
54
|
+
self.fields = []
|
|
52
55
|
if fields:
|
|
53
56
|
self.set_field_names(fields)
|
|
54
57
|
else:
|
|
55
|
-
self.fields = []
|
|
56
58
|
self.widths = []
|
|
57
59
|
self.aligns = []
|
|
58
60
|
self.set_padding_width(padding_width)
|
|
@@ -75,10 +77,10 @@ class PrettyTable:
|
|
|
75
77
|
"""Return a new PrettyTable whose data rows are a slice of this one's
|
|
76
78
|
|
|
77
79
|
Arguments:
|
|
78
|
-
|
|
80
|
+
|
|
79
81
|
i - beginning slice index
|
|
80
82
|
j - ending slice index"""
|
|
81
|
-
|
|
83
|
+
|
|
82
84
|
newtable = copy.deepcopy(self)
|
|
83
85
|
newtable.rows = self.rows[i:j]
|
|
84
86
|
return newtable
|
|
@@ -86,17 +88,31 @@ class PrettyTable:
|
|
|
86
88
|
def __str__(self):
|
|
87
89
|
|
|
88
90
|
return self.get_string()
|
|
89
|
-
|
|
91
|
+
|
|
92
|
+
##############################
|
|
93
|
+
# ATTRIBUTE SETTERS #
|
|
94
|
+
##############################
|
|
95
|
+
|
|
90
96
|
def set_field_names(self, fields):
|
|
91
97
|
|
|
92
98
|
"""Set the names of the fields
|
|
93
99
|
|
|
94
100
|
Arguments:
|
|
95
|
-
|
|
101
|
+
|
|
96
102
|
fields - list or tuple of field names"""
|
|
97
103
|
|
|
104
|
+
# We *may* need to change the widths if this isn't the first time
|
|
105
|
+
# setting the field names. This could certainly be done more
|
|
106
|
+
# efficiently.
|
|
107
|
+
if self.fields:
|
|
108
|
+
self.widths = [len(field) for field in fields]
|
|
109
|
+
for row in self.rows:
|
|
110
|
+
for i in range(0,len(row)):
|
|
111
|
+
if len(unicode(row[i])) > self.widths[i]:
|
|
112
|
+
self.widths[i] = len(unicode(row[i]))
|
|
113
|
+
else:
|
|
114
|
+
self.widths = [len(field) for field in fields]
|
|
98
115
|
self.fields = fields
|
|
99
|
-
self.widths = [len(field) for field in fields]
|
|
100
116
|
self.aligns = len(fields)*["c"]
|
|
101
117
|
self.cache = {}
|
|
102
118
|
self.html_cache = {}
|
|
@@ -106,7 +122,7 @@ class PrettyTable:
|
|
|
106
122
|
"""Set the alignment of a field by its fieldname
|
|
107
123
|
|
|
108
124
|
Arguments:
|
|
109
|
-
|
|
125
|
+
|
|
110
126
|
fieldname - name of the field whose alignment is to be changed
|
|
111
127
|
align - desired alignment - "l" for left, "c" for centre and "r" for right"""
|
|
112
128
|
|
|
@@ -123,14 +139,14 @@ class PrettyTable:
|
|
|
123
139
|
"""Set the number of empty spaces between a column's edge and its content
|
|
124
140
|
|
|
125
141
|
Arguments:
|
|
126
|
-
|
|
142
|
+
|
|
127
143
|
padding_width - number of spaces, must be a positive integer"""
|
|
128
144
|
|
|
129
145
|
try:
|
|
130
146
|
assert int(padding_width) >= 0
|
|
131
147
|
except AssertionError:
|
|
132
|
-
raise Exception("Invalid value for padding_width: %s!" %
|
|
133
|
-
|
|
148
|
+
raise Exception("Invalid value for padding_width: %s!" % unicode(padding_width))
|
|
149
|
+
|
|
134
150
|
self.padding_width = padding_width
|
|
135
151
|
self.cache = {}
|
|
136
152
|
self.html_cache = {}
|
|
@@ -140,14 +156,14 @@ class PrettyTable:
|
|
|
140
156
|
"""Set the number of empty spaces between a column's left edge and its content
|
|
141
157
|
|
|
142
158
|
Arguments:
|
|
143
|
-
|
|
159
|
+
|
|
144
160
|
left_padding - number of spaces, must be a positive integer"""
|
|
145
161
|
|
|
146
162
|
try:
|
|
147
163
|
assert left_padding == None or int(left_padding) >= 0
|
|
148
164
|
except AssertionError:
|
|
149
|
-
raise Exception("Invalid value for left_padding: %s!" %
|
|
150
|
-
|
|
165
|
+
raise Exception("Invalid value for left_padding: %s!" % unicode(left_padding))
|
|
166
|
+
|
|
151
167
|
self.left_padding = left_padding
|
|
152
168
|
self.cache = {}
|
|
153
169
|
self.html_cache = {}
|
|
@@ -157,14 +173,14 @@ class PrettyTable:
|
|
|
157
173
|
"""Set the number of empty spaces between a column's right edge and its content
|
|
158
174
|
|
|
159
175
|
Arguments:
|
|
160
|
-
|
|
176
|
+
|
|
161
177
|
right_padding - number of spaces, must be a positive integer"""
|
|
162
178
|
|
|
163
179
|
try:
|
|
164
180
|
assert right_padding == None or int(right_padding) >= 0
|
|
165
181
|
except AssertionError:
|
|
166
|
-
raise Exception("Invalid value for right_padding: %s!" %
|
|
167
|
-
|
|
182
|
+
raise Exception("Invalid value for right_padding: %s!" % unicode(right_padding))
|
|
183
|
+
|
|
168
184
|
self.right_padding = right_padding
|
|
169
185
|
self.cache = {}
|
|
170
186
|
self.html_cache = {}
|
|
@@ -174,33 +190,37 @@ class PrettyTable:
|
|
|
174
190
|
"""Set the characters to use when drawing the table border
|
|
175
191
|
|
|
176
192
|
Arguments:
|
|
177
|
-
|
|
193
|
+
|
|
178
194
|
vertical - character used to draw a vertical line segment. Default is |
|
|
179
195
|
horizontal - character used to draw a horizontal line segment. Default is -
|
|
180
196
|
junction - character used to draw a line junction. Default is +"""
|
|
181
|
-
|
|
197
|
+
|
|
182
198
|
if len(vertical) > 1 or len(horizontal) > 1 or len(junction) > 1:
|
|
183
199
|
raise Exception("All border characters must be strings of length ONE!")
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
200
|
+
self.vertical_char = vertical
|
|
201
|
+
self.horizontal_char = horizontal
|
|
202
|
+
self.junction_char = junction
|
|
187
203
|
self.cache = {}
|
|
188
204
|
|
|
205
|
+
##############################
|
|
206
|
+
# DATA INPUT METHODS #
|
|
207
|
+
##############################
|
|
208
|
+
|
|
189
209
|
def add_row(self, row):
|
|
190
210
|
|
|
191
211
|
"""Add a row to the table
|
|
192
212
|
|
|
193
213
|
Arguments:
|
|
194
|
-
|
|
214
|
+
|
|
195
215
|
row - row of data, should be a list with as many elements as the table
|
|
196
216
|
has fields"""
|
|
197
|
-
|
|
217
|
+
|
|
198
218
|
if len(row) != len(self.fields):
|
|
199
|
-
raise Exception("Row has incorrect number of values")
|
|
219
|
+
raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self.fields)))
|
|
200
220
|
self.rows.append(row)
|
|
201
221
|
for i in range(0,len(row)):
|
|
202
|
-
if len(
|
|
203
|
-
self.widths[i] = len(
|
|
222
|
+
if len(unicode(row[i])) > self.widths[i]:
|
|
223
|
+
self.widths[i] = len(unicode(row[i]))
|
|
204
224
|
self.html_cache = {}
|
|
205
225
|
|
|
206
226
|
def add_column(self, fieldname, column, align="c"):
|
|
@@ -208,12 +228,12 @@ class PrettyTable:
|
|
|
208
228
|
"""Add a column to the table.
|
|
209
229
|
|
|
210
230
|
Arguments:
|
|
211
|
-
|
|
231
|
+
|
|
212
232
|
fieldname - name of the field to contain the new column of data
|
|
213
233
|
column - column of data, should be a list with as many elements as the
|
|
214
234
|
table has rows
|
|
215
235
|
align - desired alignment for this column - "l" for left, "c" for centre and "r" for right"""
|
|
216
|
-
|
|
236
|
+
|
|
217
237
|
if len(self.rows) in (0, len(column)):
|
|
218
238
|
if align not in ["l","c","r"]:
|
|
219
239
|
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
@@ -224,40 +244,72 @@ class PrettyTable:
|
|
|
224
244
|
if len(self.rows) < i+1:
|
|
225
245
|
self.rows.append([])
|
|
226
246
|
self.rows[i].append(column[i])
|
|
227
|
-
if len(
|
|
228
|
-
self.widths[-1] = len(
|
|
247
|
+
if len(unicode(column[i])) > self.widths[-1]:
|
|
248
|
+
self.widths[-1] = len(unicode(column[i]))
|
|
229
249
|
else:
|
|
230
250
|
raise Exception("Column length %d does not match number of rows %d!" % (len(column), len(self.rows)))
|
|
231
251
|
|
|
252
|
+
##############################
|
|
253
|
+
# MISC PRIVATE METHODS #
|
|
254
|
+
##############################
|
|
255
|
+
|
|
256
|
+
def _get_sorted_rows(self, start, end, sortby, reversesort):
|
|
257
|
+
# Sort rows using the "Decorate, Sort, Undecorate" (DSU) paradigm
|
|
258
|
+
rows = copy.deepcopy(self.rows[start:end])
|
|
259
|
+
sortindex = self.fields.index(sortby)
|
|
260
|
+
# Decorate
|
|
261
|
+
rows = [[row[sortindex]]+row for row in rows]
|
|
262
|
+
# Sort
|
|
263
|
+
rows.sort(reverse=reversesort)
|
|
264
|
+
# Undecorate
|
|
265
|
+
rows = [row[1:] for row in rows]
|
|
266
|
+
return rows
|
|
267
|
+
|
|
268
|
+
def _get_paddings(self):
|
|
269
|
+
|
|
270
|
+
if self.left_padding is not None:
|
|
271
|
+
lpad = self.left_padding
|
|
272
|
+
else:
|
|
273
|
+
lpad = self.padding_width
|
|
274
|
+
if self.right_padding is not None:
|
|
275
|
+
rpad = self.right_padding
|
|
276
|
+
else:
|
|
277
|
+
rpad = self.padding_width
|
|
278
|
+
return lpad, rpad
|
|
279
|
+
|
|
280
|
+
##############################
|
|
281
|
+
# ASCII PRINT/STRING METHODS #
|
|
282
|
+
##############################
|
|
283
|
+
|
|
232
284
|
def printt(self, start=0, end=None, fields=None, header=True, border=True, hrules=FRAME, sortby=None, reversesort=False):
|
|
233
285
|
|
|
234
286
|
"""Print table in current state to stdout.
|
|
235
|
-
|
|
287
|
+
|
|
236
288
|
Arguments:
|
|
237
|
-
|
|
289
|
+
|
|
238
290
|
start - index of first data row to include in output
|
|
239
291
|
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
240
292
|
fields - names of fields (columns) to include
|
|
241
|
-
|
|
242
|
-
|
|
293
|
+
sortby - name of field to sort rows by
|
|
294
|
+
reversesort - True or False to sort in descending or ascending order
|
|
243
295
|
border - should be True or False to print or not print borders
|
|
244
|
-
|
|
296
|
+
hrules - controls printing of horizontal rules after each row. Allowed values: FRAME, ALL, NONE"""
|
|
245
297
|
|
|
246
298
|
print self.get_string(start, end, fields, header, border, hrules, sortby, reversesort)
|
|
247
299
|
|
|
248
300
|
def get_string(self, start=0, end=None, fields=None, header=True, border=True, hrules=FRAME, sortby=None, reversesort=False):
|
|
249
301
|
|
|
250
302
|
"""Return string representation of table in current state.
|
|
251
|
-
|
|
303
|
+
|
|
252
304
|
Arguments:
|
|
253
|
-
|
|
305
|
+
|
|
254
306
|
start - index of first data row to include in output
|
|
255
307
|
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
256
308
|
fields - names of fields (columns) to include
|
|
257
|
-
|
|
258
|
-
|
|
309
|
+
sortby - name of field to sort rows by
|
|
310
|
+
reversesort - True or False to sort in descending or ascending order
|
|
259
311
|
border - should be True or False to print or not print borders
|
|
260
|
-
|
|
312
|
+
hrules - controls printing of horizontal rules after each row. Allowed values: FRAME, ALL, NONE"""
|
|
261
313
|
|
|
262
314
|
if self.caching:
|
|
263
315
|
key = cPickle.dumps((start, end, fields, header, border, hrules, sortby, reversesort))
|
|
@@ -268,49 +320,40 @@ class PrettyTable:
|
|
|
268
320
|
bits = []
|
|
269
321
|
if not self.fields:
|
|
270
322
|
return ""
|
|
323
|
+
if not header:
|
|
324
|
+
# Recalculate widths - avoids tables with long field names but narrow data looking odd
|
|
325
|
+
old_widths = self.widths[:]
|
|
326
|
+
self.widths = [0]*len(self.fields)
|
|
327
|
+
for row in self.rows:
|
|
328
|
+
for i in range(0,len(row)):
|
|
329
|
+
if len(unicode(row[i])) > self.widths[i]:
|
|
330
|
+
self.widths[i] = len(unicode(row[i]))
|
|
271
331
|
if header:
|
|
272
332
|
bits.append(self._stringify_header(fields, border, hrules))
|
|
273
|
-
elif hrules != NONE:
|
|
333
|
+
elif border and hrules != NONE:
|
|
274
334
|
bits.append(self._stringify_hrule(fields, border))
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
335
|
+
if sortby:
|
|
336
|
+
rows = self._get_sorted_rows(start, end, sortby, reversesort)
|
|
337
|
+
else:
|
|
338
|
+
rows = self.rows[start:end]
|
|
279
339
|
for row in rows:
|
|
280
340
|
bits.append(self._stringify_row(row, fields, border, hrule))
|
|
281
341
|
if border and not hrule:
|
|
282
342
|
bits.append(self._stringify_hrule(fields, border))
|
|
283
343
|
string = "\n".join(bits)
|
|
284
|
-
|
|
344
|
+
|
|
285
345
|
if self.caching:
|
|
286
346
|
self.cache[key] = string
|
|
287
347
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
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):
|
|
348
|
+
if not header:
|
|
349
|
+
# Restore previous widths
|
|
350
|
+
self.widths = old_widths
|
|
351
|
+
for row in self.rows:
|
|
352
|
+
for i in range(0,len(row)):
|
|
353
|
+
if len(unicode(row[i])) > self.widths[i]:
|
|
354
|
+
self.widths[i] = len(unicode(row[i]))
|
|
304
355
|
|
|
305
|
-
|
|
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
|
|
356
|
+
return string
|
|
314
357
|
|
|
315
358
|
def _stringify_hrule(self, fields=None, border=True):
|
|
316
359
|
|
|
@@ -356,77 +399,152 @@ class PrettyTable:
|
|
|
356
399
|
if fields and field not in fields:
|
|
357
400
|
continue
|
|
358
401
|
if align == "l":
|
|
359
|
-
bits.append(" " * lpad +
|
|
402
|
+
bits.append(" " * lpad + unicode(value).ljust(width) + " " * rpad)
|
|
360
403
|
elif align == "r":
|
|
361
|
-
bits.append(" " * lpad +
|
|
404
|
+
bits.append(" " * lpad + unicode(value).rjust(width) + " " * rpad)
|
|
362
405
|
else:
|
|
363
|
-
bits.append(" " * lpad +
|
|
406
|
+
bits.append(" " * lpad + unicode(value).center(width) + " " * rpad)
|
|
364
407
|
if border:
|
|
365
408
|
bits.append(self.vertical_char)
|
|
366
409
|
if border and hrule == ALL:
|
|
367
410
|
bits.append("\n")
|
|
368
411
|
bits.append(self._stringify_hrule(fields, border))
|
|
369
412
|
return "".join(bits)
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
413
|
+
|
|
414
|
+
##############################
|
|
415
|
+
# HTML PRINT/STRING METHODS #
|
|
416
|
+
##############################
|
|
417
|
+
|
|
418
|
+
def print_html(self, start=0, end=None, fields=None, sortby=None, reversesort=False, format=True, header=True, border=True, hrules=FRAME, attributes=None):
|
|
419
|
+
|
|
373
420
|
"""Print HTML formatted version of table in current state to stdout.
|
|
374
|
-
|
|
421
|
+
|
|
375
422
|
Arguments:
|
|
376
|
-
|
|
423
|
+
|
|
377
424
|
start - index of first data row to include in output
|
|
378
425
|
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
379
426
|
fields - names of fields (columns) to include
|
|
427
|
+
sortby - name of field to sort rows by
|
|
428
|
+
format - should be True or False to attempt to format alignmet, padding, etc. or not
|
|
429
|
+
header - should be True or False to print a header showing field names or not
|
|
380
430
|
border - should be True or False to print or not print borders
|
|
381
|
-
hrules - include horizontal rule after each row
|
|
431
|
+
hrules - include horizontal rule after each row
|
|
432
|
+
attributes - dictionary of name/value pairs to include as HTML attributes in the <table> tag"""
|
|
382
433
|
|
|
383
|
-
print self.get_html_string(start, end, fields, header,
|
|
434
|
+
print self.get_html_string(start, end, fields, sortby, reversesort, format, header, border, hrules, attributes)
|
|
384
435
|
|
|
385
|
-
def get_html_string(self, start=0, end=None, fields=None, header=True,
|
|
436
|
+
def get_html_string(self, start=0, end=None, fields=None, sortby=None, reversesort=False, format=True, header=True, border=True, hrules=FRAME, attributes=None):
|
|
386
437
|
|
|
387
438
|
"""Return string representation of HTML formatted version of table in current state.
|
|
388
|
-
|
|
439
|
+
|
|
389
440
|
Arguments:
|
|
390
|
-
|
|
441
|
+
|
|
391
442
|
start - index of first data row to include in output
|
|
392
443
|
end - index of last data row to include in output PLUS ONE (list slice style)
|
|
393
444
|
fields - names of fields (columns) to include
|
|
445
|
+
sortby - name of
|
|
394
446
|
border - should be True or False to print or not print borders
|
|
395
|
-
|
|
447
|
+
format - should be True or False to attempt to format alignmet, padding, etc. or not
|
|
448
|
+
header - should be True or False to print a header showing field names or not
|
|
449
|
+
border - should be True or False to print or not print borders
|
|
450
|
+
hrules - include horizontal rule after each row
|
|
451
|
+
attributes - dictionary of name/value pairs to include as HTML attributes in the <table> tag"""
|
|
396
452
|
|
|
397
453
|
if self.caching:
|
|
398
|
-
key = cPickle.dumps((start, end, fields, header, sortby, reversesort))
|
|
454
|
+
key = cPickle.dumps((start, end, fields, format, header, border, hrules, sortby, reversesort, attributes))
|
|
399
455
|
if key in self.html_cache:
|
|
400
456
|
return self.html_cache[key]
|
|
401
457
|
|
|
458
|
+
if format:
|
|
459
|
+
tmp_html_func=self._get_formatted_html_string
|
|
460
|
+
else:
|
|
461
|
+
tmp_html_func=self._get_simple_html_string
|
|
462
|
+
string = tmp_html_func(start, end, fields, sortby, reversesort, header, border, hrules, attributes)
|
|
463
|
+
|
|
464
|
+
if self.caching:
|
|
465
|
+
self.html_cache[key] = string
|
|
466
|
+
|
|
467
|
+
return string
|
|
468
|
+
|
|
469
|
+
def _get_simple_html_string(self, start, end, fields, sortby, reversesort, header, border, hrules, attributes):
|
|
470
|
+
|
|
402
471
|
bits = []
|
|
403
|
-
|
|
472
|
+
# Slow but works
|
|
473
|
+
table_tag = '<table'
|
|
474
|
+
if border:
|
|
475
|
+
table_tag += ' border="1"'
|
|
476
|
+
if attributes:
|
|
477
|
+
for attr_name in attributes:
|
|
478
|
+
table_tag += ' %s="%s"' % (attr_name, attributes[attr_name])
|
|
479
|
+
table_tag += '>'
|
|
480
|
+
bits.append(table_tag)
|
|
404
481
|
# Headers
|
|
482
|
+
bits.append(" <tr>")
|
|
483
|
+
for field in self.fields:
|
|
484
|
+
if fields and field not in fields:
|
|
485
|
+
continue
|
|
486
|
+
bits.append(" <th>%s</th>" % cgi.escape(unicode(field)))
|
|
487
|
+
bits.append(" </tr>")
|
|
488
|
+
# Data
|
|
489
|
+
if sortby:
|
|
490
|
+
rows = self._get_sorted_rows(stard, end, sortby, reversesort)
|
|
491
|
+
else:
|
|
492
|
+
rows = self.rows
|
|
493
|
+
for row in self.rows:
|
|
494
|
+
bits.append(" <tr>")
|
|
495
|
+
for field, datum in zip(self.fields, row):
|
|
496
|
+
if fields and field not in fields:
|
|
497
|
+
continue
|
|
498
|
+
bits.append(" <td>%s</td>" % cgi.escape(unicode(datum)))
|
|
499
|
+
bits.append(" </tr>")
|
|
500
|
+
bits.append("</table>")
|
|
501
|
+
string = "\n".join(bits)
|
|
502
|
+
|
|
503
|
+
return string
|
|
504
|
+
|
|
505
|
+
def _get_formatted_html_string(self, start, end, fields, sortby, reversesort, header, border, hrules, attributes):
|
|
506
|
+
|
|
507
|
+
bits = []
|
|
508
|
+
# Slow but works
|
|
509
|
+
table_tag = '<table'
|
|
510
|
+
if border:
|
|
511
|
+
table_tag += ' border="1"'
|
|
512
|
+
if hrules == NONE:
|
|
513
|
+
table_tag += ' frame="vsides" rules="cols"'
|
|
514
|
+
if attributes:
|
|
515
|
+
for attr_name in attributes:
|
|
516
|
+
table_tag += ' %s="%s"' % (attr_name, attributes[attr_name])
|
|
517
|
+
table_tag += '>'
|
|
518
|
+
bits.append(table_tag)
|
|
519
|
+
# Headers
|
|
520
|
+
lpad, rpad = self._get_paddings()
|
|
405
521
|
if header:
|
|
406
522
|
bits.append(" <tr>")
|
|
407
523
|
for field in self.fields:
|
|
408
524
|
if fields and field not in fields:
|
|
409
525
|
continue
|
|
410
|
-
bits.append(" <th>%s</th>" % cgi.escape(field))
|
|
526
|
+
bits.append(" <th style=\"padding-left: %dem; padding-right: %dem; text-align: center\">%s</th>" % (lpad, rpad, cgi.escape(unicode(field))))
|
|
411
527
|
bits.append(" </tr>")
|
|
412
528
|
# Data
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
529
|
+
if sortby:
|
|
530
|
+
rows = self._get_sorted_rows(start, end, sortby, reversesort)
|
|
531
|
+
else:
|
|
532
|
+
rows = self.rows
|
|
417
533
|
for row in self.rows:
|
|
418
534
|
bits.append(" <tr>")
|
|
419
|
-
for field, datum in zip(self.fields, row):
|
|
420
|
-
if fields and field in fields:
|
|
535
|
+
for field, align, datum in zip(self.fields, self.aligns, row):
|
|
536
|
+
if fields and field not in fields:
|
|
421
537
|
continue
|
|
422
|
-
|
|
538
|
+
if align == "l":
|
|
539
|
+
bits.append(" <td style=\"padding-left: %dem; padding-right: %dem; text-align: left\">%s</td>" % (lpad, rpad, cgi.escape(unicode(datum))))
|
|
540
|
+
elif align == "r":
|
|
541
|
+
bits.append(" <td style=\"padding-left: %dem; padding-right: %dem; text-align: right\">%s</td>" % (lpad, rpad, cgi.escape(unicode(datum))))
|
|
542
|
+
else:
|
|
543
|
+
bits.append(" <td style=\"padding-left: %dem; padding-right: %dem; text-align: center\">%s</td>" % (lpad, rpad, cgi.escape(unicode(datum))))
|
|
423
544
|
bits.append(" </tr>")
|
|
424
545
|
bits.append("</table>")
|
|
425
546
|
string = "\n".join(bits)
|
|
426
547
|
|
|
427
|
-
if self.caching:
|
|
428
|
-
self.html_cache[key] = string
|
|
429
|
-
|
|
430
548
|
return string
|
|
431
549
|
|
|
432
550
|
def main():
|
|
@@ -444,22 +562,23 @@ def main():
|
|
|
444
562
|
|
|
445
563
|
if len(sys.argv) > 1 and sys.argv[1] == "test":
|
|
446
564
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
565
|
+
# This "test suite" is hideous and provides poor, arbitrary coverage.
|
|
566
|
+
# I'll replace it with some proper unit tests Sometime Soon (TM).
|
|
567
|
+
# Promise.
|
|
450
568
|
print "Testing field subset selection:"
|
|
451
569
|
x.printt(fields=["City name","Population"])
|
|
452
570
|
print "Testing row subset selection:"
|
|
453
571
|
x.printt(start=2, end=5)
|
|
454
572
|
print "Testing hrules settings:"
|
|
455
|
-
|
|
573
|
+
print "FRAME:"
|
|
456
574
|
x.printt(hrules=FRAME)
|
|
457
|
-
|
|
575
|
+
print "ALL:"
|
|
458
576
|
x.printt(hrules=ALL)
|
|
459
|
-
|
|
577
|
+
print "NONE:"
|
|
460
578
|
x.printt(hrules=NONE)
|
|
461
579
|
print "Testing lack of headers:"
|
|
462
580
|
x.printt(header=False)
|
|
581
|
+
x.printt(header=False, border=False)
|
|
463
582
|
print "Testing lack of borders:"
|
|
464
583
|
x.printt(border=False)
|
|
465
584
|
print "Testing sorting:"
|
|
@@ -480,11 +599,11 @@ def main():
|
|
|
480
599
|
x.set_right_padding(None)
|
|
481
600
|
x.set_padding_width(2)
|
|
482
601
|
print "Testing changing characters"
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
602
|
+
x.set_border_chars("*","*","*")
|
|
603
|
+
x.printt()
|
|
604
|
+
x.set_border_chars("!","~","o")
|
|
605
|
+
x.printt()
|
|
606
|
+
x.set_border_chars("|","-","+")
|
|
488
607
|
print "Testing everything at once:"
|
|
489
608
|
x.printt(start=2, end=5, fields=["City name","Population"], border=False, hrules=True)
|
|
490
609
|
print "Rebuilding by columns:"
|
|
@@ -496,7 +615,11 @@ def main():
|
|
|
496
615
|
x.printt()
|
|
497
616
|
print "Testing HTML:"
|
|
498
617
|
x.print_html()
|
|
499
|
-
x.print_html(
|
|
618
|
+
x.print_html(border=False)
|
|
619
|
+
x.print_html(border=True)
|
|
620
|
+
x.print_html(format=False)
|
|
621
|
+
x.print_html(attributes={"name": "table", "id": "table"})
|
|
500
622
|
|
|
501
623
|
if __name__ == "__main__":
|
|
502
624
|
main()
|
|
625
|
+
|
|
@@ -3,7 +3,7 @@ from setuptools import setup
|
|
|
3
3
|
|
|
4
4
|
setup(
|
|
5
5
|
name='prettytable',
|
|
6
|
-
version='0.
|
|
6
|
+
version='0.5',
|
|
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',
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|