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.
Files changed (3) hide show
  1. EGG-INFO/PKG-INFO +1 -1
  2. prettytable.py +227 -177
  3. 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.2
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.2
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
- def __init__(self, fields=None, caching=True):
33
-
34
- """Return a new PrettyTable instance
35
-
36
- Arguments:
37
-
38
- fields - list or tuple of field names
39
- caching - boolean value to turn string caching on/off"""
40
-
41
- # Data
42
- if fields:
43
- self.set_field_names(fields)
44
- else:
45
- self.fields = []
46
- self.widths = []
47
- self.aligns = []
48
- self.rows = []
49
- self.cache = {}
50
-
51
- # Options
52
- self.hrules = False
53
- self.caching = caching
54
-
55
- def __getslice__(self, i, j):
56
-
57
- """Return a new PrettyTable whose data rows are a slice of this one's
58
-
59
- Arguments:
60
-
61
- i - beginning slice index
62
- j - ending slice index"""
63
-
64
- newtable = copy.deepcopy(self)
65
- newtable.rows = self.rows[i:j]
66
- return newtable
67
-
68
- def __str__(self):
69
-
70
- return self.get_string()
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
- def set_field_names(self, fields):
73
-
74
- """Set the alignment of a field by its fieldname
75
-
76
- Arguments:
77
-
78
- fields - list or tuple of field names"""
79
-
80
- self.fields = fields
81
- self.widths = [len(field) for field in fields]
82
- self.aligns = len(fields)*["c"]
83
- self.cache = {}
84
-
85
- def set_field_align(self, fieldname, align):
86
-
87
- """Set the alignment of a field by its fieldname
88
-
89
- Arguments:
90
-
91
- fieldname - name of the field whose alignment is to be changed
92
- align - desired alignment - "l" for left, "c" for centre and "r" for right"""
93
-
94
- if fieldname not in self.fields:
95
- raise Exception("No field %s exists!" % fieldname)
96
- if align not in ["l","c","r"]:
97
- raise Exception("Alignment %s is invalid, use l, c or r!" % align)
98
- self.aligns[self.fields.index(fieldname)] = align
99
- self.cache = {}
100
-
101
- def add_row(self, row):
102
-
103
- """Add a row to the table
104
-
105
- Arguments:
106
-
107
- row - row of data, should be a list with as many elements as the table
108
- has fields"""
109
-
110
- if len(row) != len(self.fields):
111
- raise Exception("Row has incorrect number of values")
112
- self.rows.append(row)
113
- for i in range(0,len(row)):
114
- if len(str(row[i])) > self.widths[i]:
115
- self.widths[i] = len(str(row[i]))
116
- self.cache = {}
117
-
118
- def get_string(self, start=0, end=None, fields=None, hrules=None):
119
-
120
- """Return string representation of table in current state.
121
-
122
- Arguments:
123
-
124
- start - index of first data row to include in output
125
- end - index of last data row to include in output PLUS ONE (list slice style)"""
126
-
127
- if self.caching:
128
- key = (start, end, fields, hrules)
129
- if key in self.cache:
130
- print "Using cache!"
131
- return self.cache[key]
132
-
133
- bits = []
134
- if not self.fields:
135
- return ""
136
- bits.append(self._stringify_header(fields))
137
- hrule = hrules or self.hrules
138
- for row in self.rows[start:end]:
139
- bits.append(self._stringify_row(row, fields, hrule))
140
- if not hrule:
141
- bits.append(self._stringify_hrule())
142
- string = "\n".join(bits)
143
-
144
- if self.caching:
145
- self.cache[key] = string
146
-
147
- return string
148
-
149
- def printt(self, start=0, end=None, fields=None, hrules=None):
150
-
151
- """Print table in current state to stdout.
152
-
153
- Arguments:
154
-
155
- start - index of first data row to include in output
156
- end - index of last data row to include in output PLUS ONE (list slice style)"""
157
-
158
- print self.get_string(start, end, fields, hrules)
159
-
160
- def _stringify_hrule(self, fields=None):
161
- bits = ["+"]
162
- for field, width in zip(self.fields, self.widths):
163
- if fields and field not in fields:
164
- continue
165
- bits.append((width+2)*"-")
166
- bits.append("+")
167
- return "".join(bits)
168
-
169
- def _stringify_header(self, fields=None):
170
-
171
- bits = [self._stringify_hrule()]
172
- bits.append("\n")
173
- bits.append("|")
174
- for field, width in zip(self.fields, self.widths):
175
- if fields and field not in fields:
176
- continue
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
- x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
202
- x.set_field_align("City name", "l") # Left align city names
203
- x.add_row(["Adelaide",1295, 1158259, 600.5])
204
- x.add_row(["Brisbane",5905, 1857594, 1146.4])
205
- x.add_row(["Darwin", 112, 120900, 1714.7])
206
- x.add_row(["Hobart", 1357, 205556, 619.5])
207
- x.add_row(["Sydney", 2058, 4336374, 1214.8])
208
- x.add_row(["Melbourne", 1566, 3806092, 646.9])
209
- x.add_row(["Perth", 5386, 1554769, 869.4])
210
- print x
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
- main()
263
+ main()
prettytable.pyc CHANGED
Binary file