passagemath-repl 10.4.62__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. passagemath_repl-10.4.62.data/scripts/sage-cachegrind +25 -0
  2. passagemath_repl-10.4.62.data/scripts/sage-callgrind +16 -0
  3. passagemath_repl-10.4.62.data/scripts/sage-cleaner +230 -0
  4. passagemath_repl-10.4.62.data/scripts/sage-coverage +327 -0
  5. passagemath_repl-10.4.62.data/scripts/sage-eval +14 -0
  6. passagemath_repl-10.4.62.data/scripts/sage-fixdoctests +708 -0
  7. passagemath_repl-10.4.62.data/scripts/sage-inline-fortran +12 -0
  8. passagemath_repl-10.4.62.data/scripts/sage-ipynb2rst +50 -0
  9. passagemath_repl-10.4.62.data/scripts/sage-ipython +16 -0
  10. passagemath_repl-10.4.62.data/scripts/sage-massif +25 -0
  11. passagemath_repl-10.4.62.data/scripts/sage-notebook +267 -0
  12. passagemath_repl-10.4.62.data/scripts/sage-omega +25 -0
  13. passagemath_repl-10.4.62.data/scripts/sage-preparse +302 -0
  14. passagemath_repl-10.4.62.data/scripts/sage-run +27 -0
  15. passagemath_repl-10.4.62.data/scripts/sage-run-cython +10 -0
  16. passagemath_repl-10.4.62.data/scripts/sage-runtests +9 -0
  17. passagemath_repl-10.4.62.data/scripts/sage-startuptime.py +163 -0
  18. passagemath_repl-10.4.62.data/scripts/sage-valgrind +34 -0
  19. passagemath_repl-10.4.62.dist-info/METADATA +77 -0
  20. passagemath_repl-10.4.62.dist-info/RECORD +162 -0
  21. passagemath_repl-10.4.62.dist-info/WHEEL +5 -0
  22. passagemath_repl-10.4.62.dist-info/top_level.txt +1 -0
  23. sage/all__sagemath_repl.py +119 -0
  24. sage/doctest/__init__.py +4 -0
  25. sage/doctest/__main__.py +236 -0
  26. sage/doctest/all.py +4 -0
  27. sage/doctest/check_tolerance.py +261 -0
  28. sage/doctest/control.py +1727 -0
  29. sage/doctest/external.py +534 -0
  30. sage/doctest/fixtures.py +383 -0
  31. sage/doctest/forker.py +2665 -0
  32. sage/doctest/marked_output.py +102 -0
  33. sage/doctest/parsing.py +1708 -0
  34. sage/doctest/parsing_test.py +79 -0
  35. sage/doctest/reporting.py +733 -0
  36. sage/doctest/rif_tol.py +124 -0
  37. sage/doctest/sources.py +1657 -0
  38. sage/doctest/test.py +584 -0
  39. sage/doctest/tests/1second.rst +4 -0
  40. sage/doctest/tests/99seconds.rst +4 -0
  41. sage/doctest/tests/abort.rst +5 -0
  42. sage/doctest/tests/atexit.rst +7 -0
  43. sage/doctest/tests/fail_and_die.rst +6 -0
  44. sage/doctest/tests/initial.rst +15 -0
  45. sage/doctest/tests/interrupt.rst +7 -0
  46. sage/doctest/tests/interrupt_diehard.rst +14 -0
  47. sage/doctest/tests/keyboardinterrupt.rst +11 -0
  48. sage/doctest/tests/longtime.rst +5 -0
  49. sage/doctest/tests/nodoctest +5 -0
  50. sage/doctest/tests/random_seed.rst +4 -0
  51. sage/doctest/tests/show_skipped.rst +18 -0
  52. sage/doctest/tests/sig_on.rst +9 -0
  53. sage/doctest/tests/simple_failure.rst +8 -0
  54. sage/doctest/tests/sleep_and_raise.rst +106 -0
  55. sage/doctest/tests/tolerance.rst +31 -0
  56. sage/doctest/util.py +750 -0
  57. sage/interfaces/cleaner.py +48 -0
  58. sage/interfaces/quit.py +163 -0
  59. sage/misc/all__sagemath_repl.py +51 -0
  60. sage/misc/banner.py +235 -0
  61. sage/misc/benchmark.py +221 -0
  62. sage/misc/classgraph.py +131 -0
  63. sage/misc/copying.py +22 -0
  64. sage/misc/cython.py +694 -0
  65. sage/misc/dev_tools.py +745 -0
  66. sage/misc/edit_module.py +304 -0
  67. sage/misc/explain_pickle.py +3079 -0
  68. sage/misc/gperftools.py +361 -0
  69. sage/misc/inline_fortran.py +212 -0
  70. sage/misc/messaging.py +86 -0
  71. sage/misc/pager.py +21 -0
  72. sage/misc/profiler.py +179 -0
  73. sage/misc/python.py +70 -0
  74. sage/misc/remote_file.py +53 -0
  75. sage/misc/sage_eval.py +246 -0
  76. sage/misc/sage_input.py +3621 -0
  77. sage/misc/sagedoc.py +1742 -0
  78. sage/misc/sh.py +38 -0
  79. sage/misc/trace.py +90 -0
  80. sage/repl/__init__.py +16 -0
  81. sage/repl/all.py +15 -0
  82. sage/repl/attach.py +625 -0
  83. sage/repl/configuration.py +186 -0
  84. sage/repl/display/__init__.py +1 -0
  85. sage/repl/display/fancy_repr.py +354 -0
  86. sage/repl/display/formatter.py +318 -0
  87. sage/repl/display/jsmol_iframe.py +290 -0
  88. sage/repl/display/pretty_print.py +153 -0
  89. sage/repl/display/util.py +163 -0
  90. sage/repl/image.py +302 -0
  91. sage/repl/inputhook.py +91 -0
  92. sage/repl/interface_magic.py +298 -0
  93. sage/repl/interpreter.py +854 -0
  94. sage/repl/ipython_extension.py +593 -0
  95. sage/repl/ipython_kernel/__init__.py +1 -0
  96. sage/repl/ipython_kernel/__main__.py +4 -0
  97. sage/repl/ipython_kernel/all_jupyter.py +10 -0
  98. sage/repl/ipython_kernel/install.py +301 -0
  99. sage/repl/ipython_kernel/interact.py +278 -0
  100. sage/repl/ipython_kernel/kernel.py +217 -0
  101. sage/repl/ipython_kernel/widgets.py +466 -0
  102. sage/repl/ipython_kernel/widgets_sagenb.py +587 -0
  103. sage/repl/ipython_tests.py +163 -0
  104. sage/repl/load.py +326 -0
  105. sage/repl/preparse.py +2218 -0
  106. sage/repl/prompts.py +90 -0
  107. sage/repl/rich_output/__init__.py +4 -0
  108. sage/repl/rich_output/backend_base.py +648 -0
  109. sage/repl/rich_output/backend_doctest.py +316 -0
  110. sage/repl/rich_output/backend_emacs.py +151 -0
  111. sage/repl/rich_output/backend_ipython.py +596 -0
  112. sage/repl/rich_output/buffer.py +311 -0
  113. sage/repl/rich_output/display_manager.py +829 -0
  114. sage/repl/rich_output/example.avi +0 -0
  115. sage/repl/rich_output/example.canvas3d +1 -0
  116. sage/repl/rich_output/example.dvi +0 -0
  117. sage/repl/rich_output/example.flv +0 -0
  118. sage/repl/rich_output/example.gif +0 -0
  119. sage/repl/rich_output/example.jpg +0 -0
  120. sage/repl/rich_output/example.mkv +0 -0
  121. sage/repl/rich_output/example.mov +0 -0
  122. sage/repl/rich_output/example.mp4 +0 -0
  123. sage/repl/rich_output/example.ogv +0 -0
  124. sage/repl/rich_output/example.pdf +0 -0
  125. sage/repl/rich_output/example.png +0 -0
  126. sage/repl/rich_output/example.svg +54 -0
  127. sage/repl/rich_output/example.webm +0 -0
  128. sage/repl/rich_output/example.wmv +0 -0
  129. sage/repl/rich_output/example_jmol.spt.zip +0 -0
  130. sage/repl/rich_output/example_wavefront_scene.mtl +7 -0
  131. sage/repl/rich_output/example_wavefront_scene.obj +17 -0
  132. sage/repl/rich_output/output_basic.py +391 -0
  133. sage/repl/rich_output/output_browser.py +103 -0
  134. sage/repl/rich_output/output_catalog.py +54 -0
  135. sage/repl/rich_output/output_graphics.py +320 -0
  136. sage/repl/rich_output/output_graphics3d.py +345 -0
  137. sage/repl/rich_output/output_video.py +231 -0
  138. sage/repl/rich_output/preferences.py +432 -0
  139. sage/repl/rich_output/pretty_print.py +339 -0
  140. sage/repl/rich_output/test_backend.py +201 -0
  141. sage/repl/user_globals.py +214 -0
  142. sage/tests/__init__.py +1 -0
  143. sage/tests/all.py +3 -0
  144. sage/tests/article_heuberger_krenn_kropf_fsm-in-sage.py +630 -0
  145. sage/tests/arxiv_0812_2725.py +351 -0
  146. sage/tests/benchmark.py +1923 -0
  147. sage/tests/book_schilling_zabrocki_kschur_primer.py +795 -0
  148. sage/tests/book_stein_ent.py +651 -0
  149. sage/tests/book_stein_modform.py +558 -0
  150. sage/tests/cmdline.py +790 -0
  151. sage/tests/combinatorial_hopf_algebras.py +52 -0
  152. sage/tests/finite_poset.py +623 -0
  153. sage/tests/functools_partial_src.py +27 -0
  154. sage/tests/gosper-sum.py +218 -0
  155. sage/tests/lazy_imports.py +28 -0
  156. sage/tests/modular_group_cohomology.py +80 -0
  157. sage/tests/numpy.py +21 -0
  158. sage/tests/parigp.py +76 -0
  159. sage/tests/startup.py +27 -0
  160. sage/tests/symbolic-series.py +76 -0
  161. sage/tests/sympy.py +16 -0
  162. sage/tests/test_deprecation.py +31 -0
@@ -0,0 +1,648 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ r"""
3
+ Base Class for Backends
4
+
5
+ The display backends are the commandline, the
6
+ IPython notebook, the Emacs sage mode, the Sage doctester, .... All of
7
+ these have different capabilities for what they can display.
8
+
9
+ To implement a new display backend, you need to subclass
10
+ :class:`BackendBase`. All backend-specific handling of rich output
11
+ should be in :meth:`~BackendBase.displayhook` and
12
+ :meth:`~BackendBase.display_immediately`. See :class:`BackendSimple`
13
+ for an absolutely minimal example of a functioning backend.
14
+
15
+ You declare the types of rich output that your backend can handle in
16
+ :meth:`~BackendBase.supported_output`. There are two ways to then
17
+ display specific output types in your own backend.
18
+
19
+ * Directly use one of the existing output containers listed in
20
+ :mod:`sage.repl.rich_output.output_catalog`. That is, test for the
21
+ rich output type in :meth:`~BackendBase.display_immediately` and
22
+ handle it.
23
+
24
+ * Subclass the rich output container to attach your backend-specific
25
+ functionality. Then :meth:`~BackendBase.display_immediately` will
26
+ receive instances of your subclass. See
27
+ :class:`~sage.repl.rich_output.test_backend.BackendTest` for an
28
+ example of how this is done.
29
+
30
+ You can also mix both ways of implementing different rich output types.
31
+
32
+ EXAMPLES::
33
+
34
+ sage: from sage.repl.rich_output.backend_base import BackendSimple
35
+ sage: backend = BackendSimple()
36
+ sage: plain_text = backend.plain_text_formatter(list(range(10))); plain_text
37
+ OutputPlainText container
38
+ sage: backend.displayhook(plain_text, plain_text)
39
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
40
+ """
41
+
42
+ # ****************************************************************************
43
+ # Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com>
44
+ #
45
+ # Distributed under the terms of the GNU General Public License (GPL)
46
+ # as published by the Free Software Foundation; either version 2 of
47
+ # the License, or (at your option) any later version.
48
+ # https://www.gnu.org/licenses/
49
+ # ****************************************************************************
50
+
51
+ import builtins
52
+ from io import StringIO
53
+ from sage.structure.sage_object import SageObject
54
+
55
+
56
+ class BackendBase(SageObject):
57
+
58
+ def _repr_(self):
59
+ """
60
+ Return string representation of the backend.
61
+
62
+ Every backend must implement this method.
63
+
64
+ OUTPUT: string
65
+
66
+ EXAMPLES::
67
+
68
+ sage: from sage.repl.rich_output.backend_base import BackendBase
69
+ sage: backend = BackendBase()
70
+ sage: backend._repr_()
71
+ Traceback (most recent call last):
72
+ ...
73
+ NotImplementedError: derived classes must implement this method
74
+ """
75
+ raise NotImplementedError('derived classes must implement this method')
76
+
77
+ def get_display_manager(self):
78
+ """
79
+ Return the display manager singleton.
80
+
81
+ This is a convenience method to access the display manager
82
+ singleton.
83
+
84
+ OUTPUT:
85
+
86
+ The unique
87
+ :class:`~sage.repl.rich_output.display_manager.DisplayManager`
88
+ instance.
89
+
90
+ EXAMPLES::
91
+
92
+ sage: from sage.repl.rich_output.backend_base import BackendBase
93
+ sage: backend = BackendBase()
94
+ sage: backend.get_display_manager()
95
+ The Sage display manager using the doctest backend
96
+ """
97
+ from sage.repl.rich_output import get_display_manager
98
+ return get_display_manager()
99
+
100
+ def install(self, **kwds):
101
+ """
102
+ Hook that will be called once before the backend is used for the
103
+ first time.
104
+
105
+ The default implementation does nothing.
106
+
107
+ INPUT:
108
+
109
+ - ``kwds`` -- optional keyword arguments that are passed
110
+ through by the
111
+ :meth:`~sage.repl.rich_output.display_manager.DisplayManager.switch_backend`
112
+ method.
113
+
114
+ EXAMPLES::
115
+
116
+ sage: from sage.repl.rich_output.backend_base import BackendBase
117
+ sage: backend = BackendBase()
118
+ sage: backend.install()
119
+ """
120
+ pass
121
+
122
+ def uninstall(self):
123
+ """
124
+ Hook that will be called once right before the backend is removed.
125
+
126
+ The default implementation does nothing.
127
+
128
+ EXAMPLES::
129
+
130
+ sage: from sage.repl.rich_output.backend_base import BackendBase
131
+ sage: backend = BackendBase()
132
+ sage: backend.uninstall()
133
+ """
134
+ pass
135
+
136
+ def default_preferences(self):
137
+ """
138
+ Return the backend's display preferences.
139
+
140
+ Override this method to change the default preferences when
141
+ using your backend.
142
+
143
+ OUTPUT:
144
+
145
+ Instance of
146
+ :class:`~sage.repl.rich_output.preferences.DisplayPreferences`.
147
+
148
+ EXAMPLES::
149
+
150
+ sage: from sage.repl.rich_output.backend_base import BackendBase
151
+ sage: backend = BackendBase()
152
+ sage: backend.default_preferences()
153
+ Display preferences:
154
+ * align_latex is not specified
155
+ * graphics is not specified
156
+ * supplemental_plot is not specified
157
+ * text is not specified
158
+ """
159
+ from sage.repl.rich_output.preferences import DisplayPreferences
160
+ return DisplayPreferences()
161
+
162
+ def supported_output(self):
163
+ """
164
+ Return the outputs that are supported by the backend.
165
+
166
+ Subclasses must implement this method.
167
+
168
+ OUTPUT:
169
+
170
+ Iterable of output container classes, that is, subclass of
171
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`). May
172
+ be a list/tuple/set/frozenset. The order is ignored. Only used
173
+ internally by the display manager.
174
+
175
+ You may return backend-specific subclasses of existing output
176
+ containers. This allows you to attach backend-specific
177
+ functionality to the output container.
178
+
179
+ EXAMPLES::
180
+
181
+ sage: from sage.repl.rich_output.backend_base import BackendBase
182
+ sage: backend = BackendBase()
183
+ sage: backend.supported_output()
184
+ Traceback (most recent call last):
185
+ ...
186
+ NotImplementedError: derived classes must implement this method
187
+ """
188
+ raise NotImplementedError('derived classes must implement this method')
189
+
190
+ def is_in_terminal(self):
191
+ """
192
+ Test whether the UI is meant to run in a terminal.
193
+
194
+ See
195
+ :meth:`sage.repl.rich_output.display_manager.DisplayManager.is_in_terminal`
196
+ for details.
197
+
198
+ OUTPUT: defaults to ``False``
199
+
200
+ EXAMPLES::
201
+
202
+ sage: from sage.repl.rich_output.backend_base import BackendBase
203
+ sage: backend = BackendBase()
204
+ sage: backend.is_in_terminal()
205
+ False
206
+ """
207
+ return False
208
+
209
+ def max_width(self):
210
+ """
211
+ Return the number of characters that fit into one output line.
212
+
213
+ OUTPUT: integer
214
+
215
+ EXAMPLES::
216
+
217
+ sage: from sage.repl.rich_output.backend_base import BackendBase
218
+ sage: backend = BackendBase()
219
+ sage: backend.max_width()
220
+ 79
221
+ """
222
+ return 79
223
+
224
+ def newline(self):
225
+ r"""
226
+ Return the newline string.
227
+
228
+ OUTPUT: string for starting a new line of output
229
+
230
+ EXAMPLES::
231
+
232
+ sage: from sage.repl.rich_output.backend_base import BackendBase
233
+ sage: backend = BackendBase()
234
+ sage: backend.newline()
235
+ '\n'
236
+ """
237
+ return '\n'
238
+
239
+ def _apply_pretty_printer(self, pretty_printer_class, obj):
240
+ """
241
+ Helper method to format ``obj`` as text.
242
+
243
+ INPUT:
244
+
245
+ - ``pretty_printer_class`` -- subclass of
246
+ :class:`sage.repl.display.pretty_print.SagePrettyPrinter`
247
+
248
+ - ``obj`` -- anything
249
+
250
+ OUTPUT: string
251
+
252
+ EXAMPLES::
253
+
254
+ sage: from sage.repl.rich_output.backend_base import BackendBase
255
+ sage: backend = BackendBase()
256
+ sage: from sage.repl.display.pretty_print import SagePrettyPrinter
257
+ sage: backend._apply_pretty_printer(SagePrettyPrinter, 1/2)
258
+ '1/2'
259
+ """
260
+ stream = StringIO()
261
+ printer = pretty_printer_class(
262
+ stream, self.max_width(), self.newline())
263
+ printer.pretty(obj)
264
+ printer.flush()
265
+ return stream.getvalue()
266
+
267
+ def plain_text_formatter(self, obj, **kwds):
268
+ r"""
269
+ Hook to override how plain text is being formatted.
270
+
271
+ If the object does not have a ``_rich_repr_`` method, or if it
272
+ does not return a rich output object
273
+ (:class:`~sage.repl.rich_output.output_basic.OutputBase`),
274
+ then this method is used to generate plain text output.
275
+
276
+ INPUT:
277
+
278
+ - ``obj`` -- anything
279
+
280
+ - ``**kwds`` -- optional keyword arguments to control the
281
+ formatting. Supported are:
282
+
283
+ * ``concatenate`` -- boolean (default: ``False``); if
284
+ ``True``, the argument ``obj`` must be iterable and its
285
+ entries will be concatenated. There is a single
286
+ whitespace between entries.
287
+
288
+ OUTPUT:
289
+
290
+ Instance of
291
+ :class:`~sage.repl.rich_output.output_basic.OutputPlainText`
292
+ containing the string representation of the object.
293
+
294
+ EXAMPLES::
295
+
296
+ sage: from sage.repl.rich_output.backend_base import BackendBase
297
+ sage: backend = BackendBase()
298
+ sage: out = backend.plain_text_formatter(list(range(30)))
299
+ sage: out
300
+ OutputPlainText container
301
+ sage: out.text
302
+ buffer containing 139 bytes
303
+ sage: out.text.get_str()
304
+ '[0,\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n
305
+ 10,\n 11,\n 12,\n 13,\n 14,\n 15,\n 16,\n 17,\n 18,\n
306
+ 19,\n 20,\n 21,\n 22,\n 23,\n 24,\n 25,\n 26,\n 27,\n
307
+ 28,\n 29]'
308
+
309
+ sage: out = backend.plain_text_formatter(list(range(20)), concatenate=True)
310
+ sage: out.text.get_str()
311
+ '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19'
312
+ """
313
+ from sage.repl.display.pretty_print import SagePrettyPrinter
314
+ if kwds.get('concatenate', False):
315
+ plain_text = ' '.join(
316
+ self._apply_pretty_printer(SagePrettyPrinter, o) for o in obj)
317
+ else:
318
+ plain_text = self._apply_pretty_printer(SagePrettyPrinter, obj)
319
+ from sage.repl.rich_output.output_basic import OutputPlainText
320
+ return OutputPlainText(plain_text)
321
+
322
+ def ascii_art_formatter(self, obj, **kwds):
323
+ r"""
324
+ Hook to override how ascii art is being formatted.
325
+
326
+ INPUT:
327
+
328
+ - ``obj`` -- anything
329
+
330
+ - ``**kwds`` -- optional keyword arguments to control the
331
+ formatting. Supported are:
332
+
333
+ * ``concatenate`` -- boolean (default: ``False``); if
334
+ ``True``, the argument ``obj`` must be iterable and its
335
+ entries will be concatenated. There is a single
336
+ whitespace between entries.
337
+
338
+ OUTPUT:
339
+
340
+ Instance of
341
+ :class:`~sage.repl.rich_output.output_basic.OutputAsciiArt`
342
+ containing the ascii art string representation of the object.
343
+
344
+ EXAMPLES::
345
+
346
+ sage: from sage.repl.rich_output.backend_base import BackendBase
347
+ sage: backend = BackendBase()
348
+ sage: out = backend.ascii_art_formatter(list(range(30)))
349
+ sage: out
350
+ OutputAsciiArt container
351
+ sage: out.ascii_art
352
+ buffer containing 114 bytes
353
+ sage: print(out.ascii_art.get_str())
354
+ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
355
+ <BLANKLINE>
356
+ 22, 23, 24, 25, 26, 27, 28, 29 ]
357
+ sage: backend.ascii_art_formatter([1,2,3], concatenate=False).ascii_art.get_str()
358
+ '[ 1, 2, 3 ]'
359
+ sage: backend.ascii_art_formatter([1,2,3], concatenate=True).ascii_art.get_str()
360
+ '1 2 3'
361
+ """
362
+ from sage.typeset.ascii_art import ascii_art
363
+ if kwds.get('concatenate', False):
364
+ result = ascii_art(*obj, sep=' ')
365
+ else:
366
+ result = ascii_art(obj)
367
+ from sage.repl.rich_output.output_basic import OutputAsciiArt
368
+ return OutputAsciiArt(str(result))
369
+
370
+ def unicode_art_formatter(self, obj, **kwds):
371
+ r"""
372
+ Hook to override how unicode art is being formatted.
373
+
374
+ INPUT:
375
+
376
+ - ``obj`` -- anything
377
+
378
+ - ``**kwds`` -- optional keyword arguments to control the
379
+ formatting. Supported are:
380
+
381
+ * ``concatenate`` -- boolean (default: ``False``); if
382
+ ``True``, the argument ``obj`` must be iterable and its
383
+ entries will be concatenated. There is a single
384
+ whitespace between entries.
385
+
386
+ OUTPUT:
387
+
388
+ Instance of
389
+ :class:`~sage.repl.rich_output.output_basic.OutputUnicodeArt`
390
+ containing the unicode art string representation of the object.
391
+
392
+ EXAMPLES::
393
+
394
+ sage: from sage.repl.rich_output.backend_base import BackendBase
395
+ sage: backend = BackendBase()
396
+ sage: out = backend.unicode_art_formatter(list(range(30)))
397
+ sage: out
398
+ OutputUnicodeArt container
399
+ sage: out.unicode_art
400
+ buffer containing 114 bytes
401
+ sage: print(out.unicode_art.get_str())
402
+ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
403
+ <BLANKLINE>
404
+ 22, 23, 24, 25, 26, 27, 28, 29 ]
405
+
406
+ sage: backend.unicode_art_formatter([1,2,3], concatenate=False).unicode_art.get_str()
407
+ '[ 1, 2, 3 ]'
408
+ sage: backend.unicode_art_formatter([1,2,3], concatenate=True).unicode_art.get_str()
409
+ '1 2 3'
410
+ """
411
+ from sage.typeset.unicode_art import unicode_art
412
+ if kwds.get('concatenate', False):
413
+ result = unicode_art(*obj, sep=' ')
414
+ else:
415
+ result = unicode_art(obj)
416
+ from sage.repl.rich_output.output_basic import OutputUnicodeArt
417
+ return OutputUnicodeArt(str(result))
418
+
419
+ def latex_formatter(self, obj, **kwds):
420
+ r"""
421
+ Hook to override how latex is being formatted.
422
+
423
+ INPUT:
424
+
425
+ - ``obj`` -- anything
426
+
427
+ - ``**kwds`` -- optional keyword arguments to control the
428
+ formatting. Supported are:
429
+
430
+ * ``concatenate`` -- boolean (default: ``False``); if
431
+ ``True``, the argument ``obj`` must be iterable and its
432
+ entries will be concatenated. There is a single
433
+ whitespace between entries.
434
+
435
+ OUTPUT:
436
+
437
+ Instance of :class:`~sage.repl.rich_output.output_browser.OutputHtml`
438
+ containing the latex string representation of the object.
439
+
440
+ EXAMPLES::
441
+
442
+ sage: from sage.repl.rich_output.backend_base import BackendBase
443
+ sage: backend = BackendBase()
444
+ sage: out = backend.latex_formatter(1/2)
445
+ sage: out
446
+ OutputHtml container
447
+ sage: out.html
448
+ buffer containing 42 bytes
449
+ sage: out.html.get_str()
450
+ '<html>\\(\\displaystyle \\frac{1}{2}\\)</html>'
451
+
452
+ sage: # needs sage.symbolic
453
+ sage: out = backend.latex_formatter([1/2, x, 3/4, ZZ], concatenate=False)
454
+ sage: out.html.get_str()
455
+ '<html>\\(\\displaystyle \\newcommand{\\Bold}[1]{\\mathbf{#1}}\\left[\\frac{1}{2}, x, \\frac{3}{4}, \\Bold{Z}\\right]\\)</html>'
456
+ sage: out = backend.latex_formatter([1/2, x, 3/4, ZZ], concatenate=True)
457
+ sage: out.html.get_str()
458
+ '<html>\\(\\displaystyle \\newcommand{\\Bold}[1]{\\mathbf{#1}}\\frac{1}{2} x \\frac{3}{4} \\Bold{Z}\\)</html>'
459
+
460
+ TESTS::
461
+
462
+ sage: backend.latex_formatter([], concatenate=False).html.get_str()
463
+ '<html>\\(\\displaystyle \\left[\\right]\\)</html>'
464
+ sage: backend.latex_formatter([], concatenate=True).html.get_str()
465
+ '<html>\\(\\displaystyle \\)</html>'
466
+ """
467
+ concatenate = kwds.get('concatenate', False)
468
+ from sage.misc.html import html
469
+ from sage.repl.rich_output.output_browser import OutputHtml
470
+ return OutputHtml(html(obj, concatenate=concatenate, strict=True))
471
+
472
+ def set_underscore_variable(self, obj):
473
+ """
474
+ Set the ``_`` builtin variable.
475
+
476
+ By default, this sets the special ``_`` variable. Backends
477
+ that organize the history differently (e.g. IPython) can
478
+ override this method.
479
+
480
+ INPUT:
481
+
482
+ - ``obj`` -- result of the most recent evaluation
483
+
484
+ EXAMPLES::
485
+
486
+ sage: from sage.repl.rich_output.backend_base import BackendBase
487
+ sage: backend = BackendBase()
488
+ sage: backend.set_underscore_variable(123)
489
+ sage: _
490
+ 123
491
+
492
+ sage: 'foo'
493
+ 'foo'
494
+ sage: _ # indirect doctest
495
+ 'foo'
496
+ """
497
+ builtins._ = obj
498
+
499
+ def displayhook(self, plain_text, rich_output):
500
+ """
501
+ Backend implementation of the displayhook.
502
+
503
+ The value of the last statement on a REPL input line or
504
+ notebook cell are usually handed to the Python displayhook and
505
+ shown on screen. By overriding this method you define how
506
+ your backend handles output. The difference to the usual
507
+ displayhook is that Sage already converted the value to the
508
+ most suitable rich output container.
509
+
510
+ Derived classes must implement this method.
511
+
512
+ INPUT:
513
+
514
+ - ``plain_text`` -- instance of
515
+ :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The
516
+ plain text version of the output.
517
+
518
+ - ``rich_output`` -- instance of an output container class
519
+ (subclass of
520
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed
521
+ to be one of the output containers returned from
522
+ :meth:`supported_output`, possibly the same as
523
+ ``plain_text``.
524
+
525
+ OUTPUT:
526
+
527
+ This method may return something, which is then returned from
528
+ the display manager's
529
+ :meth:`~sage.repl.rich_output.display_manager.DisplayManager.displayhook`
530
+ method.
531
+
532
+ EXAMPLES::
533
+
534
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
535
+ sage: plain_text = OutputPlainText.example()
536
+ sage: from sage.repl.rich_output.backend_base import BackendBase
537
+ sage: backend = BackendBase()
538
+ sage: backend.displayhook(plain_text, plain_text)
539
+ Traceback (most recent call last):
540
+ ...
541
+ NotImplementedError: derived classes must implement this method
542
+ """
543
+ return self.display_immediately(plain_text, rich_output)
544
+
545
+ def display_immediately(self, plain_text, rich_output):
546
+ """
547
+ Show output without going back to the command line prompt.
548
+
549
+ This method is similar to the rich output :meth:`displayhook`,
550
+ except that it can be invoked at any time. Typically, it ends
551
+ up being called by :meth:`sage.plot.graphics.Graphics.show`.
552
+
553
+ Derived classes must implement this method.
554
+
555
+ INPUT:
556
+
557
+ Same as :meth:`displayhook`.
558
+
559
+ OUTPUT:
560
+
561
+ This method may return something so you can implement
562
+ :meth:`displayhook` by calling this method. However, when
563
+ called by the display manager any potential return value is
564
+ discarded: There is no way to return anything without
565
+ returning to the command prompt.
566
+
567
+ EXAMPLES::
568
+
569
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
570
+ sage: plain_text = OutputPlainText.example()
571
+ sage: from sage.repl.rich_output.backend_base import BackendBase
572
+ sage: backend = BackendBase()
573
+ sage: backend.display_immediately(plain_text, plain_text)
574
+ Traceback (most recent call last):
575
+ ...
576
+ NotImplementedError: derived classes must implement this method
577
+ """
578
+ raise NotImplementedError('derived classes must implement this method')
579
+
580
+
581
+ class BackendSimple(BackendBase):
582
+ """
583
+ Simple Backend.
584
+
585
+ This backend only supports plain text.
586
+
587
+ EXAMPLES::
588
+
589
+ sage: from sage.repl.rich_output.backend_base import BackendSimple
590
+ sage: BackendSimple()
591
+ simple
592
+ """
593
+
594
+ def _repr_(self):
595
+ r"""
596
+ Return string representation of the backend.
597
+
598
+ OUTPUT: string
599
+
600
+ EXAMPLES::
601
+
602
+ sage: from sage.repl.rich_output.backend_base import BackendSimple
603
+ sage: backend = BackendSimple()
604
+ sage: backend._repr_()
605
+ 'simple'
606
+ """
607
+ return 'simple'
608
+
609
+ def supported_output(self):
610
+ """
611
+ Return the outputs that are supported by the backend.
612
+
613
+ OUTPUT:
614
+
615
+ Iterable of output container classes, that is, subclass of
616
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`).
617
+ This backend only supports the plain text output container.
618
+
619
+ EXAMPLES::
620
+
621
+ sage: from sage.repl.rich_output.backend_base import BackendSimple
622
+ sage: backend = BackendSimple()
623
+ sage: backend.supported_output()
624
+ {<class 'sage.repl.rich_output.output_basic.OutputPlainText'>}
625
+ """
626
+ from sage.repl.rich_output.output_basic import OutputPlainText
627
+ return set([OutputPlainText])
628
+
629
+ def display_immediately(self, plain_text, rich_output):
630
+ """
631
+ Show output without going back to the command line prompt.
632
+
633
+ INPUT:
634
+
635
+ Same as :meth:`~BackendBase.displayhook`.
636
+
637
+ OUTPUT: this backend returns nothing, it just prints to stdout
638
+
639
+ EXAMPLES::
640
+
641
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
642
+ sage: plain_text = OutputPlainText.example()
643
+ sage: from sage.repl.rich_output.backend_base import BackendSimple
644
+ sage: backend = BackendSimple()
645
+ sage: backend.display_immediately(plain_text, plain_text)
646
+ Example plain text output
647
+ """
648
+ print(rich_output.text.get_str())