prettytable 0.6__zip → 0.7.1__zip
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.7.1/CHANGELOG +142 -0
- prettytable-0.7.1/COPYING +30 -0
- prettytable-0.7.1/MANIFEST.in +4 -0
- {prettytable-0.6 → prettytable-0.7.1}/PKG-INFO +5 -3
- {prettytable-0.6 → prettytable-0.7.1}/README +48 -12
- {prettytable-0.6 → prettytable-0.7.1}/prettytable.egg-info/PKG-INFO +5 -3
- {prettytable-0.6 → prettytable-0.7.1}/prettytable.egg-info/SOURCES.txt +4 -0
- {prettytable-0.6 → prettytable-0.7.1}/prettytable.py +562 -182
- prettytable-0.7.1/prettytable_test.py +569 -0
- {prettytable-0.6 → prettytable-0.7.1}/setup.py +5 -1
- {prettytable-0.6 → prettytable-0.7.1}/prettytable.egg-info/dependency_links.txt +0 -0
- {prettytable-0.6 → prettytable-0.7.1}/prettytable.egg-info/top_level.txt +0 -0
- {prettytable-0.6 → prettytable-0.7.1}/setup.cfg +0 -0
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
# coding=UTF-8
|
|
2
|
+
|
|
3
|
+
from prettytable import *
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
py3k = sys.version_info[0] >= 3
|
|
7
|
+
try:
|
|
8
|
+
import sqlite3
|
|
9
|
+
_have_sqlite = True
|
|
10
|
+
except ImportError:
|
|
11
|
+
_have_sqlite = False
|
|
12
|
+
if py3k:
|
|
13
|
+
import io as StringIO
|
|
14
|
+
else:
|
|
15
|
+
import StringIO
|
|
16
|
+
from math import pi, e, sqrt
|
|
17
|
+
import unittest
|
|
18
|
+
|
|
19
|
+
class BuildEquivelanceTest(unittest.TestCase):
|
|
20
|
+
|
|
21
|
+
"""Make sure that building a table row-by-row and column-by-column yield the same results"""
|
|
22
|
+
|
|
23
|
+
def setUp(self):
|
|
24
|
+
|
|
25
|
+
# Row by row...
|
|
26
|
+
self.row = PrettyTable()
|
|
27
|
+
self.row.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
|
|
28
|
+
self.row.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
29
|
+
self.row.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
30
|
+
self.row.add_row(["Darwin", 112, 120900, 1714.7])
|
|
31
|
+
self.row.add_row(["Hobart", 1357, 205556, 619.5])
|
|
32
|
+
self.row.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
33
|
+
self.row.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
34
|
+
self.row.add_row(["Perth", 5386, 1554769, 869.4])
|
|
35
|
+
|
|
36
|
+
# Column by column...
|
|
37
|
+
self.col = PrettyTable()
|
|
38
|
+
self.col.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
|
|
39
|
+
self.col.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
|
|
40
|
+
self.col.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
|
|
41
|
+
self.col.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
|
|
42
|
+
|
|
43
|
+
# A mix of both!
|
|
44
|
+
self.mix = PrettyTable()
|
|
45
|
+
self.mix.field_names = ["City name", "Area"]
|
|
46
|
+
self.mix.add_row(["Adelaide",1295])
|
|
47
|
+
self.mix.add_row(["Brisbane",5905])
|
|
48
|
+
self.mix.add_row(["Darwin", 112])
|
|
49
|
+
self.mix.add_row(["Hobart", 1357])
|
|
50
|
+
self.mix.add_row(["Sydney", 2058])
|
|
51
|
+
self.mix.add_row(["Melbourne", 1566])
|
|
52
|
+
self.mix.add_row(["Perth", 5386])
|
|
53
|
+
self.mix.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769])
|
|
54
|
+
self.mix.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
|
|
55
|
+
|
|
56
|
+
def testRowColEquivalenceASCII(self):
|
|
57
|
+
|
|
58
|
+
self.assertEqual(self.row.get_string(), self.col.get_string())
|
|
59
|
+
|
|
60
|
+
def testRowMixEquivalenceASCII(self):
|
|
61
|
+
|
|
62
|
+
self.assertEqual(self.row.get_string(), self.mix.get_string())
|
|
63
|
+
|
|
64
|
+
def testRowColEquivalenceHTML(self):
|
|
65
|
+
|
|
66
|
+
self.assertEqual(self.row.get_html_string(), self.col.get_html_string())
|
|
67
|
+
|
|
68
|
+
def testRowMixEquivalenceHTML(self):
|
|
69
|
+
|
|
70
|
+
self.assertEqual(self.row.get_html_string(), self.mix.get_html_string())
|
|
71
|
+
|
|
72
|
+
#class FieldnamelessTableTest(unittest.TestCase):
|
|
73
|
+
#
|
|
74
|
+
# """Make sure that building and stringing a table with no fieldnames works fine"""
|
|
75
|
+
#
|
|
76
|
+
# def setUp(self):
|
|
77
|
+
# self.x = PrettyTable()
|
|
78
|
+
# self.x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
79
|
+
# self.x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
80
|
+
# self.x.add_row(["Darwin", 112, 120900, 1714.7])
|
|
81
|
+
# self.x.add_row(["Hobart", 1357, 205556, 619.5])
|
|
82
|
+
# self.x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
83
|
+
# self.x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
84
|
+
# self.x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
85
|
+
#
|
|
86
|
+
# def testCanStringASCII(self):
|
|
87
|
+
# self.x.get_string()
|
|
88
|
+
#
|
|
89
|
+
# def testCanStringHTML(self):
|
|
90
|
+
# self.x.get_html_string()
|
|
91
|
+
#
|
|
92
|
+
# def testAddFieldnamesLater(self):
|
|
93
|
+
# self.x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
|
|
94
|
+
# self.x.get_string()
|
|
95
|
+
|
|
96
|
+
class CityDataTest(unittest.TestCase):
|
|
97
|
+
|
|
98
|
+
"""Just build the Australian capital city data example table."""
|
|
99
|
+
|
|
100
|
+
def setUp(self):
|
|
101
|
+
|
|
102
|
+
self.x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
|
|
103
|
+
self.x.add_row(["Adelaide",1295, 1158259, 600.5])
|
|
104
|
+
self.x.add_row(["Brisbane",5905, 1857594, 1146.4])
|
|
105
|
+
self.x.add_row(["Darwin", 112, 120900, 1714.7])
|
|
106
|
+
self.x.add_row(["Hobart", 1357, 205556, 619.5])
|
|
107
|
+
self.x.add_row(["Sydney", 2058, 4336374, 1214.8])
|
|
108
|
+
self.x.add_row(["Melbourne", 1566, 3806092, 646.9])
|
|
109
|
+
self.x.add_row(["Perth", 5386, 1554769, 869.4])
|
|
110
|
+
|
|
111
|
+
class OptionOverrideTests(CityDataTest):
|
|
112
|
+
|
|
113
|
+
"""Make sure all options are properly overwritten by get_string."""
|
|
114
|
+
|
|
115
|
+
def testBorder(self):
|
|
116
|
+
default = self.x.get_string()
|
|
117
|
+
override = self.x.get_string(border=False)
|
|
118
|
+
self.assertTrue(default != override)
|
|
119
|
+
|
|
120
|
+
def testHeader(self):
|
|
121
|
+
default = self.x.get_string()
|
|
122
|
+
override = self.x.get_string(header=False)
|
|
123
|
+
self.assertTrue(default != override)
|
|
124
|
+
|
|
125
|
+
def testHrulesAll(self):
|
|
126
|
+
default = self.x.get_string()
|
|
127
|
+
override = self.x.get_string(hrules=ALL)
|
|
128
|
+
self.assertTrue(default != override)
|
|
129
|
+
|
|
130
|
+
def testHrulesNone(self):
|
|
131
|
+
|
|
132
|
+
default = self.x.get_string()
|
|
133
|
+
override = self.x.get_string(hrules=NONE)
|
|
134
|
+
self.assertTrue(default != override)
|
|
135
|
+
|
|
136
|
+
class OptionAttributeTests(CityDataTest):
|
|
137
|
+
|
|
138
|
+
"""Make sure all options which have an attribute interface work as they should.
|
|
139
|
+
Also make sure option settings are copied correctly when a table is cloned by
|
|
140
|
+
slicing."""
|
|
141
|
+
|
|
142
|
+
def testSetForAllColumns(self):
|
|
143
|
+
self.x.field_names = sorted(self.x.field_names)
|
|
144
|
+
self.x.align = "l"
|
|
145
|
+
self.x.max_width = 10
|
|
146
|
+
self.x.start = 2
|
|
147
|
+
self.x.end = 4
|
|
148
|
+
self.x.sortby = "Area"
|
|
149
|
+
self.x.reversesort = True
|
|
150
|
+
self.x.header = True
|
|
151
|
+
self.x.border = False
|
|
152
|
+
self.x.hrule = True
|
|
153
|
+
self.x.int_format = "4"
|
|
154
|
+
self.x.float_format = "2.2"
|
|
155
|
+
self.x.padding_width = 2
|
|
156
|
+
self.x.left_padding_width = 2
|
|
157
|
+
self.x.right_padding_width = 2
|
|
158
|
+
self.x.vertical_char = "!"
|
|
159
|
+
self.x.horizontal_char = "~"
|
|
160
|
+
self.x.junction_char = "*"
|
|
161
|
+
self.x.format = True
|
|
162
|
+
self.x.attributes = {"class" : "prettytable"}
|
|
163
|
+
assert self.x.get_string() == self.x[:].get_string()
|
|
164
|
+
|
|
165
|
+
def testSetForOneColumn(self):
|
|
166
|
+
self.x.align["Rainfall"] = "l"
|
|
167
|
+
self.x.max_width["Name"] = 10
|
|
168
|
+
self.x.int_format["Population"] = "4"
|
|
169
|
+
self.x.float_format["Area"] = "2.2"
|
|
170
|
+
assert self.x.get_string() == self.x[:].get_string()
|
|
171
|
+
|
|
172
|
+
class BasicTests(CityDataTest):
|
|
173
|
+
|
|
174
|
+
"""Some very basic tests."""
|
|
175
|
+
|
|
176
|
+
def testNoBlankLines(self):
|
|
177
|
+
|
|
178
|
+
"""No table should ever have blank lines in it."""
|
|
179
|
+
|
|
180
|
+
string = self.x.get_string()
|
|
181
|
+
lines = string.split("\n")
|
|
182
|
+
self.assertTrue("" not in lines)
|
|
183
|
+
|
|
184
|
+
def testAllLengthsEqual(self):
|
|
185
|
+
|
|
186
|
+
"""All lines in a table should be of the same length."""
|
|
187
|
+
|
|
188
|
+
string = self.x.get_string()
|
|
189
|
+
lines = string.split("\n")
|
|
190
|
+
lengths = [len(line) for line in lines]
|
|
191
|
+
lengths = set(lengths)
|
|
192
|
+
self.assertEqual(len(lengths),1)
|
|
193
|
+
|
|
194
|
+
class NoBorderBasicTests(BasicTests):
|
|
195
|
+
|
|
196
|
+
"""Run the basic tests with border = False"""
|
|
197
|
+
|
|
198
|
+
def setUp(self):
|
|
199
|
+
BasicTests.setUp(self)
|
|
200
|
+
self.x.border = False
|
|
201
|
+
|
|
202
|
+
class NoHeaderBasicTests(BasicTests):
|
|
203
|
+
|
|
204
|
+
"""Run the basic tests with header = False"""
|
|
205
|
+
|
|
206
|
+
def setUp(self):
|
|
207
|
+
BasicTests.setUp(self)
|
|
208
|
+
self.x.header = False
|
|
209
|
+
|
|
210
|
+
class HrulesNoneBasicTests(BasicTests):
|
|
211
|
+
|
|
212
|
+
"""Run the basic tests with hrules = NONE"""
|
|
213
|
+
|
|
214
|
+
def setUp(self):
|
|
215
|
+
BasicTests.setUp(self)
|
|
216
|
+
self.x.hrules = NONE
|
|
217
|
+
|
|
218
|
+
class HrulesAllBasicTests(BasicTests):
|
|
219
|
+
|
|
220
|
+
"""Run the basic tests with hrules = ALL"""
|
|
221
|
+
|
|
222
|
+
def setUp(self):
|
|
223
|
+
BasicTests.setUp(self)
|
|
224
|
+
self.x.hrules = ALL
|
|
225
|
+
|
|
226
|
+
class PresetBasicTests(BasicTests):
|
|
227
|
+
|
|
228
|
+
"""Run the basic tests after using set_style"""
|
|
229
|
+
|
|
230
|
+
def setUp(self):
|
|
231
|
+
BasicTests.setUp(self)
|
|
232
|
+
self.x.set_style(MSWORD_FRIENDLY)
|
|
233
|
+
|
|
234
|
+
class SlicingTests(CityDataTest):
|
|
235
|
+
|
|
236
|
+
def setUp(self):
|
|
237
|
+
CityDataTest.setUp(self)
|
|
238
|
+
|
|
239
|
+
def testSliceAll(self):
|
|
240
|
+
y = self.x[:]
|
|
241
|
+
assert self.x.get_string() == y.get_string()
|
|
242
|
+
|
|
243
|
+
def testSliceFirstTwoRows(self):
|
|
244
|
+
y = self.x[0:2]
|
|
245
|
+
string = y.get_string()
|
|
246
|
+
assert len(string.split("\n")) == 6
|
|
247
|
+
assert "Adelaide" in string
|
|
248
|
+
assert "Brisbane" in string
|
|
249
|
+
assert "Melbourne" not in string
|
|
250
|
+
assert "Perth" not in string
|
|
251
|
+
|
|
252
|
+
def testSliceLastTwoRows(self):
|
|
253
|
+
y = self.x[-2:]
|
|
254
|
+
string = y.get_string()
|
|
255
|
+
assert len(string.split("\n")) == 6
|
|
256
|
+
assert "Adelaide" not in string
|
|
257
|
+
assert "Brisbane" not in string
|
|
258
|
+
assert "Melbourne" in string
|
|
259
|
+
assert "Perth" in string
|
|
260
|
+
|
|
261
|
+
class SortingTests(CityDataTest):
|
|
262
|
+
|
|
263
|
+
def setUp(self):
|
|
264
|
+
CityDataTest.setUp(self)
|
|
265
|
+
|
|
266
|
+
def testSortBy(self):
|
|
267
|
+
self.x.sortby = self.x.field_names[0]
|
|
268
|
+
old = self.x.get_string()
|
|
269
|
+
for field in self.x.field_names[1:]:
|
|
270
|
+
self.x.sortby = field
|
|
271
|
+
new = self.x.get_string()
|
|
272
|
+
assert new != old
|
|
273
|
+
|
|
274
|
+
def testReverseSort(self):
|
|
275
|
+
for field in self.x.field_names:
|
|
276
|
+
self.x.sortby = field
|
|
277
|
+
self.x.reversesort = False
|
|
278
|
+
forward = self.x.get_string()
|
|
279
|
+
self.x.reversesort = True
|
|
280
|
+
backward = self.x.get_string()
|
|
281
|
+
forward_lines = forward.split("\n")[2:] # Discard header lines
|
|
282
|
+
backward_lines = backward.split("\n")[2:]
|
|
283
|
+
backward_lines.reverse()
|
|
284
|
+
assert forward_lines == backward_lines
|
|
285
|
+
|
|
286
|
+
def testSortKey(self):
|
|
287
|
+
# Test sorting by length of city name
|
|
288
|
+
def key(vals):
|
|
289
|
+
vals[0] = len(vals[0])
|
|
290
|
+
return vals
|
|
291
|
+
self.x.sortby = "City name"
|
|
292
|
+
self.x.sort_key = key
|
|
293
|
+
assert self.x.get_string().strip() == """+-----------+------+------------+-----------------+
|
|
294
|
+
| City name | Area | Population | Annual Rainfall |
|
|
295
|
+
+-----------+------+------------+-----------------+
|
|
296
|
+
| Perth | 5386 | 1554769 | 869.4 |
|
|
297
|
+
| Darwin | 112 | 120900 | 1714.7 |
|
|
298
|
+
| Hobart | 1357 | 205556 | 619.5 |
|
|
299
|
+
| Sydney | 2058 | 4336374 | 1214.8 |
|
|
300
|
+
| Adelaide | 1295 | 1158259 | 600.5 |
|
|
301
|
+
| Brisbane | 5905 | 1857594 | 1146.4 |
|
|
302
|
+
| Melbourne | 1566 | 3806092 | 646.9 |
|
|
303
|
+
+-----------+------+------------+-----------------+
|
|
304
|
+
""".strip()
|
|
305
|
+
|
|
306
|
+
class IntegerFormatBasicTests(BasicTests):
|
|
307
|
+
|
|
308
|
+
"""Run the basic tests after setting an integer format string"""
|
|
309
|
+
|
|
310
|
+
def setUp(self):
|
|
311
|
+
BasicTests.setUp(self)
|
|
312
|
+
self.x.int_format = "04"
|
|
313
|
+
|
|
314
|
+
class FloatFormatBasicTests(BasicTests):
|
|
315
|
+
|
|
316
|
+
"""Run the basic tests after setting a float format string"""
|
|
317
|
+
|
|
318
|
+
def setUp(self):
|
|
319
|
+
BasicTests.setUp(self)
|
|
320
|
+
self.x.float_format = "6.2f"
|
|
321
|
+
|
|
322
|
+
class FloatFormatTests(unittest.TestCase):
|
|
323
|
+
|
|
324
|
+
def setUp(self):
|
|
325
|
+
self.x = PrettyTable(["Constant", "Value"])
|
|
326
|
+
self.x.add_row(["Pi", pi])
|
|
327
|
+
self.x.add_row(["e", e])
|
|
328
|
+
self.x.add_row(["sqrt(2)", sqrt(2)])
|
|
329
|
+
|
|
330
|
+
def testNoDecimals(self):
|
|
331
|
+
self.x.float_format = ".0f"
|
|
332
|
+
self.x.caching = False
|
|
333
|
+
assert "." not in self.x.get_string()
|
|
334
|
+
|
|
335
|
+
def testRoundTo5DP(self):
|
|
336
|
+
self.x.float_format = ".5f"
|
|
337
|
+
string = self.x.get_string()
|
|
338
|
+
assert "3.14159" in string
|
|
339
|
+
assert "3.141592" not in string
|
|
340
|
+
assert "2.71828" in string
|
|
341
|
+
assert "2.718281" not in string
|
|
342
|
+
assert "2.718282" not in string
|
|
343
|
+
assert "1.41421" in string
|
|
344
|
+
assert "1.414213" not in string
|
|
345
|
+
|
|
346
|
+
def testPadWith2Zeroes(self):
|
|
347
|
+
self.x.float_format = "06.2f"
|
|
348
|
+
string = self.x.get_string()
|
|
349
|
+
assert "003.14" in string
|
|
350
|
+
assert "002.72" in string
|
|
351
|
+
assert "001.41" in string
|
|
352
|
+
|
|
353
|
+
class BreakLineTests(unittest.TestCase):
|
|
354
|
+
def testAsciiBreakLine(self):
|
|
355
|
+
t = PrettyTable(['Field 1', 'Field 2'])
|
|
356
|
+
t.add_row(['value 1', 'value2\nsecond line'])
|
|
357
|
+
t.add_row(['value 3', 'value4'])
|
|
358
|
+
result = t.get_string(hrules=ALL)
|
|
359
|
+
assert result.strip() == """
|
|
360
|
+
+---------+-------------+
|
|
361
|
+
| Field 1 | Field 2 |
|
|
362
|
+
+---------+-------------+
|
|
363
|
+
| value 1 | value2 |
|
|
364
|
+
| | second line |
|
|
365
|
+
+---------+-------------+
|
|
366
|
+
| value 3 | value4 |
|
|
367
|
+
+---------+-------------+
|
|
368
|
+
""".strip()
|
|
369
|
+
|
|
370
|
+
t = PrettyTable(['Field 1', 'Field 2'])
|
|
371
|
+
t.add_row(['value 1', 'value2\nsecond line'])
|
|
372
|
+
t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
|
|
373
|
+
result = t.get_string(hrules=ALL)
|
|
374
|
+
assert result.strip() == """
|
|
375
|
+
+------------+-------------+
|
|
376
|
+
| Field 1 | Field 2 |
|
|
377
|
+
+------------+-------------+
|
|
378
|
+
| value 1 | value2 |
|
|
379
|
+
| | second line |
|
|
380
|
+
+------------+-------------+
|
|
381
|
+
| value 3 | value4 |
|
|
382
|
+
| | |
|
|
383
|
+
| other line | |
|
|
384
|
+
| | value5 |
|
|
385
|
+
+------------+-------------+
|
|
386
|
+
""".strip()
|
|
387
|
+
|
|
388
|
+
t = PrettyTable(['Field 1', 'Field 2'])
|
|
389
|
+
t.add_row(['value 1', 'value2\nsecond line'])
|
|
390
|
+
t.add_row(['value 3\n\nother line', 'value4\n\n\nvalue5'])
|
|
391
|
+
result = t.get_string()
|
|
392
|
+
assert result.strip() == """
|
|
393
|
+
+------------+-------------+
|
|
394
|
+
| Field 1 | Field 2 |
|
|
395
|
+
+------------+-------------+
|
|
396
|
+
| value 1 | value2 |
|
|
397
|
+
| | second line |
|
|
398
|
+
| value 3 | value4 |
|
|
399
|
+
| | |
|
|
400
|
+
| other line | |
|
|
401
|
+
| | value5 |
|
|
402
|
+
+------------+-------------+
|
|
403
|
+
""".strip()
|
|
404
|
+
|
|
405
|
+
def testHtmlBreakLine(self):
|
|
406
|
+
t = PrettyTable(['Field 1', 'Field 2'])
|
|
407
|
+
t.add_row(['value 1', 'value2\nsecond line'])
|
|
408
|
+
t.add_row(['value 3', 'value4'])
|
|
409
|
+
result = t.get_html_string(hrules=ALL)
|
|
410
|
+
assert result.strip() == """
|
|
411
|
+
<table>
|
|
412
|
+
<tr>
|
|
413
|
+
<th>Field 1</th>
|
|
414
|
+
<th>Field 2</th>
|
|
415
|
+
</tr>
|
|
416
|
+
<tr>
|
|
417
|
+
<td>value 1</td>
|
|
418
|
+
<td>value2<br>second line</td>
|
|
419
|
+
</tr>
|
|
420
|
+
<tr>
|
|
421
|
+
<td>value 3</td>
|
|
422
|
+
<td>value4</td>
|
|
423
|
+
</tr>
|
|
424
|
+
</table>
|
|
425
|
+
""".strip()
|
|
426
|
+
|
|
427
|
+
class HtmlOutputTests(unittest.TestCase):
|
|
428
|
+
|
|
429
|
+
def testHtmlOutput(self):
|
|
430
|
+
t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
|
|
431
|
+
t.add_row(['value 1', 'value2', 'value3'])
|
|
432
|
+
t.add_row(['value 4', 'value5', 'value6'])
|
|
433
|
+
t.add_row(['value 7', 'value8', 'value9'])
|
|
434
|
+
result = t.get_html_string()
|
|
435
|
+
assert result.strip() == """
|
|
436
|
+
<table>
|
|
437
|
+
<tr>
|
|
438
|
+
<th>Field 1</th>
|
|
439
|
+
<th>Field 2</th>
|
|
440
|
+
<th>Field 3</th>
|
|
441
|
+
</tr>
|
|
442
|
+
<tr>
|
|
443
|
+
<td>value 1</td>
|
|
444
|
+
<td>value2</td>
|
|
445
|
+
<td>value3</td>
|
|
446
|
+
</tr>
|
|
447
|
+
<tr>
|
|
448
|
+
<td>value 4</td>
|
|
449
|
+
<td>value5</td>
|
|
450
|
+
<td>value6</td>
|
|
451
|
+
</tr>
|
|
452
|
+
<tr>
|
|
453
|
+
<td>value 7</td>
|
|
454
|
+
<td>value8</td>
|
|
455
|
+
<td>value9</td>
|
|
456
|
+
</tr>
|
|
457
|
+
</table>
|
|
458
|
+
""".strip()
|
|
459
|
+
|
|
460
|
+
def testHtmlOutputFormated(self):
|
|
461
|
+
t = PrettyTable(['Field 1', 'Field 2', 'Field 3'])
|
|
462
|
+
t.add_row(['value 1', 'value2', 'value3'])
|
|
463
|
+
t.add_row(['value 4', 'value5', 'value6'])
|
|
464
|
+
t.add_row(['value 7', 'value8', 'value9'])
|
|
465
|
+
result = t.get_html_string(format=True)
|
|
466
|
+
assert result.strip() == """
|
|
467
|
+
<table frame="box" rules="cols">
|
|
468
|
+
<tr>
|
|
469
|
+
<th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
|
|
470
|
+
<th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
|
|
471
|
+
<th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
|
|
472
|
+
</tr>
|
|
473
|
+
<tr>
|
|
474
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 1</td>
|
|
475
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value2</td>
|
|
476
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value3</td>
|
|
477
|
+
</tr>
|
|
478
|
+
<tr>
|
|
479
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 4</td>
|
|
480
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value5</td>
|
|
481
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value6</td>
|
|
482
|
+
</tr>
|
|
483
|
+
<tr>
|
|
484
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 7</td>
|
|
485
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value8</td>
|
|
486
|
+
<td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value9</td>
|
|
487
|
+
</tr>
|
|
488
|
+
</table>
|
|
489
|
+
""".strip()
|
|
490
|
+
|
|
491
|
+
class CsvConstructorTest(BasicTests):
|
|
492
|
+
|
|
493
|
+
def setUp(self):
|
|
494
|
+
|
|
495
|
+
csv_string = """City name, Area , Population , Annual Rainfall
|
|
496
|
+
Sydney, 2058 , 4336374 , 1214.8
|
|
497
|
+
Melbourne, 1566 , 3806092 , 646.9
|
|
498
|
+
Brisbane, 5905 , 1857594 , 1146.4
|
|
499
|
+
Perth, 5386 , 1554769 , 869.4
|
|
500
|
+
Adelaide, 1295 , 1158259 , 600.5
|
|
501
|
+
Hobart, 1357 , 205556 , 619.5
|
|
502
|
+
Darwin, 0112 , 120900 , 1714.7"""
|
|
503
|
+
csv_fp = StringIO.StringIO(csv_string)
|
|
504
|
+
self.x = from_csv(csv_fp)
|
|
505
|
+
|
|
506
|
+
if _have_sqlite:
|
|
507
|
+
class DatabaseConstructorTest(BasicTests):
|
|
508
|
+
|
|
509
|
+
def setUp(self):
|
|
510
|
+
self.conn = sqlite3.connect(":memory:")
|
|
511
|
+
self.cur = self.conn.cursor()
|
|
512
|
+
self.cur.execute("CREATE TABLE cities (name TEXT, area INTEGER, population INTEGER, rainfall REAL)")
|
|
513
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Adelaide\", 1295, 1158259, 600.5)")
|
|
514
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Brisbane\", 5905, 1857594, 1146.4)")
|
|
515
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Darwin\", 112, 120900, 1714.7)")
|
|
516
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Hobart\", 1357, 205556, 619.5)")
|
|
517
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Sydney\", 2058, 4336374, 1214.8)")
|
|
518
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Melbourne\", 1566, 3806092, 646.9)")
|
|
519
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Perth\", 5386, 1554769, 869.4)")
|
|
520
|
+
self.cur.execute("SELECT * FROM cities")
|
|
521
|
+
self.x = from_db_cursor(self.cur)
|
|
522
|
+
|
|
523
|
+
def testNonSelectCurosr(self):
|
|
524
|
+
self.cur.execute("INSERT INTO cities VALUES (\"Adelaide\", 1295, 1158259, 600.5)")
|
|
525
|
+
assert from_db_cursor(self.cur) is None
|
|
526
|
+
|
|
527
|
+
class HtmlConstructorTest(CityDataTest):
|
|
528
|
+
|
|
529
|
+
def testHtmlAndBack(self):
|
|
530
|
+
html_string = self.x.get_html_string()
|
|
531
|
+
new_table = from_html(html_string)[0]
|
|
532
|
+
assert new_table.get_string() == self.x.get_string()
|
|
533
|
+
|
|
534
|
+
def testHtmlOneAndBack(self):
|
|
535
|
+
html_string = self.x.get_html_string()
|
|
536
|
+
new_table = from_html_one(html_string)
|
|
537
|
+
assert new_table.get_string() == self.x.get_string()
|
|
538
|
+
|
|
539
|
+
def testHtmlOneFailOnMany(self):
|
|
540
|
+
html_string = self.x.get_html_string()
|
|
541
|
+
html_string += self.x.get_html_string()
|
|
542
|
+
self.assertRaises(Exception, from_html_one, html_string)
|
|
543
|
+
|
|
544
|
+
class PrintEnglishTest(CityDataTest):
|
|
545
|
+
|
|
546
|
+
def testPrint(self):
|
|
547
|
+
print()
|
|
548
|
+
print(self.x)
|
|
549
|
+
|
|
550
|
+
class PrintJapanestTest(unittest.TestCase):
|
|
551
|
+
|
|
552
|
+
def setUp(self):
|
|
553
|
+
|
|
554
|
+
self.x = PrettyTable(["Kanji", "Hiragana", "English"])
|
|
555
|
+
self.x.add_row(["神戸", "こうべ", "Kobe"])
|
|
556
|
+
self.x.add_row(["京都", "きょうと", "Kyoto"])
|
|
557
|
+
self.x.add_row(["長崎", "ながさき", "Nagasaki"])
|
|
558
|
+
self.x.add_row(["名古屋", "なごや", "Nagoya"])
|
|
559
|
+
self.x.add_row(["大阪", "おおさか", "Osaka"])
|
|
560
|
+
self.x.add_row(["札幌", "さっぽろ", "Sapporo"])
|
|
561
|
+
self.x.add_row(["東京", "とうきょう", "Tokyo"])
|
|
562
|
+
self.x.add_row(["横浜", "よこはま", "Yokohama"])
|
|
563
|
+
|
|
564
|
+
def testPrint(self):
|
|
565
|
+
print()
|
|
566
|
+
print(self.x)
|
|
567
|
+
|
|
568
|
+
if __name__ == "__main__":
|
|
569
|
+
unittest.main()
|
|
@@ -7,15 +7,19 @@ setup(
|
|
|
7
7
|
version=version,
|
|
8
8
|
classifiers=[
|
|
9
9
|
'Programming Language :: Python',
|
|
10
|
+
'Programming Language :: Python :: 2.4',
|
|
11
|
+
'Programming Language :: Python :: 2.5',
|
|
10
12
|
'Programming Language :: Python :: 2.6',
|
|
11
13
|
'Programming Language :: Python :: 2.7',
|
|
12
14
|
'Programming Language :: Python :: 3',
|
|
13
15
|
'License :: OSI Approved :: BSD License',
|
|
14
16
|
'Topic :: Text Processing'
|
|
15
17
|
],
|
|
18
|
+
license="BSD (3 clause)",
|
|
16
19
|
description='A simple Python library for easily displaying tabular data in a visually appealing ASCII table format',
|
|
17
20
|
author='Luke Maurits',
|
|
18
21
|
author_email='luke@maurits.id.au',
|
|
19
22
|
url='http://code.google.com/p/prettytable',
|
|
20
|
-
py_modules=['prettytable']
|
|
23
|
+
py_modules=['prettytable'],
|
|
24
|
+
test_suite = "prettytable_test"
|
|
21
25
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|