##// END OF EJS Templates
Add deprecation warnings for sympyprinting...
Sean Vig -
Show More
@@ -1,151 +1,171 b''
1 1 """
2 2 A print function that pretty prints sympy Basic objects.
3 3
4 4 :moduleauthor: Brian Granger
5 5
6 6 Usage
7 7 =====
8 8
9 9 Once the extension is loaded, Sympy Basic objects are automatically
10 10 pretty-printed.
11 11
12 As of SymPy 0.7.2, maintenance of this extension has moved to SymPy under
13 sympy.interactive.ipythonprinting, any modifications to account for changes to
14 SymPy should be submitted to SymPy rather than changed here. This module is
15 maintained here for backwards compatablitiy with old SymPy versions.
16
12 17 """
13 18 #-----------------------------------------------------------------------------
14 19 # Copyright (C) 2008 The IPython Development Team
15 20 #
16 21 # Distributed under the terms of the BSD License. The full license is in
17 22 # the file COPYING, distributed as part of this software.
18 23 #-----------------------------------------------------------------------------
19 24
20 25 #-----------------------------------------------------------------------------
21 26 # Imports
22 27 #-----------------------------------------------------------------------------
23 28
24 29 from IPython.lib.latextools import latex_to_png
25 30 from IPython.testing import decorators as dec
26 31 # use @dec.skipif_not_sympy to skip tests requiring sympy
27 32
28 33 try:
29 34 from sympy import pretty, latex
30 35 except ImportError:
31 36 pass
32 37
38 import warnings
39
33 40 #-----------------------------------------------------------------------------
34 41 # Definitions of special display functions for use with IPython
35 42 #-----------------------------------------------------------------------------
36 43
37 44 def print_basic_unicode(o, p, cycle):
38 45 """A function to pretty print sympy Basic objects."""
39 46 if cycle:
40 47 return p.text('Basic(...)')
41 48 out = pretty(o, use_unicode=True)
42 49 if '\n' in out:
43 50 p.text(u'\n')
44 51 p.text(out)
45 52
46 53
47 54 def print_png(o):
48 55 """
49 56 A function to display sympy expression using inline style LaTeX in PNG.
50 57 """
51 58 s = latex(o, mode='inline')
52 59 # mathtext does not understand certain latex flags, so we try to replace
53 60 # them with suitable subs.
54 61 s = s.replace('\\operatorname','')
55 62 s = s.replace('\\overline', '\\bar')
56 63 png = latex_to_png(s)
57 64 return png
58 65
59 66
60 67 def print_display_png(o):
61 68 """
62 69 A function to display sympy expression using display style LaTeX in PNG.
63 70 """
64 71 s = latex(o, mode='plain')
65 72 s = s.strip('$')
66 73 # As matplotlib does not support display style, dvipng backend is
67 74 # used here.
68 75 png = latex_to_png(s, backend='dvipng', wrap=True)
69 76 return png
70 77
71 78
72 79 def can_print_latex(o):
73 80 """
74 81 Return True if type o can be printed with LaTeX.
75 82
76 83 If o is a container type, this is True if and only if every element of o
77 84 can be printed with LaTeX.
78 85 """
79 86 import sympy
80 87 if isinstance(o, (list, tuple, set, frozenset)):
81 88 return all(can_print_latex(i) for i in o)
82 89 elif isinstance(o, dict):
83 90 return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)
84 91 elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):
85 92 return True
86 93 return False
87 94
88 95 def print_latex(o):
89 96 """A function to generate the latex representation of sympy
90 97 expressions."""
91 98 if can_print_latex(o):
92 99 s = latex(o, mode='plain')
93 100 s = s.replace('\\dag','\\dagger')
94 101 s = s.strip('$')
95 102 return '$$%s$$' % s
96 103 # Fallback to the string printer
97 104 return None
98 105
99 106 _loaded = False
100 107
101 108 def load_ipython_extension(ip):
102 109 """Load the extension in IPython."""
103 110 import sympy
111
112 # sympyprinting extension has been moved to SymPy as of 0.7.2, if it
113 # exists there, warn the user and import it
114 try:
115 import sympy.interactive.ipythonprinting
116 except ImportError:
117 pass
118 else:
119 warnings.warn("The sympyprinting extension in IPython is deprecated, "
120 "use sympy.interactive.ipythonprinting")
121 ip.extension_manager.load_extension('sympy.interactive.ipythonprinting')
122 return
123
104 124 global _loaded
105 125 if not _loaded:
106 126 plaintext_formatter = ip.display_formatter.formatters['text/plain']
107 127
108 128 for cls in (object, str):
109 129 plaintext_formatter.for_type(cls, print_basic_unicode)
110 130
111 131 printable_containers = [list, tuple]
112 132
113 133 # set and frozen set were broken with SymPy's latex() function, but
114 134 # was fixed in the 0.7.1-git development version. See
115 135 # http://code.google.com/p/sympy/issues/detail?id=3062.
116 136 if sympy.__version__ > '0.7.1':
117 137 printable_containers += [set, frozenset]
118 138 else:
119 139 plaintext_formatter.for_type(cls, print_basic_unicode)
120 140
121 141 plaintext_formatter.for_type_by_name(
122 142 'sympy.core.basic', 'Basic', print_basic_unicode
123 143 )
124 144 plaintext_formatter.for_type_by_name(
125 145 'sympy.matrices.matrices', 'Matrix', print_basic_unicode
126 146 )
127 147
128 148 png_formatter = ip.display_formatter.formatters['image/png']
129 149
130 150 png_formatter.for_type_by_name(
131 151 'sympy.core.basic', 'Basic', print_png
132 152 )
133 153 png_formatter.for_type_by_name(
134 154 'sympy.matrices.matrices', 'Matrix', print_display_png
135 155 )
136 156 for cls in [dict, int, long, float] + printable_containers:
137 157 png_formatter.for_type(cls, print_png)
138 158
139 159 latex_formatter = ip.display_formatter.formatters['text/latex']
140 160 latex_formatter.for_type_by_name(
141 161 'sympy.core.basic', 'Basic', print_latex
142 162 )
143 163 latex_formatter.for_type_by_name(
144 164 'sympy.matrices.matrices', 'Matrix', print_latex
145 165 )
146 166
147 167 for cls in printable_containers:
148 168 # Use LaTeX only if every element is printable by latex
149 169 latex_formatter.for_type(cls, print_latex)
150 170
151 171 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now