prettytable 0.1__py2.6.egg → 0.2.1__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 +91 -29
- 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.1
|
|
3
|
+
Version: 0.2.1
|
|
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,3 +1,4 @@
|
|
|
1
|
+
# PrettyTable 0.2.1
|
|
1
2
|
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
2
3
|
# All rights reserved.
|
|
3
4
|
#
|
|
@@ -25,17 +26,33 @@
|
|
|
25
26
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
26
27
|
|
|
27
28
|
import copy
|
|
29
|
+
import cPickle
|
|
30
|
+
import sys
|
|
28
31
|
|
|
29
32
|
class PrettyTable:
|
|
30
33
|
|
|
31
|
-
def __init__(self):
|
|
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"""
|
|
32
42
|
|
|
33
|
-
|
|
43
|
+
# Data
|
|
44
|
+
if fields:
|
|
45
|
+
self.set_field_names(fields)
|
|
46
|
+
else:
|
|
47
|
+
self.fields = []
|
|
48
|
+
self.widths = []
|
|
49
|
+
self.aligns = []
|
|
34
50
|
self.rows = []
|
|
35
|
-
self.
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
self.cache = {}
|
|
52
|
+
|
|
53
|
+
# Options
|
|
38
54
|
self.hrules = False
|
|
55
|
+
self.caching = caching
|
|
39
56
|
|
|
40
57
|
def __getslice__(self, i, j):
|
|
41
58
|
|
|
@@ -50,11 +67,22 @@ class PrettyTable:
|
|
|
50
67
|
newtable.rows = self.rows[i:j]
|
|
51
68
|
return newtable
|
|
52
69
|
|
|
70
|
+
def __str__(self):
|
|
71
|
+
|
|
72
|
+
return self.get_string()
|
|
73
|
+
|
|
53
74
|
def set_field_names(self, fields):
|
|
54
75
|
|
|
76
|
+
"""Set the alignment of a field by its fieldname
|
|
77
|
+
|
|
78
|
+
Arguments:
|
|
79
|
+
|
|
80
|
+
fields - list or tuple of field names"""
|
|
81
|
+
|
|
55
82
|
self.fields = fields
|
|
56
83
|
self.widths = [len(field) for field in fields]
|
|
57
84
|
self.aligns = len(fields)*["c"]
|
|
85
|
+
self.cache = {}
|
|
58
86
|
|
|
59
87
|
def set_field_align(self, fieldname, align):
|
|
60
88
|
|
|
@@ -70,6 +98,7 @@ class PrettyTable:
|
|
|
70
98
|
if align not in ["l","c","r"]:
|
|
71
99
|
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
72
100
|
self.aligns[self.fields.index(fieldname)] = align
|
|
101
|
+
self.cache = {}
|
|
73
102
|
|
|
74
103
|
def add_row(self, row):
|
|
75
104
|
|
|
@@ -86,50 +115,73 @@ class PrettyTable:
|
|
|
86
115
|
for i in range(0,len(row)):
|
|
87
116
|
if len(str(row[i])) > self.widths[i]:
|
|
88
117
|
self.widths[i] = len(str(row[i]))
|
|
118
|
+
self.cache = {}
|
|
89
119
|
|
|
90
|
-
def
|
|
120
|
+
def get_string(self, start=0, end=None, fields=None, hrules=None):
|
|
91
121
|
|
|
92
|
-
"""
|
|
122
|
+
"""Return string representation of table in current state.
|
|
93
123
|
|
|
94
124
|
Arguments:
|
|
95
125
|
|
|
96
126
|
start - index of first data row to include in output
|
|
97
127
|
end - index of last data row to include in output PLUS ONE (list slice style)"""
|
|
98
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 = []
|
|
99
135
|
if not self.fields:
|
|
100
|
-
return
|
|
101
|
-
self.
|
|
102
|
-
|
|
103
|
-
hrule = hrules
|
|
104
|
-
else:
|
|
105
|
-
hrule = self.hrules
|
|
136
|
+
return ""
|
|
137
|
+
bits.append(self._stringify_header(fields))
|
|
138
|
+
hrule = hrules or self.hrules
|
|
106
139
|
for row in self.rows[start:end]:
|
|
107
|
-
self.
|
|
108
|
-
if not
|
|
109
|
-
self.
|
|
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
|
|
110
149
|
|
|
111
|
-
def
|
|
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):
|
|
112
162
|
bits = ["+"]
|
|
113
163
|
for field, width in zip(self.fields, self.widths):
|
|
114
164
|
if fields and field not in fields:
|
|
115
165
|
continue
|
|
116
166
|
bits.append((width+2)*"-")
|
|
117
167
|
bits.append("+")
|
|
118
|
-
|
|
168
|
+
return "".join(bits)
|
|
119
169
|
|
|
120
|
-
def
|
|
170
|
+
def _stringify_header(self, fields=None):
|
|
121
171
|
|
|
122
|
-
self.
|
|
123
|
-
bits
|
|
172
|
+
bits = [self._stringify_hrule(fields)]
|
|
173
|
+
bits.append("\n")
|
|
174
|
+
bits.append("|")
|
|
124
175
|
for field, width in zip(self.fields, self.widths):
|
|
125
176
|
if fields and field not in fields:
|
|
126
177
|
continue
|
|
127
178
|
bits.append(field.center(width+2))
|
|
128
179
|
bits.append("|")
|
|
129
|
-
|
|
130
|
-
self.
|
|
180
|
+
bits.append("\n")
|
|
181
|
+
bits.append(self._stringify_hrule(fields))
|
|
182
|
+
return "".join(bits)
|
|
131
183
|
|
|
132
|
-
def
|
|
184
|
+
def _stringify_row(self, row, fields, hrule):
|
|
133
185
|
bits = ["|"]
|
|
134
186
|
for field, value, width, align in zip(self.fields, row, self.widths, self.aligns):
|
|
135
187
|
if fields and field not in fields:
|
|
@@ -141,14 +193,14 @@ class PrettyTable:
|
|
|
141
193
|
else:
|
|
142
194
|
bits.append(str(value).center(width+2))
|
|
143
195
|
bits.append("|")
|
|
144
|
-
print "".join(bits)
|
|
145
196
|
if hrule:
|
|
146
|
-
|
|
197
|
+
bits.append("\n")
|
|
198
|
+
bits.append(self._stringify_hrule(fields))
|
|
199
|
+
return "".join(bits)
|
|
147
200
|
|
|
148
201
|
def main():
|
|
149
202
|
|
|
150
|
-
x = PrettyTable()
|
|
151
|
-
x.set_field_names(["City name", "Area", "Population", "Annual Rainfall"])
|
|
203
|
+
x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
|
|
152
204
|
x.set_field_align("City name", "l") # Left align city names
|
|
153
205
|
x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
154
206
|
x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
@@ -157,7 +209,17 @@ def main():
|
|
|
157
209
|
x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
158
210
|
x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
159
211
|
x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
160
|
-
x
|
|
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)
|
|
161
223
|
|
|
162
224
|
if __name__ == "__main__":
|
|
163
225
|
main()
|
prettytable.pyc
CHANGED
|
Binary file
|