Esempio n. 1
0
    def __call__(self, obj, p, cycle):
        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
             ]
        """
        klass = _safe_getattr(obj, '__class__', None) or type(obj)
        klass_repr = _safe_getattr(klass, '__repr__', None)
        if klass_repr in _baseclass_reprs:
            p.text(klass_repr(obj))
        else:
            # A user-provided repr. Find newlines and replace them with p.break_()
            output = _safe_repr(obj)
            for idx, output_line in enumerate(output.split('\n')):
                if idx:
                    p.break_()
                p.text(output_line)
        return True
Esempio n. 2
0
    def __call__(self, obj, p, cycle):
        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
             ]
        """
        klass = _safe_getattr(obj, '__class__', None) or type(obj)
        klass_repr = _safe_getattr(klass, '__repr__', None)
        if klass_repr in _baseclass_reprs:
            p.text(klass_repr(obj))
        else:
            # A user-provided repr. Find newlines and replace them with p.break_()
            output = _safe_repr(obj)
            for idx, output_line in enumerate(output.split('\n')):
                if idx:
                    p.break_()
                p.text(output_line)
        return True
Esempio n. 3
0
def warn_format_error(method, self, *args, **kwargs):
    """decorator for warning on failed format call"""
    try:
        r = method(self, *args, **kwargs)
    except Exception as e:
        warn("Exception in %s formatter: %s" % (self.format_type, e))
        return None
    if r is None or isinstance(r, self._return_type) or \
        (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
        return r
    else:
        warn("%s formatter returned invalid type %s (expected %s) for object: %s" % (
            self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])
        ))
Esempio n. 4
0
def warn_format_error(method, self, *args, **kwargs):
    """decorator for warning on failed format call"""
    try:
        r = method(self, *args, **kwargs)
    except Exception as e:
        warn("Exception in %s formatter: %s" % (self.format_type, e))
        return None
    if r is None or isinstance(r, self._return_type) or \
        (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
        return r
    else:
        warn(
            "%s formatter returned invalid type %s (expected %s) for object: %s"
            % (self.format_type, type(r), self._return_type,
               pretty._safe_repr(args[0])))
Esempio n. 5
0
 def __call__(self, obj):
     """Compute the pretty representation of the object."""
     if not self.pprint:
         return pretty._safe_repr(obj)
     else:
         # This uses use StringIO, as cStringIO doesn't handle unicode.
         stream = StringIO()
         # self.newline.encode() is a quick fix for issue gh-597. We need to
         # ensure that stream does not get a mix of unicode and bytestrings,
         # or it will cause trouble.
         printer = pretty.RepresentationPrinter(stream, self.verbose,
             self.max_width, unicode_to_str(self.newline),
             singleton_pprinters=self.singleton_printers,
             type_pprinters=self.type_printers,
             deferred_pprinters=self.deferred_printers)
         printer.pretty(obj)
         printer.flush()
         return stream.getvalue()
Esempio n. 6
0
 def __call__(self, obj):
     """Compute the pretty representation of the object."""
     if not self.pprint:
         return pretty._safe_repr(obj)
     else:
         # This uses use StringIO, as cStringIO doesn't handle unicode.
         stream = StringIO()
         # self.newline.encode() is a quick fix for issue gh-597. We need to
         # ensure that stream does not get a mix of unicode and bytestrings,
         # or it will cause trouble.
         printer = pretty.RepresentationPrinter(stream, self.verbose,
             self.max_width, unicode_to_str(self.newline),
             singleton_pprinters=self.singleton_printers,
             type_pprinters=self.type_printers,
             deferred_pprinters=self.deferred_printers)
         printer.pretty(obj)
         printer.flush()
         return stream.getvalue()