forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pprint.py
More file actions
184 lines (158 loc) · 6.83 KB
/
Copy pathtest_pprint.py
File metadata and controls
184 lines (158 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import pytest
import numpy as np
from .. import Table
from .. import pprint
BIG_WIDE_ARR = np.arange(2000, dtype=np.float).reshape(100, 20)
SMALL_ARR = np.arange(12, dtype=np.int).reshape(4, 3)
class TestMultiD():
def test_multidim(self):
"""Test printing with multidimensional column"""
arr = [np.array([[1, 2],
[10, 20]]),
np.array([[3, 4],
[30, 40]]),
np.array([[5, 6],
[50, 60]])]
t = Table(arr)
lines = t.pformat()
print lines
assert lines == ['col0 [2] col1 [2] col2 [2]',
'-------- -------- --------',
' 1 .. 2 3 .. 4 5 .. 6',
'10 .. 20 30 .. 40 50 .. 60']
t = Table([arr])
lines = t.pformat()
print lines
assert lines == ['col0 [2,2]',
'----------',
' 1 .. 20',
' 3 .. 40',
' 5 .. 60']
class TestPprint():
def setup_method(self, method):
self.tb = Table(BIG_WIDE_ARR)
self.tb['col0'].format = '%e'
self.tb['col1'].format = '%.6f'
self.tb['col0'].units = 'km**2'
self.tb['col19'].units = 'kg sec m**-2'
self.ts = Table(SMALL_ARR)
def test_format0(self):
"""Try getting screen size but fail to defaults because testing doesn't
have access to screen (fcntl.ioctl fails).
"""
arr = np.arange(4000, dtype=np.float).reshape(100, 40)
lines = Table(arr).pformat()
assert len(lines) == pprint.MAX_LINES
for line in lines:
assert (len(line) > pprint.MAX_WIDTH - 10 and
len(line) <= pprint.MAX_WIDTH)
def test_format1(self):
"""Basic test of formatting"""
lines = self.tb.pformat(max_lines=8, max_width=40)
assert lines == [' col0 col1 ... col19 ',
'------------ ----------- ... ------',
'0.000000e+00 1.000000 ... 19.0',
'2.000000e+01 21.000000 ... 39.0',
'4.000000e+01 41.000000 ... 59.0',
' ... ... ... ...',
'1.960000e+03 1961.000000 ... 1979.0',
'1.980000e+03 1981.000000 ... 1999.0']
def test_format2(self):
"""Include the units header row"""
lines = self.tb.pformat(max_lines=8, max_width=40, show_units=True)
assert lines == [' col0 ... col19 ',
' km**2 ... kg sec m**-2',
'------------ ... ------------',
'0.000000e+00 ... 19.0',
'2.000000e+01 ... 39.0',
' ... ... ...',
'1.960000e+03 ... 1979.0',
'1.980000e+03 ... 1999.0']
def test_format3(self):
"""Do not include the name header row"""
lines = self.tb.pformat(max_lines=8, max_width=40, show_name=False)
assert lines == ['0.000000e+00 1.000000 ... 19.0',
'2.000000e+01 21.000000 ... 39.0',
'4.000000e+01 41.000000 ... 59.0',
'6.000000e+01 61.000000 ... 79.0',
' ... ... ... ...',
'1.940000e+03 1941.000000 ... 1959.0',
'1.960000e+03 1961.000000 ... 1979.0',
'1.980000e+03 1981.000000 ... 1999.0']
def test_noclip(self):
"""Basic table print"""
lines = self.ts.pformat(max_lines=-1, max_width=-1)
assert lines == ['col0 col1 col2',
'---- ---- ----',
' 0 1 2',
' 3 4 5',
' 6 7 8',
' 9 10 11']
def test_clip1(self):
"""max lines below hard limit of 6
"""
lines = self.ts.pformat(max_lines=3, max_width=-1)
assert lines == ['col0 col1 col2',
'---- ---- ----',
' 0 1 2',
' 3 4 5',
' 6 7 8',
' 9 10 11']
def test_clip2(self):
"""max lines below hard limit of 6 and output longer than 6
"""
lines = self.ts.pformat(max_lines=3, max_width=-1, show_units=True)
assert lines == ['col0 col1 col2',
' ',
'---- ---- ----',
' 0 1 2',
' ... ... ...',
' 9 10 11']
def test_clip3(self):
"""Max lines below hard limit of 6 and max width below hard limit
of 10
"""
lines = self.ts.pformat(max_lines=3, max_width=1, show_units=True)
assert lines == ['col0 ...',
' ...',
'---- ...',
' 0 ...',
' ... ...',
' 9 ...']
def test_clip4(self):
"""Test a range of max_lines"""
for max_lines in range(130):
lines = self.tb.pformat(max_lines=max_lines)
assert len(lines) == max(6, min(102, max_lines))
class TestFormat():
def test_column_format(self):
t = Table([[1, 2], [3, 4]], names=('a', 'b'))
# default (format=None)
assert str(t['a']) == ' a \n---\n 1\n 2'
# Old-style that is almost new-style
t['a'].format = '{ %4.2f }'
assert str(t['a']) == ' a \n--------\n{ 1.00 }\n{ 2.00 }'
# New-style that is almost old-style
t['a'].format = '%{0:}'
assert str(t['a']) == ' a \n---\n %1\n %2'
# New-style with extra spaces
t['a'].format = ' {0:05d} '
assert str(t['a']) == ' a \n-------\n 00001 \n 00002 '
# New-style has precedence
t['a'].format = '%4.2f {0:}'
assert str(t['a']) == ' a \n-------\n%4.2f 1\n%4.2f 2'
# Invalid format spec
t['a'].format = 'fail'
with pytest.raises(ValueError):
str(t['a'])
def test_column_format_with_threshold(self):
import astropy.table.pprint
MAX_LINES = pprint.MAX_LINES
pprint.MAX_LINES = 6
t = Table([np.arange(20)], names=['a'])
t['a'].format = '%{0:}'
assert str(t['a']) == ' a \n---\n %0\n %1\n...\n%19'
t['a'].format = '{ %4.2f }'
assert str(t['a']) == ' a \n---------\n { 0.00 }\n' \
' { 1.00 }\n ...\n{ 19.00 }'
pprint.MAX_LINES = MAX_LINES