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