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. """
14 ip
= IPython
.ipapi
.get()
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
26 out
= stringify_func(arg
)
35 ip
.set_hook('result_display', result_display
)
40 import __builtin__
, sys
43 """Python's pretty-printer display hook.
45 This function was adapted from:
47 http://www.python.org/dev/peps/pep-0217/
52 print stringify_func(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. """
62 return IPython
.Shell
.make_IPython(argv
)
67 class HistoryConsole(code
.InteractiveConsole
):
69 code
.InteractiveConsole
.__init
__(self
)
71 history
= os
.path
.expanduser('~/.sympy-history')
74 import readline
, atexit
76 readline
.parse_and_bind('tab: complete')
78 if hasattr(readline
, 'read_history_file'):
80 readline
.read_history_file(history
)
84 atexit
.register(readline
.write_history_file
, history
)
88 return HistoryConsole()
90 if session
not in ['ipython', 'python']:
91 raise ValueError("'%s' is not a valid session name" % session
)
98 ip
= IPython
.ipapi
.get()
101 if session
== 'ipython':
102 ip
, in_ipyshell
= ip
.IP
, True
104 raise ValueError("Can't start Python shell from IPython")
106 if session
== 'ipython':
111 if session
== 'ipython':
116 ip
.runcode(ip
.compile("from __future__ import division"))
117 ip
.runcode(ip
.compile("from sympy.interactive import *"))
120 stringify_func
= 'lambda arg: pretty(arg, %s)' % use_unicode
122 stringify_func
= 'sstrrepr'
124 ip
.runcode(ip
.compile("init_printing(%s)" % stringify_func
))
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
138 message
= welcome
+ '\n'
141 sys
.exit('Exiting ...')
143 def shutdown_hook(self
):
146 ip
.set_hook('shutdown_hook', shutdown_hook
)