Skip to content

Commit 0ca4ff3

Browse files
committed
strings without u-prefix + tests on Python 3.2 (except doctests)
1 parent 00c6502 commit 0ca4ff3

7 files changed

+191
-183
lines changed

HOWTOPUBLISH

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# bump version number in setup.py and tabulate.py
22
python2 benchmark.py # then update README
3-
tox -e py26,py27-extra,py33-extra
3+
tox -e py26,py27-extra,py32,py33-extra
44
python2 setup.py register -n
55
python2 setup.py register sdist upload
66
# tag version release

tabulate.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _pipe_line_with_colons(colwidths, colaligns):
9292
"""Return a horizontal line with optional colons to indicate column's
9393
alignment (as in `pipe` output format)."""
9494
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
95-
return u"|" + u"|".join(segments) + u"|"
95+
return "|" + "|".join(segments) + "|"
9696

9797

9898
def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
@@ -198,7 +198,7 @@ def simple_separated_format(separator):
198198
"""Construct a simple TableFormat with columns separated by a separator.
199199
200200
>>> tsv = simple_separated_format("\\t") ; \
201-
tabulate([["foo", 1], ["spam", 23]], tablefmt=tsv) == u'foo \\t 1\\nspam\\t23'
201+
tabulate([["foo", 1], ["spam", 23]], tablefmt=tsv) == 'foo \\t 1\\nspam\\t23'
202202
True
203203
204204
"""
@@ -309,7 +309,7 @@ def _padleft(width, s, has_invisible=True):
309309
310310
"""
311311
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
312-
fmt = u"{0:>%ds}" % iwidth
312+
fmt = "{0:>%ds}" % iwidth
313313
return fmt.format(s)
314314

315315

@@ -321,7 +321,7 @@ def _padright(width, s, has_invisible=True):
321321
322322
"""
323323
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
324-
fmt = u"{0:<%ds}" % iwidth
324+
fmt = "{0:<%ds}" % iwidth
325325
return fmt.format(s)
326326

327327

@@ -333,7 +333,7 @@ def _padboth(width, s, has_invisible=True):
333333
334334
"""
335335
iwidth = width + len(s) - len(_strip_invisible(s)) if has_invisible else width
336-
fmt = u"{0:^%ds}" % iwidth
336+
fmt = "{0:^%ds}" % iwidth
337337
return fmt.format(s)
338338

339339

@@ -427,7 +427,7 @@ def _column_type(strings, has_invisible=True):
427427
return reduce(_more_generic, types, int)
428428

429429

430-
def _format(val, valtype, floatfmt, missingval=u""):
430+
def _format(val, valtype, floatfmt, missingval=""):
431431
"""Format a value accoding to its type.
432432
433433
Unicode is supported:
@@ -443,13 +443,13 @@ def _format(val, valtype, floatfmt, missingval=u""):
443443
return missingval
444444

445445
if valtype in [int, _text_type]:
446-
return u"{0}".format(val)
446+
return "{0}".format(val)
447447
elif valtype is _binary_type:
448448
return _text_type(val, "ascii")
449449
elif valtype is float:
450450
return format(float(val), floatfmt)
451451
else:
452-
return u"{0}".format(val)
452+
return "{0}".format(val)
453453

454454

455455
def _align_header(header, alignment, width):
@@ -458,7 +458,7 @@ def _align_header(header, alignment, width):
458458
elif alignment == "center":
459459
return _padboth(width, header)
460460
elif not alignment:
461-
return u"{0}".format(header)
461+
return "{0}".format(header)
462462
else:
463463
return _padleft(width, header)
464464

@@ -532,14 +532,14 @@ def _normalize_tabular_data(tabular_data, headers):
532532
nhs = len(headers)
533533
ncols = len(rows[0])
534534
if nhs < ncols:
535-
headers = [u""]*(ncols - nhs) + headers
535+
headers = [""]*(ncols - nhs) + headers
536536

537537
return rows, headers
538538

539539

540540
def tabulate(tabular_data, headers=[], tablefmt="simple",
541541
floatfmt="g", numalign="decimal", stralign="left",
542-
missingval=u""):
542+
missingval=""):
543543
"""Format a fixed width table for pretty printing.
544544
545545
>>> print(tabulate([[1, 2.34], [-56, "8.999"], ["2", "10001"]]))
@@ -740,8 +740,8 @@ def tabulate(tabular_data, headers=[], tablefmt="simple",
740740

741741
# optimization: look for ANSI control codes once,
742742
# enable smart width functions only if a control code is found
743-
plain_text = u'\n'.join(['\t'.join(map(_text_type, headers))] + \
744-
[u'\t'.join(map(_text_type, row)) for row in list_of_lists])
743+
plain_text = '\n'.join(['\t'.join(map(_text_type, headers))] + \
744+
['\t'.join(map(_text_type, row)) for row in list_of_lists])
745745
has_invisible = re.search(_invisible_codes, plain_text)
746746
if has_invisible:
747747
width_fn = _visible_width
@@ -806,7 +806,7 @@ def _build_line(colwidths, colaligns, linefmt):
806806

807807
def _pad_row(cells, padding):
808808
if cells:
809-
pad = u" "*padding
809+
pad = " "*padding
810810
padded_cells = [pad + cell + pad for cell in cells]
811811
return padded_cells
812812
else:

test/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_tabulate_formats():
2020
print("tabulate_formats = %r" % supported)
2121
assert type(supported) is list
2222
for fmt in supported:
23-
assert type(fmt) is type(u"")
23+
assert type(fmt) is type("")
2424

2525

2626
def _check_signature(function, expected_sig):

test/test_input.py

+71-71
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ def test_iterable_of_iterables():
1111
"Input: an interable of iterables."
1212
ii = iter(map(lambda x: iter(x), [range(5), range(5,0,-1)]))
1313
expected = "\n".join(
14-
[u'- - - - -',
15-
u'0 1 2 3 4',
16-
u'5 4 3 2 1',
17-
u'- - - - -'])
14+
['- - - - -',
15+
'0 1 2 3 4',
16+
'5 4 3 2 1',
17+
'- - - - -'])
1818
result = tabulate(ii)
1919
print("Expected:\n%s\n" % expected)
2020
print("Got:\n%s\n" % result)
@@ -25,10 +25,10 @@ def test_iterable_of_iterables_headers():
2525
"Input: an interable of iterables with headers."
2626
ii = iter(map(lambda x: iter(x), [range(5), range(5,0,-1)]))
2727
expected = "\n".join(
28-
[u' a b c d e',
29-
u'--- --- --- --- ---',
30-
u' 0 1 2 3 4',
31-
u' 5 4 3 2 1'])
28+
[' a b c d e',
29+
'--- --- --- --- ---',
30+
' 0 1 2 3 4',
31+
' 5 4 3 2 1'])
3232
result = tabulate(ii, "abcde")
3333
print("Expected:\n%s\n" % expected)
3434
print("Got:\n%s\n" % result)
@@ -39,10 +39,10 @@ def test_iterable_of_iterables_firstrow():
3939
"Input: an interable of iterables with the first row as headers"
4040
ii = iter(map(lambda x: iter(x), ["abcde", range(5), range(5,0,-1)]))
4141
expected = "\n".join(
42-
[u' a b c d e',
43-
u'--- --- --- --- ---',
44-
u' 0 1 2 3 4',
45-
u' 5 4 3 2 1'])
42+
[' a b c d e',
43+
'--- --- --- --- ---',
44+
' 0 1 2 3 4',
45+
' 5 4 3 2 1'])
4646
result = tabulate(ii, "firstrow")
4747
print("Expected:\n%s\n" % expected)
4848
print("Got:\n%s\n" % result)
@@ -53,10 +53,10 @@ def test_list_of_lists():
5353
"Input: a list of lists with headers."
5454
ll = [["a","one",1],["b","two",None]]
5555
expected = "\n".join([
56-
u' string number',
57-
u'-- -------- --------',
58-
u'a one 1',
59-
u'b two'])
56+
' string number',
57+
'-- -------- --------',
58+
'a one 1',
59+
'b two'])
6060
result = tabulate(ll, headers=["string","number"])
6161
print("Expected:\n%s\n" % expected)
6262
print("Got:\n%s\n" % result)
@@ -67,10 +67,10 @@ def test_list_of_lists_firstrow():
6767
"Input: a list of lists with the first row as headers."
6868
ll = [["string","number"],["a","one",1],["b","two",None]]
6969
expected = "\n".join([
70-
u' string number',
71-
u'-- -------- --------',
72-
u'a one 1',
73-
u'b two'])
70+
' string number',
71+
'-- -------- --------',
72+
'a one 1',
73+
'b two'])
7474
result = tabulate(ll, headers="firstrow")
7575
print("Expected:\n%s\n" % expected)
7676
print("Got:\n%s\n" % result)
@@ -81,10 +81,10 @@ def test_list_of_lists_keys():
8181
"Input: a list of lists with column indices as headers."
8282
ll = [["a","one",1],["b","two",None]]
8383
expected = "\n".join([
84-
u'0 1 2',
85-
u'--- --- ---',
86-
u'a one 1',
87-
u'b two'])
84+
'0 1 2',
85+
'--- --- ---',
86+
'a one 1',
87+
'b two'])
8888
result = tabulate(ll, headers="keys")
8989
print("Expected:\n%s\n" % expected)
9090
print("Got:\n%s\n" % result)
@@ -98,19 +98,19 @@ def test_dict_like():
9898
# keys' order (hence columns' order) is not deterministic in Python 3
9999
# => we have to consider both possible results as valid
100100
expected1 = "\n".join([
101-
u' a b',
102-
u'--- ---',
103-
u' 0 101',
104-
u' 1 102',
105-
u' 2 103',
106-
u' 104'])
101+
' a b',
102+
'--- ---',
103+
' 0 101',
104+
' 1 102',
105+
' 2 103',
106+
' 104'])
107107
expected2 = "\n".join([
108-
u' b a',
109-
u'--- ---',
110-
u'101 0',
111-
u'102 1',
112-
u'103 2',
113-
u'104'])
108+
' b a',
109+
'--- ---',
110+
'101 0',
111+
'102 1',
112+
'103 2',
113+
'104'])
114114
result = tabulate(dd, "keys")
115115
print("Keys' order: %s" % dd.keys())
116116
print("Expected 1:\n%s\n" % expected1)
@@ -125,11 +125,11 @@ def test_numpy_2d():
125125
import numpy
126126
na = (numpy.arange(1,10, dtype=numpy.float32).reshape((3,3))**3)*0.5
127127
expected = "\n".join([
128-
u' a b c',
129-
u'----- ----- -----',
130-
u' 0.5 4 13.5',
131-
u' 32 62.5 108',
132-
u'171.5 256 364.5'])
128+
' a b c',
129+
'----- ----- -----',
130+
' 0.5 4 13.5',
131+
' 32 62.5 108',
132+
'171.5 256 364.5'])
133133
result = tabulate(na, ["a", "b", "c"])
134134
print("Expected:\n%s\n" % expected)
135135
print("Got:\n%s\n" % result)
@@ -145,10 +145,10 @@ def test_numpy_2d_firstrow():
145145
import numpy
146146
na = (numpy.arange(1,10, dtype=numpy.int32).reshape((3,3))**3)
147147
expected = "\n".join([
148-
u' 1 8 27',
149-
u'--- --- ----',
150-
u' 64 125 216',
151-
u'343 512 729'])
148+
' 1 8 27',
149+
'--- --- ----',
150+
' 64 125 216',
151+
'343 512 729'])
152152
result = tabulate(na, headers="firstrow")
153153
print("Expected:\n%s\n" % expected)
154154
print("Got:\n%r\n" % result)
@@ -164,11 +164,11 @@ def test_numpy_2d_keys():
164164
import numpy
165165
na = (numpy.arange(1,10, dtype=numpy.float32).reshape((3,3))**3)*0.5
166166
expected = "\n".join([
167-
u' 0 1 2',
168-
u'----- ----- -----',
169-
u' 0.5 4 13.5',
170-
u' 32 62.5 108',
171-
u'171.5 256 364.5'])
167+
' 0 1 2',
168+
'----- ----- -----',
169+
' 0.5 4 13.5',
170+
' 32 62.5 108',
171+
'171.5 256 364.5'])
172172
result = tabulate(na, headers="keys")
173173
print("Expected:\n%s\n" % expected)
174174
print("Got:\n%s\n" % result)
@@ -250,10 +250,10 @@ def test_pandas():
250250
import pandas
251251
df = pandas.DataFrame([["one",1],["two",None]], index=["a","b"])
252252
expected = "\n".join([
253-
u' string number',
254-
u'-- -------- --------',
255-
u'a one 1',
256-
u'b two nan'])
253+
' string number',
254+
'-- -------- --------',
255+
'a one 1',
256+
'b two nan'])
257257
result = tabulate(df, headers=["string", "number"])
258258
print("Expected:\n%s\n" % expected)
259259
print("Got:\n%s\n" % result)
@@ -271,9 +271,9 @@ def test_pandas_firstrow():
271271
columns=["string","number"],
272272
index=["a","b"])
273273
expected = "\n".join([
274-
u'a one 1.0',
275-
u'--- ----- -----',
276-
u'b two nan'])
274+
'a one 1.0',
275+
'--- ----- -----',
276+
'b two nan'])
277277
result = tabulate(df, headers="firstrow")
278278
print("Expected:\n%s\n" % expected)
279279
print("Got:\n%s\n" % result)
@@ -291,10 +291,10 @@ def test_pandas_keys():
291291
columns=["string","number"],
292292
index=["a","b"])
293293
expected = "\n".join(
294-
[u' string number',
295-
u'-- -------- --------',
296-
u'a one 1',
297-
u'b two nan'])
294+
[' string number',
295+
'-- -------- --------',
296+
'a one 1',
297+
'b two nan'])
298298
result = tabulate(df, headers="keys")
299299
print("Expected:\n%s\n" % expected)
300300
print("Got:\n%s\n" % result)
@@ -309,11 +309,11 @@ def test_list_of_namedtuples():
309309
from collections import namedtuple
310310
NT = namedtuple("NT", ['foo', 'bar'])
311311
lt = [NT(1,2), NT(3,4)]
312-
expected = u"\n".join([
313-
u'- -',
314-
u'1 2',
315-
u'3 4',
316-
u'- -'])
312+
expected = "\n".join([
313+
'- -',
314+
'1 2',
315+
'3 4',
316+
'- -'])
317317
result = tabulate(lt)
318318
print("Expected:\n%s\n" % expected)
319319
print("Got:\n%s\n" % result)
@@ -325,11 +325,11 @@ def test_list_of_namedtuples_keys():
325325
from collections import namedtuple
326326
NT = namedtuple("NT", ['foo', 'bar'])
327327
lt = [NT(1,2), NT(3,4)]
328-
expected = u"\n".join([
329-
u' foo bar',
330-
u'----- -----',
331-
u' 1 2',
332-
u' 3 4'])
328+
expected = "\n".join([
329+
' foo bar',
330+
'----- -----',
331+
' 1 2',
332+
' 3 4'])
333333
result = tabulate(lt, headers="keys")
334334
print("Expected:\n%s\n" % expected)
335335
print("Got:\n%s\n" % result)

0 commit comments

Comments
 (0)