prettytable 0.3__tar.gz
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.
- prettytable-0.3/PKG-INFO +10 -0
- prettytable-0.3/prettytable.egg-info/PKG-INFO +10 -0
- prettytable-0.3/prettytable.egg-info/SOURCES.txt +6 -0
- prettytable-0.3/prettytable.egg-info/dependency_links.txt +1 -0
- prettytable-0.3/prettytable.egg-info/top_level.txt +1 -0
- prettytable-0.3/prettytable.py +263 -0
- prettytable-0.3/setup.cfg +5 -0
- prettytable-0.3/setup.py +12 -0
prettytable-0.3/PKG-INFO
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: prettytable
|
|
3
|
+
Version: 0.3
|
|
4
|
+
Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
|
|
5
|
+
Home-page: http://www.luke.maurits.id.au/software/prettytable
|
|
6
|
+
Author: Luke Maurits
|
|
7
|
+
Author-email: luke@maurits.id.au
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: prettytable
|
|
3
|
+
Version: 0.3
|
|
4
|
+
Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
|
|
5
|
+
Home-page: http://www.luke.maurits.id.au/software/prettytable
|
|
6
|
+
Author: Luke Maurits
|
|
7
|
+
Author-email: luke@maurits.id.au
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Description: UNKNOWN
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prettytable
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# PrettyTable 0.3
|
|
2
|
+
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
3
|
+
# All rights reserved.
|
|
4
|
+
# With contributions from:
|
|
5
|
+
# * Chris Clark
|
|
6
|
+
#
|
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
|
9
|
+
#
|
|
10
|
+
# * Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
# this list of conditions and the following disclaimer.
|
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
# and/or other materials provided with the distribution.
|
|
15
|
+
# * The name of the author may not be used to endorse or promote products
|
|
16
|
+
# derived from this software without specific prior written permission.
|
|
17
|
+
#
|
|
18
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
22
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
|
|
30
|
+
import copy
|
|
31
|
+
import cPickle
|
|
32
|
+
import sys
|
|
33
|
+
|
|
34
|
+
class PrettyTable:
|
|
35
|
+
|
|
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))
|
|
126
|
+
|
|
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)
|
|
232
|
+
|
|
233
|
+
def main():
|
|
234
|
+
|
|
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)
|
|
261
|
+
|
|
262
|
+
if __name__ == "__main__":
|
|
263
|
+
main()
|
prettytable-0.3/setup.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
from setuptools import setup
|
|
3
|
+
|
|
4
|
+
setup(
|
|
5
|
+
name='prettytable',
|
|
6
|
+
version='0.3',
|
|
7
|
+
description='A simple Python library for easily displaying tabular data in a visually appealing ASCII table format',
|
|
8
|
+
author='Luke Maurits',
|
|
9
|
+
author_email='luke@maurits.id.au',
|
|
10
|
+
url='http://www.luke.maurits.id.au/software/prettytable',
|
|
11
|
+
py_modules=['prettytable']
|
|
12
|
+
)
|