def ascii_art_formatter(self, obj, **kwds): r""" Hook to override how ascii art is being formatted. INPUT: - ``obj`` -- anything. - ``**kwds`` -- optional keyword arguments to control the formatting. Supported are: * ``concatenate`` -- boolean (default: ``False``). If ``True``, the argument ``obj`` must be iterable and its entries will be concatenated. There is a single whitespace between entries. OUTPUT: Instance of :class:`~sage.repl.rich_output.output_basic.OutputAsciiArt` containing the ascii art string representation of the object. EXAMPLES:: sage: from sage.repl.rich_output.backend_base import BackendBase sage: backend = BackendBase() sage: out = backend.ascii_art_formatter(range(30)) sage: out OutputAsciiArt container sage: out.ascii_art buffer containing 228 bytes sage: print(out.ascii_art.get()) [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, <BLANKLINE> ] 22, 23, 24, 25, 26, 27, 28, 29 ] sage: backend.ascii_art_formatter([1,2,3], concatenate=False).ascii_art.get() '[ ]\n[ 1, 2, 3 ]' sage: backend.ascii_art_formatter([1,2,3], concatenate=True ).ascii_art.get() '1 2 3' """ from sage.misc.ascii_art import ascii_art, empty_ascii_art if kwds.get('concatenate', False): result = empty_ascii_art for o in obj: if result is not empty_ascii_art: result += ascii_art(' ') result += ascii_art(o) else: result = ascii_art(obj) from sage.repl.rich_output.output_basic import OutputAsciiArt return OutputAsciiArt(str(result))
def __call__(self, obj, p, cycle): r""" Return ascii art format. 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 AsciiArtRepr sage: pp = AsciiArtRepr() sage: pp.format_string(x/2) 'x\n-\n2' """ from sage.misc.ascii_art import ascii_art output = ascii_art(obj) p.text(output) return True
def try_format_obj(self, obj): """ Format non-graphics object. OUTPUT: A string representation or ``None``. The latter means that no Sage-specific formatting is defined and the default should be used. TESTS:: sage: from sage.misc.displayhook import DisplayHookBase sage: dhb = DisplayHookBase() sage: dhb.try_format_obj('Hello, world!') """ if self.simple: return self.simple_format_obj(obj) if self.ascii_art: from sage.misc.ascii_art import ascii_art return ascii_art(obj) if self.typeset: from sage.misc.latex import pretty_print pretty_print(obj) return '' assert(False)
def _ascii_art_generator(self, m): r""" Return an ascii art representing the generator indexed by ``m``. TESTS:: sage: R = NonCommutativeSymmetricFunctions(QQ).R() sage: ascii_art(R[1,2,2,4]) R **** ** ** * sage: Partitions.global_options(diagram_str="#", convention="french") sage: ascii_art(R[1,2,2,4]) R # ## ## #### """ from sage.misc.ascii_art import AsciiArt, ascii_art pref = AsciiArt([self.prefix()]) r = pref * (AsciiArt([" "**Integer(len(pref))]) + ascii_art(m)) r._baseline = r._h - 1 return r
def ascii_art_gen(m): if m[1] != 1: r = (AsciiArt([" "**Integer(len(pref))]) + ascii_art(m[1])) else: r = empty_ascii_art r = r * P._ascii_art_generator(m[0]) r._baseline = r._h - 2 return r
def _ascii_art_(self): r""" TESTS:: sage: from sage.combinat.shuffle import SetShuffleProduct sage: ascii_art(SetShuffleProduct([[BinaryTree()], [BinaryTree([]), BinaryTree([[],[]])]], ....: [[1,4]])) Set shuffle product of: [ [ ] ] [ [ o, o ] ] [ [ / \ ] ] [ [ ] ] [ [ ], [ o o ] ] and [ [ 1, 4 ] ] """ from sage.misc.ascii_art import ascii_art, ascii_art_list return ascii_art("Set shuffle product of:") * \ (ascii_art_list(self._l1) + ascii_art(" and ") + ascii_art_list(self._l2))
def _repr_(self): r""" Return string representation. OUTPUT: String. EXAMPLES:: sage: from sage.geometry.polyhedron.double_description import \ ....: DoubleDescriptionPair, StandardAlgorithm sage: A = matrix(QQ, [(1,0,1), (0,1,1), (-1,-1,1)]) sage: DD = StandardAlgorithm(A).run() sage: DD._repr_() 'Double description pair (A, R) defined by\n [ 1 0 1] [ 2/3 -1/3 -1/3]\nA = [ 0 1 1], R = [-1/3 2/3 -1/3]\n [-1 -1 1] [ 1/3 1/3 1/3]' """ from sage.misc.ascii_art import ascii_art s = ascii_art('Double description pair (A, R) defined by') A = ascii_art(matrix(self.A)) A._baseline = (len(self.A) / 2) A = ascii_art('A = ') + A R = ascii_art(matrix(self.R).transpose()) if len(self.R) > 0: R._baseline = (len(self.R[0]) / 2) else: R._baseline = 0 R = ascii_art('R = ') + R return str(s * (A + ascii_art(', ') + R))
def __repr__(self): r""" Return string representation. OUTPUT: String. EXAMPLES:: sage: from sage.geometry.polyhedron.double_description import \ ....: DoubleDescriptionPair, StandardAlgorithm sage: A = matrix(QQ, [(1,0,1), (0,1,1), (-1,-1,1)]) sage: DD = StandardAlgorithm(A).run() sage: DD.__repr__() 'Double description pair (A, R) defined by\n [ 1 0 1] [ 2/3 -1/3 -1/3]\nA = [ 0 1 1], R = [-1/3 2/3 -1/3]\n [-1 -1 1] [ 1/3 1/3 1/3]' """ from sage.misc.ascii_art import ascii_art from sage.matrix.constructor import matrix s = ascii_art('Double description pair (A, R) defined by') A = ascii_art(matrix(self.A)) A._baseline = (len(self.A) / 2) A = ascii_art('A = ') + A R = ascii_art(matrix(self.R).transpose()) if len(self.R) > 0: R._baseline = (len(self.R[0]) / 2) else: R._baseline = 0 R = ascii_art('R = ') + R return str(s * (A + ascii_art(', ') + R))
def _ascii_art_(self): r""" TESTS:: sage: from sage.combinat.shuffle import ShuffleProduct sage: ascii_art(ShuffleProduct([1,2,3],[4,5])) Shuffle product of: [ ] [ ] [ 1, 2, 3 ] and [ 4, 5 ] sage: B = BinaryTree sage: ascii_art(ShuffleProduct([B([]), B([[],[]])], ....: [B([[[],[]],[[],None]])])) Shuffle product of: [ __o__ ] [ ] [ / \ ] [ o, o ] [ o o ] [ / \ ] [ / \ / ] [ o o ] and [ o o o ] """ from sage.misc.ascii_art import ascii_art, ascii_art_list return ascii_art("Shuffle product of:") * \ (ascii_art_list(self._l1) + ascii_art(" and ") + ascii_art_list(self._l2))
def _ascii_art_(self): """ TESTS:: sage: ascii_art(Compositions(4).list()) [ * ] [ * ** * * ] [ * * ** *** * ** * ] [ *, * , * , * , **, ** , ***, **** ] sage: Partitions.global_options(diagram_str='#', convention="French") sage: ascii_art(Compositions(4).list()) [ # ] [ # # # ## ] [ # # ## # # ## ### ] [ #, ##, #, ###, #, ##, #, #### ] """ from sage.misc.ascii_art import ascii_art return ascii_art(self.to_skew_partition())