def __call__(self, obj, p, cycle): 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]' """ if not (isinstance(obj, (tuple, list)) and len(obj) > 0): return False ascii_art_repr = False for o in obj: try: ascii_art_repr = ascii_art_repr or o.parent()._repr_option( 'element_ascii_art') except (AttributeError, TypeError): pass if not ascii_art_repr: return False output = format_list.try_format(obj) if output is None: return False p.text(output) return True
def __call__(self, obj, p, cycle): 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]' """ if not (isinstance(obj, (tuple, list)) and len(obj) > 0): return False ascii_art_repr = False for o in obj: try: ascii_art_repr = ascii_art_repr or o.parent()._repr_option('element_ascii_art') except (AttributeError, TypeError): pass if not ascii_art_repr: return False output = format_list.try_format(obj) if output is None: return False p.text(output) return True
def __call__(self, obj, p, cycle): 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 ] """ if not (isinstance(obj, (tuple, list)) and len(obj) > 0): return False ascii_art_repr = False for o in obj: try: ascii_art_repr = ascii_art_repr or o._repr_option("ascii_art") except (AttributeError, TypeError): pass try: ascii_art_repr = ascii_art_repr or o.parent()._repr_option("element_ascii_art") except (AttributeError, TypeError): pass if not ascii_art_repr: return False output = format_list.try_format(obj) if output is None: return False p.text(output) return True