File: pprint.py

package info (click to toggle)
python-astropy 1.3-8~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 44,292 kB
  • sloc: ansic: 160,360; python: 137,322; sh: 11,493; lex: 7,638; yacc: 4,956; xml: 1,796; makefile: 474; cpp: 364
file content (716 lines) | stat: -rw-r--r-- 27,306 bytes parent folder | download | duplicates (2)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
from ..extern import six
from ..extern.six import text_type
from ..extern.six.moves import zip, range

import os
import sys
import re

import numpy as np

from .. import log
from ..utils.console import Getch, color_print, terminal_size, conf
from ..utils.data_info import dtype_info_name

__all__ = []


def default_format_func(format_, val):
    if isinstance(val, bytes):
        return val.decode('utf-8')
    else:
        return text_type(val)


_format_funcs = {}


### The first three functions are helpers for _auto_format_func


def _use_str_for_masked_values(format_func):
    """Wrap format function to trap masked values.

    String format functions and most user functions will not be able to deal
    with masked values, so we wrap them to ensure they are passed to str().
    """
    return lambda format_, val: (str(val) if val is np.ma.masked
                                 else format_func(format_, val))

def _possible_string_format_functions(format_):
    """Iterate through possible string-derived format functions.

    A string can either be a format specifier for the format built-in,
    a new-style format string, or an old-style format string.
    """
    yield lambda format_, val: format(val, format_)
    yield lambda format_, val: format_.format(val)
    yield lambda format_, val: format_ % val

def get_auto_format_func(
        col_name=None,
        possible_string_format_functions=_possible_string_format_functions):
    """
    Return a wrapped ``auto_format_func`` function which is used in
    formatting table columns.  This is primarily an internal function but
    gets used directly in other parts of astropy, e.g. `astropy.io.ascii`.

    Parameters
    ----------
    col_name : str, optional
        Column name (default=None)

    possible_string_format_functions : func, optional
        Function that yields possible string formatting functions
        (default=internal function to do this).

    Returns
    -------
    Wrapped ``auto_format_func`` function
    """

    def _auto_format_func(format_, val):
        """Format ``val`` according to ``format_`` for a plain format specifier,
        old- or new-style format strings, or using a user supplied function.
        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.
        """
        if format_ is None:
            return default_format_func(format_, val)

        format_key = (format_, col_name)
        if format_key in _format_funcs:
            return _format_funcs[format_key](format_, val)

        if six.callable(format_):
            format_func = lambda format_, val: format_(val)
            try:
                out = format_func(format_, val)
                if not isinstance(out, six.string_types):
                    raise ValueError('Format function for value {0} returned {1} '
                                     'instead of string type'
                                     .format(val, type(val)))
            except Exception as err:
                # For a masked element, the format function call likely failed
                # to handle it.  Just return the string representation for now,
                # and retry when a non-masked value comes along.
                if val is np.ma.masked:
                    return str(val)

                raise ValueError('Format function for value {0} failed: {1}'
                                 .format(val, err))
            # If the user-supplied function handles formatting masked elements, use
            # it directly.  Otherwise, wrap it in a function that traps them.
            try:
                format_func(format_, np.ma.masked)
            except Exception:
                format_func = _use_str_for_masked_values(format_func)
        else:
            # For a masked element, we cannot set string-based format functions yet,
            # as all tests below will fail.  Just return the string representation
            # of masked for now, and retry when a non-masked value comes along.
            if val is np.ma.masked:
                return str(val)

            for format_func in possible_string_format_functions(format_):
                try:
                    # Does this string format method work?
                    out = format_func(format_, val)
                    # Require that the format statement actually did something.
                    assert out != format_
                except Exception:
                    continue
                else:
                    break
            else:
                # None of the possible string functions passed muster.
                raise ValueError('Unable to parse format string {0}'
                                 .format(format_))

            # String-based format functions will fail on masked elements;
            # wrap them in a function that traps them.
            format_func = _use_str_for_masked_values(format_func)

        _format_funcs[format_key] = format_func
        return out

    return _auto_format_func


class TableFormatter(object):
    @staticmethod
    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 the default will be determined
        using the ``astropy.table.conf.max_lines`` configuration item. If a
        negative value of ``max_lines`` is supplied then there is no line
        limit applied.

        The same applies for max_width except the configuration item is
        ``astropy.table.conf.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:
            max_lines = conf.max_lines

        if max_width is None:
            max_width = conf.max_width

        if max_lines is None or max_width is None:
            lines, width = terminal_size()

        if max_lines is None:
            max_lines = lines
        elif max_lines < 0:
            max_lines = sys.maxsize
        if max_lines < 8:
            max_lines = 8

        if max_width is None:
            max_width = width
        elif max_width < 0:
            max_width = sys.maxsize
        if max_width < 10:
            max_width = 10

        return max_lines, max_width

    def _pformat_col(self, col, max_lines=None, show_name=True, show_unit=None,
                     show_dtype=False, show_length=None, html=False, align=None):
        """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_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include column dtype (default=False)

        show_length : bool
            Include column length at end.  Default is to show this only
            if the column is not shown completely.

        html : bool
            Output column as HTML

        align : str
            Left/right alignment of columns. Default is '>' (right) for all
            columns. Other allowed values are '<', '^', and '0=' for left,
            centered, and 0-padded, respectively.

        Returns
        -------
        lines : list
            List of lines with formatted column values

        outs : dict
            Dict which is used to pass back additional values
            defined within the iterator.

        """
        if show_unit is None:
            show_unit = col.info.unit is not None

        outs = {}  # Some values from _pformat_col_iter iterator that are needed here
        col_strs_iter = self._pformat_col_iter(col, max_lines, show_name=show_name,
                                               show_unit=show_unit,
                                               show_dtype=show_dtype,
                                               show_length=show_length,
                                               outs=outs)
        col_strs = list(col_strs_iter)
        if len(col_strs) > 0:
            col_width = max(len(x) for x in col_strs)

        if html:
            from ..utils.xml.writer import xml_escape
            n_header = outs['n_header']
            for i, col_str in enumerate(col_strs):
                # _pformat_col output has a header line '----' which is not needed here
                if i == n_header - 1:
                    continue
                td = 'th' if i < n_header else 'td'
                val = '<{0}>{1}</{2}>'.format(td, xml_escape(col_str.strip()), td)
                row = ('<tr>' + val + '</tr>')
                if i < n_header:
                    row = ('<thead>' + row + '</thead>')
                col_strs[i] = row

            if n_header > 0:
                # Get rid of '---' header line
                col_strs.pop(n_header - 1)
            col_strs.insert(0, '<table>')
            col_strs.append('</table>')

        # Now bring all the column string values to the same fixed width
        else:
            col_width = max(len(x) for x in col_strs) if col_strs else 1

            # Center line header content and generate dashed headerline
            for i in outs['i_centers']:
                col_strs[i] = col_strs[i].center(col_width)
            if outs['i_dashes'] is not None:
                col_strs[outs['i_dashes']] = '-' * col_width

            # Format columns according to alignment.  `align` arg has precedent, otherwise
            # use `col.format` if it starts as a legal alignment string.  If neither applies
            # then right justify.
            re_fill_align = re.compile(r'(?P<fill>.?)(?P<align>[<^>=])')
            match = None
            if align:
                # If there is an align specified then it must match
                match = re_fill_align.match(align)
                if not match:
                    raise ValueError("column align must be one of '<', '^', '>', or '='")
            elif isinstance(col.info.format, six.string_types):
                # col.info.format need not match, in which case rjust gets used
                match = re_fill_align.match(col.info.format)

            if match:
                fill_char = match.group('fill')
                align_char = match.group('align')
                if align_char == '=':
                    if fill_char != '0':
                        raise ValueError("fill character must be '0' for '=' align")
                    fill_char = ''  # str.zfill gets used which does not take fill char arg
            else:
                fill_char = ''
                align_char = '>'

            justify_methods = {'<': 'ljust', '^': 'center', '>': 'rjust', '=': 'zfill'}
            justify_method = justify_methods[align_char]
            justify_args = (col_width, fill_char) if fill_char else (col_width,)

            for i, col_str in enumerate(col_strs):
                col_strs[i] = getattr(col_str, justify_method)(*justify_args)

        if outs['show_length']:
            col_strs.append('Length = {0} rows'.format(len(col)))

        return col_strs, outs

    def _pformat_col_iter(self, col, max_lines, show_name, show_unit, outs,
                          show_dtype=False, show_length=None):
        """Iterator which yields 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_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        outs : dict
            Must be a dict which is used to pass back additional values
            defined within the iterator.

        show_dtype : bool
            Include column dtype (default=False)

        show_length : bool
            Include column length at end.  Default is to show this only
            if the column is not shown completely.
        """
        max_lines, _ = self._get_pprint_size(max_lines, -1)

        multidims = getattr(col, 'shape', [0])[1:]
        if multidims:
            multidim0 = tuple(0 for n in multidims)
            multidim1 = tuple(n - 1 for n in multidims)
            trivial_multidims = np.prod(multidims) == 1

        i_dashes = None
        i_centers = []  # Line indexes where content should be centered
        n_header = 0
        if show_name:
            i_centers.append(n_header)
            # Get column name (or 'None' if not set)
            col_name = six.text_type(col.info.name)
            if multidims:
                col_name += ' [{0}]'.format(
                    ','.join(six.text_type(n) for n in multidims))
            n_header += 1
            yield col_name
        if show_unit:
            i_centers.append(n_header)
            n_header += 1
            yield six.text_type(col.info.unit or '')
        if show_dtype:
            i_centers.append(n_header)
            n_header += 1
            try:
                dtype = dtype_info_name(col.dtype)
            except AttributeError:
                dtype = 'object'
            yield six.text_type(dtype)
        if show_unit or show_name or show_dtype:
            i_dashes = n_header
            n_header += 1
            yield '---'

        max_lines -= n_header
        n_print2 = max_lines // 2
        n_rows = len(col)

        # This block of code is responsible for producing the function that
        # will format values for this column.  The ``format_func`` function
        # takes two args (col_format, val) and returns the string-formatted
        # version.  Some points to understand:
        #
        # - col_format could itself be the formatting function, so it will
        #    actually end up being called with itself as the first arg.  In
        #    this case the function is expected to ignore its first arg.
        #
        # - auto_format_func is a function that gets called on the first
        #    column value that is being formatted.  It then determines an
        #    appropriate formatting function given the actual value to be
        #    formatted.  This might be deterministic or it might involve
        #    try/except.  The latter allows for different string formatting
        #    options like %f or {:5.3f}.  When auto_format_func is called it:

        #    1. Caches the function in the _format_funcs dict so for subsequent
        #       values the right function is called right away.
        #    2. Returns the formatted value.
        #
        # - possible_string_format_functions is a function that yields a
        #    succession of functions that might successfully format the
        #    value.  There is a default, but Mixin methods can override this.
        #    See Quantity for an example.
        #
        # - get_auto_format_func() returns a wrapped version of auto_format_func
        #    with the column name and possible_string_format_functions as
        #    enclosed variables.
        col_format = col.info.format or getattr(col.info, 'default_format', None)
        pssf = (getattr(col.info, 'possible_string_format_functions', None) or
                _possible_string_format_functions)
        auto_format_func = get_auto_format_func(col.info.name, pssf)
        format_key = (col_format, col.info.name)
        format_func = _format_funcs.get(format_key, auto_format_func)

        if len(col) > max_lines:
            if show_length is None:
                show_length = True
            i0 = n_print2 - (1 if show_length else 0)
            i1 = n_rows - n_print2 - max_lines % 2
            ii = np.concatenate([np.arange(0, i0 + 1), np.arange(i1 + 1, len(col))])
        else:
            i0 = -1
            ii = np.arange(len(col))

        # Add formatted values if within bounds allowed by max_lines
        for i in ii:
            if i == i0:
                yield '...'
            else:
                if multidims:
                    # Prevents columns like Column(data=[[(1,)],[(2,)]], name='a')
                    # with shape (n,1,...,1) from being printed as if there was
                    # more than one element in a row
                    if trivial_multidims:
                        col_str = format_func(col_format, col[(i,) + multidim0])
                    else:
                        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])
                yield col_str

        outs['show_length'] = show_length
        outs['n_header'] = n_header
        outs['i_centers'] = i_centers
        outs['i_dashes'] = i_dashes

    def _pformat_table(self, table, max_lines=None, max_width=None,
                       show_name=True, show_unit=None, show_dtype=False,
                       html=False, tableid=None, tableclass=None, align=None):
        """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_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include a header row for column dtypes (default=False)

        html : bool
            Format the output as an HTML table (default=False)

        tableid : str or None
            An ID tag for the table; only used if html is set.  Default is
            "table{id}", where id is the unique integer id of the table object,
            id(table)

        tableclass : str or list of str or `None`
            CSS classes for the table; only used if html is set.  Default is
            none

        align : str or list or tuple
            Left/right alignment of columns. Default is '>' (right) for all
            columns. Other allowed values are '<', '^', and '0=' for left,
            centered, and 0-padded, respectively. A list of strings can be
            provided for alignment of tables with multiple columns.

        Returns
        -------
        rows : list
            Formatted table as a list of strings

        outs : dict
            Dict which is used to pass back additional values
            defined within the iterator.

        """
        # "Print" all the values into temporary lists by column for subsequent
        # use and to determine the width
        max_lines, max_width = self._get_pprint_size(max_lines, max_width)
        cols = []

        if show_unit is None:
            show_unit = any(col.info.unit for col in six.itervalues(table.columns))

        # Coerce align into a correctly-sized list of alignments (if possible)
        n_cols = len(table.columns)
        if align is None or isinstance(align, six.string_types):
            align = [align] * n_cols

        elif isinstance(align, (list, tuple)):
            if len(align) != n_cols:
                raise ValueError('got {0} alignment values instead of '
                                 'the number of columns ({1})'
                                 .format(len(align), n_cols))
        else:
            raise TypeError('align keyword must be str or list or tuple (got {0})'
                            .format(type(align)))

        for align_, col in zip(align, table.columns.values()):
            lines, outs = self._pformat_col(col, max_lines, show_name=show_name,
                                            show_unit=show_unit, show_dtype=show_dtype,
                                            align=align_)
            if outs['show_length']:
                lines = lines[:-1]
            cols.append(lines)

        if not cols:
            return ['<No columns>'], {'show_length': False}

        # Use the values for the last column since they are all the same
        n_header = outs['n_header']

        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 = []
        if html:
            from ..utils.xml.writer import xml_escape

            if tableid is None:
                tableid = 'table{id}'.format(id=id(table))

            if tableclass is not None:
                if isinstance(tableclass, list):
                    tableclass = ' '.join(tableclass)
                rows.append('<table id="{tid}" class="{tcls}">'.format(
                    tid=tableid, tcls=tableclass))
            else:
                rows.append('<table id="{tid}">'.format(tid=tableid))

            for i in range(n_rows):
                # _pformat_col output has a header line '----' which is not needed here
                if i == n_header - 1:
                    continue
                td = 'th' if i < n_header else 'td'
                vals = ('<{0}>{1}</{2}>'.format(td, xml_escape(col[i].strip()), td)
                        for col in cols)
                row = ('<tr>' + ''.join(vals) + '</tr>')
                if i < n_header:
                    row = ('<thead>' + row + '</thead>')
                rows.append(row)
            rows.append('</table>')
        else:
            for i in range(n_rows):
                row = ' '.join(col[i] for col in cols)
                rows.append(row)

        return rows, outs

    def _more_tabcol(self, tabcol, max_lines=None, max_width=None,
                     show_name=True, show_unit=None, show_dtype=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_unit : bool
            Include a header row for unit.  Default is to show a row
            for units only if one or more columns has a defined value
            for the unit.

        show_dtype : bool
            Include a header row for column dtypes (default=False)
        """
        allowed_keys = 'f br<>qhpn'

        # Count the header lines
        n_header = 0
        if show_name:
            n_header += 1
        if show_unit:
            n_header += 1
        if show_dtype:
            n_header += 1
        if show_name or show_unit or show_dtype:
            n_header += 1

        # Set up kwargs for pformat call.  Only Table gets max_width.
        kwargs = dict(max_lines=-1, show_name=show_name, show_unit=show_unit,
                      show_dtype=show_dtype)
        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 = self._get_pprint_size(max_lines, max_width)
        if max_lines is 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 Exception:
                    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 range(len(lines)))
                for color, line in zip(colors, lines):
                    color_print(line, color)
            showlines = True
            print()
            print("-- f, <space>, b, r, p, n, <, >, q h (help) --", end=' ')
            # Get a valid key
            while True:
                try:
                    key = inkey().lower()
                except Exception:
                    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""", end=' ')
            if i0 < 0:
                i0 = 0
            if i0 >= len(tabcol) - delta_lines:
                i0 = len(tabcol) - delta_lines
            print("\n")