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