Coverage for /usr/lib/python3/dist-packages/sympy/core/_print_helpers.py: 71%
14 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1"""
2Base class to provide str and repr hooks that `init_printing` can overwrite.
4This is exposed publicly in the `printing.defaults` module,
5but cannot be defined there without causing circular imports.
6"""
8class Printable:
9 """
10 The default implementation of printing for SymPy classes.
12 This implements a hack that allows us to print elements of built-in
13 Python containers in a readable way. Natively Python uses ``repr()``
14 even if ``str()`` was explicitly requested. Mix in this trait into
15 a class to get proper default printing.
17 This also adds support for LaTeX printing in jupyter notebooks.
18 """
20 # Since this class is used as a mixin we set empty slots. That means that
21 # instances of any subclasses that use slots will not need to have a
22 # __dict__.
23 __slots__ = ()
25 # Note, we always use the default ordering (lex) in __str__ and __repr__,
26 # regardless of the global setting. See issue 5487.
27 def __str__(self):
28 from sympy.printing.str import sstr
29 return sstr(self, order=None)
31 __repr__ = __str__
33 def _repr_disabled(self):
34 """
35 No-op repr function used to disable jupyter display hooks.
37 When :func:`sympy.init_printing` is used to disable certain display
38 formats, this function is copied into the appropriate ``_repr_*_``
39 attributes.
41 While we could just set the attributes to `None``, doing it this way
42 allows derived classes to call `super()`.
43 """
44 return None
46 # We don't implement _repr_png_ here because it would add a large amount of
47 # data to any notebook containing SymPy expressions, without adding
48 # anything useful to the notebook. It can still enabled manually, e.g.,
49 # for the qtconsole, with init_printing().
50 _repr_png_ = _repr_disabled
52 _repr_svg_ = _repr_disabled
54 def _repr_latex_(self):
55 """
56 IPython/Jupyter LaTeX printing
58 To change the behavior of this (e.g., pass in some settings to LaTeX),
59 use init_printing(). init_printing() will also enable LaTeX printing
60 for built in numeric types like ints and container types that contain
61 SymPy objects, like lists and dictionaries of expressions.
62 """
63 from sympy.printing.latex import latex
64 s = latex(self, mode='plain')
65 return "$\\displaystyle %s$" % s