forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpprint.py
More file actions
364 lines (307 loc) · 10.7 KB
/
Copy pathpprint.py
File metadata and controls
364 lines (307 loc) · 10.7 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
import sys
from itertools import izip
from .. import log
from ..utils.console import Getch, color_print
_format_funcs = {None: lambda format_, val: str(val)}
MAX_LINES = 25
MAX_WIDTH = 80
def _get_pprint_size(max_lines=None, max_width=None):
"""Get the output size (number of lines and character width) for Column and
Table pformat/pprint methods.
If no value of ``max_lines`` is supplied then the height of the screen
terminal is used to set ``max_lines``. If the terminal height cannot be
determined then a default of ``astropy.table.pprint.MAX_LINES`` is used.
If a negative value of ``max_lines`` is supplied then there is no line
limit applied.
The Same applies for max_width except the default is
``astropy.table.pprint.MAX_WIDTH``.
Parameters
----------
max_lines : int or None
Maximum lines of output (header + data rows)
max_width : int or None
Maximum width (characters) output
Returns
-------
max_lines, max_width : int
"""
if max_lines is None or max_width is None:
try: # Will likely fail on Windows
import termios
import fcntl
import struct
s = struct.pack("HHHH", 0, 0, 0, 0)
fd_stdout = sys.stdout.fileno()
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
(lines, width, xpixels, ypixels) = struct.unpack("HHHH", x)
if lines > 12:
lines -= 6
if width > 10:
width -= 1
except:
lines, width = MAX_LINES, MAX_WIDTH
if max_lines is None:
max_lines = lines
elif max_lines < 0:
max_lines = sys.maxint
if max_lines < 6:
max_lines = 6
if max_width is None:
max_width = width
elif max_width < 0:
max_width = sys.maxint
if max_width < 10:
max_width = 10
return max_lines, max_width
def _auto_format_func(format_, val):
"""Format ``val`` according to ``format_`` for both old- and new-
style format specifications. More importantly, determine and cache
(in _format_funcs) a function that will do this subsequently. In
this way this complicated logic is only done for the first value.
Returns the formatted value.
"""
try:
# Convert val to Python object with tolist(). See
# https://github.com/astropy/astropy/issues/148#issuecomment-3930809
out = format_.format(val.tolist())
# Require that the format statement actually did something
if out == format_:
raise ValueError
format_func = lambda format_, val: format_.format(val.tolist())
except: # Not sure what exceptions might be raised
try:
out = format_ % val
if out == format_:
raise ValueError
format_func = lambda format_, val: format_ % val
except:
raise ValueError('Unable to parse format string {0}'
.format(format_))
_format_funcs[format_] = format_func
return out
def _pformat_col(col, max_lines=None, show_name=True, show_units=False):
"""Return a list of formatted string representation of column values.
Parameters
----------
max_lines : int
Maximum lines of output (header + data rows)
show_name : bool
Include column name (default=True)
show_units : bool
Include a header row for units (default=False)
Returns
-------
lines : list
List of lines with formatted column values
n_header : int
Number of lines in the header
"""
max_lines, _ = _get_pprint_size(max_lines, -1)
multidims = col.shape[1:]
if multidims:
multidim0 = tuple(0 for n in multidims)
multidim1 = tuple(n - 1 for n in multidims)
col_strs = [] # List of formatted column values
i_dashes = None
i_centers = [] # Line indexes where content should be centered
if show_name:
i_centers.append(len(col_strs))
if multidims:
col_name = col.name + ' [{0}]'.format(
','.join(str(n) for n in multidims))
else:
col_name = col.name
col_strs.append(col_name)
max_lines -= 1
if show_units:
i_centers.append(len(col_strs))
col_strs.append(col.units or '')
max_lines -= 1
if show_units or show_name:
i_dashes = len(col_strs)
col_strs.append('---')
max_lines -= 1
n_header = len(col_strs)
n_print2 = max_lines // 2
n_rows = len(col)
format_func = _format_funcs.get(col.format, _auto_format_func)
if len(col) > max_lines:
i0 = n_print2
i1 = n_rows - n_print2 - max_lines % 2
else:
i0 = len(col)
i1 = 0
# Add formatted values if within bounds allowed by max_lines
for i in xrange(n_rows):
if i < i0 or i > i1:
if multidims:
col_str = (format_func(col.format, col[(i,) + multidim0]) +
' .. ' +
format_func(col.format, col[(i,) + multidim1]))
else:
col_str = format_func(col.format, col[i])
col_strs.append(col_str)
elif i == i0:
col_strs.append('...')
col_width = max(len(x) for x in col_strs)
# Center line content and generate dashed headerline
for i in i_centers:
col_strs[i] = col_strs[i].center(col_width)
if i_dashes is not None:
col_strs[i_dashes] = '-' * col_width
# Now bring all the column string values to the same fixed width
for i, col_str in enumerate(col_strs):
col_strs[i] = col_str.rjust(col_width)
return col_strs, n_header
def _pformat_table(table, max_lines=None, max_width=None, show_name=True,
show_units=False):
"""Return a list of lines for the formatted string representation of
the table.
Parameters
----------
max_lines : int or None
Maximum number of rows to output
max_width : int or None
Maximum character width of output
show_name : bool
Include a header row for column names (default=True)
show_units : bool
Include a header row for units (default=False)
Returns
-------
out : str
Formatted table as a single string
n_header : int
Number of lines in the header
"""
# "Print" all the values into temporary lists by column for subsequent
# use and to determine the width
max_lines, max_width = _get_pprint_size(max_lines, max_width)
cols = []
for col in table.columns.values():
lines, n_header = _pformat_col(col, max_lines, show_name,
show_units)
cols.append(lines)
if not cols:
return []
n_rows = len(cols[0])
outwidth = lambda cols: sum(len(c[0]) for c in cols) + len(cols) - 1
dots_col = ['...'] * n_rows
middle = len(cols) // 2
while outwidth(cols) > max_width:
if len(cols) == 1:
break
if len(cols) == 2:
cols[1] = dots_col
break
if cols[middle] is dots_col:
cols.pop(middle)
middle = len(cols) // 2
cols[middle] = dots_col
# Now "print" the (already-stringified) column values into a
# row-oriented list.
rows = []
for i in range(n_rows):
row = ' '.join(col[i] for col in cols)
rows.append(row)
return rows, n_header
def _more_tabcol(tabcol, max_lines=None, max_width=None, show_name=True,
show_units=False):
"""Interactive "more" of a table or column.
Parameters
----------
max_lines : int or None
Maximum number of rows to output
max_width : int or None
Maximum character width of output
show_name : bool
Include a header row for column names (default=True)
show_units : bool
Include a header row for units (default=False)
"""
allowed_keys = 'f br<>qhpn'
# Count the header lines
n_header = 0
if show_name:
n_header += 1
if show_units:
n_header += 1
if show_name or show_units:
n_header += 1
# Set up kwargs for pformat call. Only Table gets max_width.
kwargs = dict(max_lines=-1, show_name=show_name, show_units=show_units)
if hasattr(tabcol, 'columns'): # tabcol is a table
kwargs['max_width'] = max_width
# If max_lines is None (=> query screen size) then increase by 2.
# This is because get_pprint_size leaves 6 extra lines so that in
# ipython you normally see the last input line.
max_lines1, max_width = _get_pprint_size(max_lines, max_width)
if max_lines == None:
max_lines1 += 2
delta_lines = max_lines1 - n_header
# Set up a function to get a single character on any platform
inkey = Getch()
i0 = 0 # First table/column row to show
showlines = True
while True:
i1 = i0 + delta_lines # Last table/col row to show
if showlines: # Don't always show the table (e.g. after help)
try:
os.system('cls' if os.name == 'nt' else 'clear')
except:
pass # No worries if clear screen call fails
lines = tabcol[i0:i1].pformat(**kwargs)
colors = ('red' if i < n_header else 'default'
for i in xrange(len(lines)))
for color, line in izip(colors, lines):
color_print(line, color)
showlines = True
print
print "-- f, <space>, b, r, p, n, <, >, q h (help) --",
# Get a valid key
while True:
try:
key = inkey().lower()
except:
print "\n"
log.error('Console does not support getting a character'
' as required by more(). Use pprint() instead.')
return
if key in allowed_keys:
break
print key
if key.lower() == 'q':
break
elif key == ' ' or key == 'f':
i0 += delta_lines
elif key == 'b':
i0 = i0 - delta_lines
elif key == 'r':
pass
elif key == '<':
i0 = 0
elif key == '>':
i0 = len(tabcol)
elif key == 'p':
i0 -= 1
elif key == 'n':
i0 += 1
elif key == 'h':
showlines = False
print """
Browsing keys:
f, <space> : forward one page
b : back one page
r : refresh same page
n : next row
p : previous row
< : go to beginning
> : go to end
q : quit browsing
h : print this help""",
if i0 < 0:
i0 = 0
if i0 >= len(tabcol) - delta_lines:
i0 = len(tabcol) - delta_lines
print "\n"