Representations of objects#

class sage.repl.display.fancy_repr.LargeMatrixHelpRepr[source]#

Bases: ObjectReprABC

Representation including help for large Sage matrices

__call__(obj, p, cycle)[source]#

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: # needs sage.modules
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...)"
sage: pp.format_string([M, M])
'--- object not handled by representer ---'
>>> from sage.all import *
>>> # needs sage.modules
>>> from sage.repl.display.fancy_repr import LargeMatrixHelpRepr
>>> M = identity_matrix(Integer(40))
>>> pp = LargeMatrixHelpRepr()
>>> pp.format_string(M)
"40 x 40 dense matrix over Integer Ring (use the '.str()' method...)"
>>> pp.format_string([M, M])
'--- object not handled by representer ---'

Leads to:

sage: M                                                                     # needs sage.modules
40 x 40 dense matrix over Integer Ring (use the '.str()' method...)
sage: [M, M]                                                                # needs sage.modules
[40 x 40 dense matrix over Integer Ring,
 40 x 40 dense matrix over Integer Ring]
>>> from sage.all import *
>>> M                                                                     # needs sage.modules
40 x 40 dense matrix over Integer Ring (use the '.str()' method...)
>>> [M, M]                                                                # needs sage.modules
[40 x 40 dense matrix over Integer Ring,
 40 x 40 dense matrix over Integer Ring]
class sage.repl.display.fancy_repr.ObjectReprABC[source]#

Bases: object

The abstract base class of an object representer.

__call__(obj, p, cycle)[source]#

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'
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import ObjectReprABC
>>> ObjectReprABC().format_string(Integer(123))   # indirect doctest
'Error: ObjectReprABC.__call__ is abstract'
format_string(obj)[source]#

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'
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import ObjectReprABC
>>> ObjectReprABC().format_string(Integer(123))
'Error: ObjectReprABC.__call__ is abstract'
class sage.repl.display.fancy_repr.PlainPythonRepr[source]#

Bases: ObjectReprABC

The ordinary Python representation

__call__(obj, p, cycle)[source]#

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))
"<class 'sage.rings.integer.Integer'>"
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import PlainPythonRepr
>>> pp = PlainPythonRepr()
>>> pp.format_string(type(Integer(1)))
"<class '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():
....:     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
 ]
>>> from sage.all import *
>>> class Newline():
...     def __repr__(self):
...         return 'newline\n'
>>> n = Newline()
>>> pp.format_string(n)
'newline\n'
>>> pp.format_string([n, n, n])
'[newline\n, newline\n, newline\n]'
>>> [n, n, n]
[newline
 , newline
 , newline
 ]
class sage.repl.display.fancy_repr.SomeIPythonRepr[source]#

Bases: ObjectReprABC

Some selected representers from IPython

EXAMPLES:

sage: from sage.repl.display.fancy_repr import SomeIPythonRepr
sage: SomeIPythonRepr()
SomeIPythonRepr pretty printer
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import SomeIPythonRepr
>>> SomeIPythonRepr()
SomeIPythonRepr pretty printer
__call__(obj, p, cycle)[source]#

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}'
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import SomeIPythonRepr
>>> pp = SomeIPythonRepr()
>>> pp.format_string(set([Integer(1), Integer(2), Integer(3)]))
'{1, 2, 3}'
class sage.repl.display.fancy_repr.TallListRepr[source]#

Bases: ObjectReprABC

Special representation for lists with tall entries (e.g. matrices)

__call__(obj, p, cycle)[source]#

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)])                               # needs sage.modules
'[\n      [1 0]\n1, 2, [0 1]\n]'
>>> from sage.all import *
>>> from sage.repl.display.fancy_repr import TallListRepr
>>> format_list = TallListRepr().format_string
>>> format_list([Integer(1), Integer(2), identity_matrix(Integer(2))])                               # needs sage.modules
'[\n      [1 0]\n1, 2, [0 1]\n]'

Check that Issue #18743 is fixed:

sage: class Foo():
....:     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
]
>>> from sage.all import *
>>> class Foo():
...     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'
>>> F = Foo()
>>> [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
]