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 -*- Representations of objects. """
#***************************************************************************** # 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/ #*****************************************************************************
_safe_getattr, _baseclass_reprs, _type_pprinters, )
""" The abstract base class of an object representer.
.. automethod:: __call__ """
""" Return string representation.
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import ObjectReprABC sage: ObjectReprABC() ObjectReprABC pretty printer """
r""" Format object.
INPUT:
- ``obj`` -- anything. Object to format.
- ``p`` -- PrettyPrinter instance.
- ``cycle`` -- boolean. Whether there is a cycle.
OUTPUT:
Boolean. Whether the representer is applicable to ``obj``. If ``True``, the string representation is appended to ``p``.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import ObjectReprABC sage: ObjectReprABC().format_string(123) # indirect doctest 'Error: ObjectReprABC.__call__ is abstract' """
""" For doctesting only: Directly return string.
INPUT:
- ``obj`` -- anything. Object to format.
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import ObjectReprABC sage: ObjectReprABC().format_string(123) 'Error: ObjectReprABC.__call__ is abstract' """ else:
""" Some selected representers from IPython
EXAMPLES::
sage: from sage.repl.display.fancy_repr import SomeIPythonRepr sage: SomeIPythonRepr() SomeIPythonRepr pretty printer
.. automethod:: __call__ """
""" Format object.
INPUT:
- ``obj`` -- anything. Object to format.
- ``p`` -- PrettyPrinter instance.
- ``cycle`` -- boolean. Whether there is a cycle.
OUTPUT:
Boolean. Whether the representer is applicable to ``obj``. If ``True``, the string representation is appended to ``p``.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import SomeIPythonRepr sage: pp = SomeIPythonRepr() sage: pp.format_string(set([1, 2, 3])) '{1, 2, 3}' """
""" Representation including help for large Sage matrices
.. automethod:: __call__ """
r""" Format matrix.
INPUT:
- ``obj`` -- anything. Object to format.
- ``p`` -- PrettyPrinter instance.
- ``cycle`` -- boolean. Whether there is a cycle.
OUTPUT:
Boolean. Whether the representer is applicable to ``obj``. If ``True``, the string representation is appended to ``p``.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import LargeMatrixHelpRepr sage: M = identity_matrix(40) sage: pp = LargeMatrixHelpRepr() sage: pp.format_string(M) "40 x 40 dense matrix over Integer Ring (use the '.str()' method to see the entries)" sage: pp.format_string([M, M]) '--- object not handled by representer ---'
Leads to::
sage: M 40 x 40 dense matrix over Integer Ring (use the '.str()' method to see the entries) sage: [M, M] [40 x 40 dense matrix over Integer Ring, 40 x 40 dense matrix over Integer Ring] """ # Do not print the help for matrices inside containers repr(obj) + " (use the '.str()' method to see the entries)" )
""" The ordinary Python representation
.. automethod:: __call__ """
r""" Format matrix.
INPUT:
- ``obj`` -- anything. Object to format.
- ``p`` -- PrettyPrinter instance.
- ``cycle`` -- boolean. Whether there is a cycle.
OUTPUT:
Boolean. Whether the representer is applicable to ``obj``. If ``True``, the string representation is appended to ``p``.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import PlainPythonRepr sage: pp = PlainPythonRepr() sage: pp.format_string(type(1)) "<type 'sage.rings.integer.Integer'>"
Do not swallow a trailing newline at the end of the output of a custom representer. Note that it is undesirable to have a trailing newline, and if we don't display it you can't fix it::
sage: class Newline(object): ....: def __repr__(self): ....: return 'newline\n' sage: n = Newline() sage: pp.format_string(n) 'newline\n' sage: pp.format_string([n, n, n]) '[newline\n, newline\n, newline\n]' sage: [n, n, n] [newline , newline , newline ] """ else: # A user-provided repr. Find newlines and replace them with p.break_()
""" Special representation for lists with tall entries (e.g. matrices)
.. automethod:: __call__ """
r""" Format list/tuple.
INPUT:
- ``obj`` -- anything. Object to format.
- ``p`` -- PrettyPrinter instance.
- ``cycle`` -- boolean. Whether there is a cycle.
OUTPUT:
Boolean. Whether the representer is applicable to ``obj``. If ``True``, the string representation is appended to ``p``.
EXAMPLES::
sage: from sage.repl.display.fancy_repr import TallListRepr sage: format_list = TallListRepr().format_string sage: format_list([1, 2, identity_matrix(2)]) '[\n [1 0]\n1, 2, [0 1]\n]'
Check that :trac:`18743` is fixed::
sage: class Foo(object): ....: def __repr__(self): ....: return '''BBB AA RRR ....: B B A A R R ....: BBB AAAA RRR ....: B B A A R R ....: BBB A A R R''' ....: def _repr_option(self, key): ....: return key == 'ascii_art' sage: F = Foo() sage: [F, F] [ BBB AA RRR BBB AA RRR B B A A R R B B A A R R BBB AAAA RRR BBB AAAA RRR B B A A R R B B A A R R BBB A A R R, BBB A A R R ] """
|