The Sage pretty printer#

Any transformation to a string goes through here. In other words, the SagePlainTextFormatter is entirely implemented via SagePrettyPrinter. Other formatters may or may not use SagePrettyPrinter to generate text output.

AUTHORS:

  • Bill Cauchois (2009): initial version

  • Jean-Baptiste Priez <jbp@kerios.fr> (2013): ASCII art

  • Volker Braun (2013): refactored into DisplayHookBase

class sage.repl.display.pretty_print.SagePrettyPrinter(output, max_width, newline, max_seq_length=None)[source]#

Bases: PrettyPrinter

Pretty print Sage objects for the commandline

INPUT:

See IPython documentation.

EXAMPLES:

sage: 123
123
>>> from sage.all import *
>>> Integer(123)
123

IPython pretty printers:

sage: set({1, 2, 3})
{1, 2, 3}
sage: dict(zzz=123, aaa=99, xab=10)    # sorted by keys
{'aaa': 99, 'xab': 10, 'zzz': 123}
>>> from sage.all import *
>>> set({Integer(1), Integer(2), Integer(3)})
{1, 2, 3}
>>> dict(zzz=Integer(123), aaa=Integer(99), xab=Integer(10))    # sorted by keys
{'aaa': 99, 'xab': 10, 'zzz': 123}

These are overridden in IPython in a way that we feel is somewhat confusing, and we prefer to print them like plain Python which is more informative. See Issue #14466

sage: 'this is a string'
'this is a string'
sage: type(123)
<class 'sage.rings.integer.Integer'>
sage: type
<... 'type'>
sage: import types
sage: type('name', (), {})
<class '__main__.name'>
sage: types.BuiltinFunctionType
<class 'builtin_function_or_method'>

sage: def foo(): pass
sage: foo
<function foo at 0x...>
>>> from sage.all import *
>>> 'this is a string'
'this is a string'
>>> type(Integer(123))
<class 'sage.rings.integer.Integer'>
>>> type
<... 'type'>
>>> import types
>>> type('name', (), {})
<class '__main__.name'>
>>> types.BuiltinFunctionType
<class 'builtin_function_or_method'>

>>> def foo(): pass
>>> foo
<function foo at 0x...>
DEBUG = False#
pretty(obj)[source]#

Pretty print obj

This is the only method that outside code should invoke.

INPUT:

  • obj – anything.

OUTPUT:

String representation for object.

EXAMPLES:

sage: from sage.repl.display.pretty_print import SagePrettyPrinter
sage: from io import StringIO
sage: stream = StringIO()
sage: SagePrettyPrinter(stream, 78, '\n').pretty([type, 123, 'foo'])
sage: stream.getvalue()
"[<... 'type'>,"
>>> from sage.all import *
>>> from sage.repl.display.pretty_print import SagePrettyPrinter
>>> from io import StringIO
>>> stream = StringIO()
>>> SagePrettyPrinter(stream, Integer(78), '\n').pretty([type, Integer(123), 'foo'])
>>> stream.getvalue()
"[<... 'type'>,"
pretty_repr = (TallListRepr pretty printer, LargeMatrixHelpRepr pretty printer, SomeIPythonRepr pretty printer, PlainPythonRepr pretty printer)#
toplevel()[source]#

Return whether we are currently at the top level.

OUTPUT:

Boolean. Whether we are currently pretty-printing an object at the outermost level (True), or whether the object is inside a container (False).

EXAMPLES:

sage: from sage.repl.display.pretty_print import SagePrettyPrinter
sage: from io import StringIO
sage: stream = StringIO()
sage: spp = SagePrettyPrinter(stream, 78, '\n')
sage: spp.toplevel()
True
>>> from sage.all import *
>>> from sage.repl.display.pretty_print import SagePrettyPrinter
>>> from io import StringIO
>>> stream = StringIO()
>>> spp = SagePrettyPrinter(stream, Integer(78), '\n')
>>> spp.toplevel()
True