prettytable 0.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 +10 -0
- EGG-INFO/SOURCES.txt +6 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/top_level.txt +1 -0
- EGG-INFO/zip-safe +1 -0
- prettytable.py +163 -0
- prettytable.pyc +0 -0
EGG-INFO/PKG-INFO
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: prettytable
|
|
3
|
+
Version: 0.1
|
|
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
|
EGG-INFO/SOURCES.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
EGG-INFO/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prettytable
|
EGG-INFO/zip-safe
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
prettytable.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Copyright (c) 2009, Luke Maurits <luke@maurits.id.au>
|
|
2
|
+
# All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
|
5
|
+
# modification, are permitted provided that the following conditions are met:
|
|
6
|
+
#
|
|
7
|
+
# * Redistributions of source code must retain the above copyright notice,
|
|
8
|
+
# this list of conditions and the following disclaimer.
|
|
9
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
11
|
+
# and/or other materials provided with the distribution.
|
|
12
|
+
# * The name of the author may not be used to endorse or promote products
|
|
13
|
+
# derived from this software without specific prior written permission.
|
|
14
|
+
#
|
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
16
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
17
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
18
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
19
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
20
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
21
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
22
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
23
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
24
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
25
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
26
|
+
|
|
27
|
+
import copy
|
|
28
|
+
|
|
29
|
+
class PrettyTable:
|
|
30
|
+
|
|
31
|
+
def __init__(self):
|
|
32
|
+
|
|
33
|
+
self.fields = []
|
|
34
|
+
self.rows = []
|
|
35
|
+
self.widths = []
|
|
36
|
+
self.aligns = []
|
|
37
|
+
|
|
38
|
+
self.hrules = False
|
|
39
|
+
|
|
40
|
+
def __getslice__(self, i, j):
|
|
41
|
+
|
|
42
|
+
"""Return a new PrettyTable whose data rows are a slice of this one's
|
|
43
|
+
|
|
44
|
+
Arguments:
|
|
45
|
+
|
|
46
|
+
i - beginning slice index
|
|
47
|
+
j - ending slice index"""
|
|
48
|
+
|
|
49
|
+
newtable = copy.deepcopy(self)
|
|
50
|
+
newtable.rows = self.rows[i:j]
|
|
51
|
+
return newtable
|
|
52
|
+
|
|
53
|
+
def set_field_names(self, fields):
|
|
54
|
+
|
|
55
|
+
self.fields = fields
|
|
56
|
+
self.widths = [len(field) for field in fields]
|
|
57
|
+
self.aligns = len(fields)*["c"]
|
|
58
|
+
|
|
59
|
+
def set_field_align(self, fieldname, align):
|
|
60
|
+
|
|
61
|
+
"""Set the alignment of a field by its fieldname
|
|
62
|
+
|
|
63
|
+
Arguments:
|
|
64
|
+
|
|
65
|
+
fieldname - name of the field whose alignment is to be changed
|
|
66
|
+
align - desired alignment - "l" for left, "c" for centre and "r" for right"""
|
|
67
|
+
|
|
68
|
+
if fieldname not in self.fields:
|
|
69
|
+
raise Exception("No field %s exists!" % fieldname)
|
|
70
|
+
if align not in ["l","c","r"]:
|
|
71
|
+
raise Exception("Alignment %s is invalid, use l, c or r!" % align)
|
|
72
|
+
self.aligns[self.fields.index(fieldname)] = align
|
|
73
|
+
|
|
74
|
+
def add_row(self, row):
|
|
75
|
+
|
|
76
|
+
"""Add a row to the table
|
|
77
|
+
|
|
78
|
+
Arguments:
|
|
79
|
+
|
|
80
|
+
row - row of data, should be a list with as many elements as the table
|
|
81
|
+
has fields"""
|
|
82
|
+
|
|
83
|
+
if len(row) != len(self.fields):
|
|
84
|
+
raise Exception("Row has incorrect number of values")
|
|
85
|
+
self.rows.append(row)
|
|
86
|
+
for i in range(0,len(row)):
|
|
87
|
+
if len(str(row[i])) > self.widths[i]:
|
|
88
|
+
self.widths[i] = len(str(row[i]))
|
|
89
|
+
|
|
90
|
+
def printt(self, start=0, end=None, fields=None, hrules=None):
|
|
91
|
+
|
|
92
|
+
"""Print table in current state to stdout.
|
|
93
|
+
|
|
94
|
+
Arguments:
|
|
95
|
+
|
|
96
|
+
start - index of first data row to include in output
|
|
97
|
+
end - index of last data row to include in output PLUS ONE (list slice style)"""
|
|
98
|
+
|
|
99
|
+
if not self.fields:
|
|
100
|
+
return
|
|
101
|
+
self._print_header(fields)
|
|
102
|
+
if hrules:
|
|
103
|
+
hrule = hrules
|
|
104
|
+
else:
|
|
105
|
+
hrule = self.hrules
|
|
106
|
+
for row in self.rows[start:end]:
|
|
107
|
+
self._print_row(row, fields, hrule)
|
|
108
|
+
if not self.hrules:
|
|
109
|
+
self._print_hrule()
|
|
110
|
+
|
|
111
|
+
def _print_hrule(self, fields=None):
|
|
112
|
+
bits = ["+"]
|
|
113
|
+
for field, width in zip(self.fields, self.widths):
|
|
114
|
+
if fields and field not in fields:
|
|
115
|
+
continue
|
|
116
|
+
bits.append((width+2)*"-")
|
|
117
|
+
bits.append("+")
|
|
118
|
+
print "".join(bits)
|
|
119
|
+
|
|
120
|
+
def _print_header(self, fields=None):
|
|
121
|
+
|
|
122
|
+
self._print_hrule()
|
|
123
|
+
bits = ["|"]
|
|
124
|
+
for field, width in zip(self.fields, self.widths):
|
|
125
|
+
if fields and field not in fields:
|
|
126
|
+
continue
|
|
127
|
+
bits.append(field.center(width+2))
|
|
128
|
+
bits.append("|")
|
|
129
|
+
print "".join(bits)
|
|
130
|
+
self._print_hrule()
|
|
131
|
+
|
|
132
|
+
def _print_row(self, row, fields, hrule):
|
|
133
|
+
bits = ["|"]
|
|
134
|
+
for field, value, width, align in zip(self.fields, row, self.widths, self.aligns):
|
|
135
|
+
if fields and field not in fields:
|
|
136
|
+
continue
|
|
137
|
+
if align == "l":
|
|
138
|
+
bits.append(str(value).ljust(width).center(width+2))
|
|
139
|
+
elif align == "r":
|
|
140
|
+
bits.append(str(value).rjust(width).center(width+2))
|
|
141
|
+
else:
|
|
142
|
+
bits.append(str(value).center(width+2))
|
|
143
|
+
bits.append("|")
|
|
144
|
+
print "".join(bits)
|
|
145
|
+
if hrule:
|
|
146
|
+
self._print_hrule()
|
|
147
|
+
|
|
148
|
+
def main():
|
|
149
|
+
|
|
150
|
+
x = PrettyTable()
|
|
151
|
+
x.set_field_names(["City name", "Area", "Population", "Annual Rainfall"])
|
|
152
|
+
x.set_field_align("City name", "l") # Left align city names
|
|
153
|
+
x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
154
|
+
x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
155
|
+
x.add_row(["Darwin", 112, 120900, 1714.7])
|
|
156
|
+
x.add_row(["Hobart", 1357, 205556, 619.5])
|
|
157
|
+
x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
158
|
+
x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
159
|
+
x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
160
|
+
x.printt()
|
|
161
|
+
|
|
162
|
+
if __name__ == "__main__":
|
|
163
|
+
main()
|
prettytable.pyc
ADDED
|
Binary file
|