sympy.interactive -- rework init_printing to support different stringifiers
[sympy.git] / sympy / interactive / __init__.py
blob5e253b838e8a02296e68f1e22db9d618ae4a1517
2 from sympy import *
4 x, y, z = symbols('xyz')
5 k, m, n = symbols('kmn', integer=True)
6 f, g, h = map(Function, 'fgh')
8 def init_printing(stringify_func):
9 """Initializes pretty-printer depending on the environment. """
11 try:
12 import IPython
14 ip = IPython.ipapi.get()
16 if ip is not None:
17 def result_display(self, arg):
18 """IPython's pretty-printer display hook.
20 This function was adapted from:
22 ipython/IPython/hooks.py:155
24 """
25 if self.rc.pprint:
26 out = stringify_func(arg)
28 if '\n' in out:
29 print
31 print out
32 else:
33 print repr(arg)
35 ip.set_hook('result_display', result_display)
36 return
37 except ImportError:
38 pass
40 import __builtin__, sys
42 def displayhook(arg):
43 """Python's pretty-printer display hook.
45 This function was adapted from:
47 http://www.python.org/dev/peps/pep-0217/
49 """
50 if arg is not None:
51 __builtin__._ = None
52 print stringify_func(arg)
53 __builtin__._ = arg
55 sys.displayhook = displayhook
57 def init_session(session="ipython", pretty=True, use_unicode=None, message=None, argv=[]):
58 """Initialize embedded IPython or Python session. """
59 import os, sys
61 def init_IPython():
62 return IPython.Shell.make_IPython(argv)
64 def init_Python():
65 import code
67 class HistoryConsole(code.InteractiveConsole):
68 def __init__(self):
69 code.InteractiveConsole.__init__(self)
71 history = os.path.expanduser('~/.sympy-history')
73 try:
74 import readline, atexit
76 readline.parse_and_bind('tab: complete')
78 if hasattr(readline, 'read_history_file'):
79 try:
80 readline.read_history_file(history)
81 except IOError:
82 pass
84 atexit.register(readline.write_history_file, history)
85 except ImportError:
86 pass
88 return HistoryConsole()
90 if session not in ['ipython', 'python']:
91 raise ValueError("'%s' is not a valid session name" % session)
93 in_ipyshell = False
95 try:
96 import IPython
98 ip = IPython.ipapi.get()
100 if ip is not None:
101 if session == 'ipython':
102 ip, in_ipyshell = ip.IP, True
103 else:
104 raise ValueError("Can't start Python shell from IPython")
105 else:
106 if session == 'ipython':
107 ip = init_IPython()
108 else:
109 ip = init_Python()
110 except ImportError:
111 if session == 'ipython':
112 raise
113 else:
114 ip = init_Python()
116 ip.runcode(ip.compile("from __future__ import division"))
117 ip.runcode(ip.compile("from sympy.interactive import *"))
119 if pretty:
120 stringify_func = 'lambda arg: pretty(arg, %s)' % use_unicode
121 else:
122 stringify_func = 'sstrrepr'
124 ip.runcode(ip.compile("init_printing(%s)" % stringify_func))
126 if not in_ipyshell:
127 from sympy import __version__ as sympy_version
128 py_version = "%d.%d.%d" % sys.version_info[:3]
130 welcome = "Python %s console for SymPy %s" % (py_version, sympy_version)
132 if os.getenv('SYMPY_USE_CACHE') == 'no':
133 welcome += ' (cache: off)'
135 if message is not None:
136 message = welcome + '\n\n' + message
137 else:
138 message = welcome + '\n'
140 ip.interact(message)
141 sys.exit('Exiting ...')
142 else:
143 def shutdown_hook(self):
144 print "Exiting ..."
146 ip.set_hook('shutdown_hook', shutdown_hook)