Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- Utility functions for pretty-printing
These utility functions are used in the implementations of ``_repr_`` methods elsewhere. """
#***************************************************************************** # Copyright (C) 2014 Volker Braun <vbraun.name@gmail.com> # # Distributed under the terms of the GNU General Public License (GPL) # as published by the Free Software Foundation; either version 2 of # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #*****************************************************************************
""" Special representation for lists with tall entries (e.g. matrices)
.. automethod:: __call__ """
# This is used to wrap lines when printing "tall" lists.
""" Helper for :meth:`_check_tall_list_and_format`
This helper function processes and outputs the contents of the running_lines array.
TESTS::
sage: from sage.repl.display.util import format_list sage: format_list._tall_list_row(['a b', 'b c', 'c']) ['a b', 'b c', 'c,', ''] """ else: # The commas go on the bottom line of this row. # Separate rows with a newline to make them stand out.
""" First check whether a list is "tall" -- whether the reprs of the elements of the list will span multiple lines and cause the list to be printed awkwardly. If not, this function returns ``None`` and does nothing; you should revert back to the normal method for printing an object (its repr). If so, return the string in the special format. Note that the special format isn't just for matrices. Any object with a multiline repr will be formatted.
INPUT:
- ``the_list`` - The list (or a tuple).
OUTPUT:
String or ``None``. The latter is returned if the list is not deemed to be tall enough and another formatter should be used.
TESTS::
sage: from sage.repl.display.util import format_list sage: print(format_list.try_format( ....: [matrix([[1, 2, 3, 4], [5, 6, 7, 8]]) for i in range(7)])) [ [1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4] [5 6 7 8], [5 6 7 8], [5 6 7 8], [5 6 7 8], [5 6 7 8], [5 6 7 8], <BLANKLINE> [1 2 3 4] [5 6 7 8] ]
sage: format_list.try_format(['not', 'tall']) is None True """ # For every object to be printed, split its repr on newlines and store the # result in this list. # Meanwhile, check to make sure the list is actually "tall". # Figure out which type of parenthesis to use, based on the type of the_list. else: raise TypeError('expected list or tuple')
# running_lines is a list of lines, which are stored as lists of strings # to be joined later. For each split repr, we add its lines to the # running_lines array. When current_column exceeds MAX_COLUMN, process # and output running_lines using _print_tall_list_row. # Add the lines from split_repr to the running_lines array. It may # be necessary to add or remove lines from either one so that the # number of lines matches up. # Output any remaining entries.
""" Return "tall list" string representation.
See also :meth:`try_format`.
INPUT:
- ``the_list`` -- list or tuple.
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.util import format_list sage: format_list(['not', 'tall']) "['not', 'tall']" """
|