prettytable 0.2__py2.6.egg → 0.3__py2.6.egg
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.
- EGG-INFO/PKG-INFO +1 -1
- prettytable.py +227 -177
- prettytable.pyc +0 -0
EGG-INFO/PKG-INFO
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 1.0
|
|
2
2
|
Name: prettytable
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3
|
|
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
|
prettytable.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
# PrettyTable 0.
|
|
1
|
+
# PrettyTable 0.3
|
|
2
2
|
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
3
3
|
# All rights reserved.
|
|
4
|
+
# With contributions from:
|
|
5
|
+
# * Chris Clark
|
|
4
6
|
#
|
|
5
7
|
# Redistribution and use in source and binary forms, with or without
|
|
6
8
|
# modification, are permitted provided that the following conditions are met:
|
|
@@ -26,188 +28,236 @@
|
|
|
26
28
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
27
29
|
|
|
28
30
|
import copy
|
|
31
|
+
import cPickle
|
|
32
|
+
import sys
|
|
29
33
|
|
|
30
34
|
class PrettyTable:
|
|
31
35
|
|
|
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
|
-
|
|
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))
|
|
71
126
|
|
|
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
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
bits.append(field.center(width+2))
|
|
178
|
-
bits.append("|")
|
|
179
|
-
bits.append("\n")
|
|
180
|
-
bits.append(self._stringify_hrule())
|
|
181
|
-
return "".join(bits)
|
|
182
|
-
|
|
183
|
-
def _stringify_row(self, row, fields, hrule):
|
|
184
|
-
bits = ["|"]
|
|
185
|
-
for field, value, width, align in zip(self.fields, row, self.widths, self.aligns):
|
|
186
|
-
if fields and field not in fields:
|
|
187
|
-
continue
|
|
188
|
-
if align == "l":
|
|
189
|
-
bits.append(str(value).ljust(width).center(width+2))
|
|
190
|
-
elif align == "r":
|
|
191
|
-
bits.append(str(value).rjust(width).center(width+2))
|
|
192
|
-
else:
|
|
193
|
-
bits.append(str(value).center(width+2))
|
|
194
|
-
bits.append("|")
|
|
195
|
-
if hrule:
|
|
196
|
-
bits.append(self._stringify_hrule())
|
|
197
|
-
return "".join(bits)
|
|
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)
|
|
198
232
|
|
|
199
233
|
def main():
|
|
200
234
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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)
|
|
211
261
|
|
|
212
262
|
if __name__ == "__main__":
|
|
213
|
-
|
|
263
|
+
main()
|
prettytable.pyc
CHANGED
|
Binary file
|