Added tag sympy-0.5.10 for changeset beabd2bcdec2
[sympy.git] / bin / isympy
bloba5610a5f425e2dab7b106b3a280b71293dd89e7b
1 #! /usr/bin/env python
2 """
3 Python shell for SymPy. Executes the commands in the "init_code" variable
4 before giving you a shell.
6 command line options:
7 -c : permits to specify a python interactive interpreter, currently only
8 python or ipython. Example usage:
9 isympy -c python
10 default is set to ipython
12 -h : prints this help message
14 --version: prints version number
15 """
17 init_code = """
18 from __future__ import division
19 from sympy import *
20 x, y, z = symbols('xyz')
21 k, m, n = symbols('kmn', integer=True)
22 f = Function("f")
23 Basic.set_repr_level(2) # pretty print output; Use "1" for python output
24 pprint_try_use_unicode() # use unicode pretty print when available
25 """
27 import sys
29 # hook in-tree sympy into python path if running in-tree version of isympy
30 import os.path
31 isympy_dir = os.path.dirname(__file__) # bin/isympy
32 sympy_top = os.path.split(isympy_dir)[0] # ../
33 sympy_dir = os.path.join(sympy_top, 'sympy') # ../sympy/
34 if os.path.isdir(sympy_dir):
35 #print 'working in-tree...'
36 sys.path.insert(0, sympy_top)
38 from sympy import __version__ as sympy_version
39 python_version = "%d.%d.%d" % tuple(sys.version_info[:3])
41 def indent(s):
42 """Puts ">>> " in front of each line."""
43 r = ""
44 for l in s.split("\n"):
45 if l != "":
46 r+=">>> "+l+"\n"
47 return r
49 welcome_msg = \
50 "Python %s console for SymPy %s. These commands were executed:\n%s" % \
51 (python_version, sympy_version, indent(init_code))
53 def run_ipython_interpreter():
55 from IPython.Shell import IPShellEmbed
57 #is the -nopprint necessary?
58 #args = ['-nopprint']
59 args = []
60 ipshell = IPShellEmbed(args)
61 api = ipshell.IP.getapi()
62 api.ex(init_code)
63 api.ex('__IP.compile("from __future__ import division", "<input>", "single") in __IP.user_ns')
65 ### create some magic commands
67 #def pretty_print(self, arg):
68 # self.api.ex("print str((%s).__pretty__())" % arg)
70 #api.expose_magic("pprint", pretty_print)
72 # Now start an embedded ipython.
73 ipshell(welcome_msg)
74 sys.exit("Exiting ...")
76 def run_python_interpreter():
77 print """\
78 Couldn't locate IPython. Having IPython installed is greatly recommended. See
79 http://ipython.scipy.org for more details. If you use Debian/Ubuntu, just
80 install the "ipython" package and start isympy again.\n"""
82 import code
83 import readline
84 import atexit
85 import os
88 class HistoryConsole(code.InteractiveConsole):
89 def __init__(self, locals=None, filename="<console>",
90 histfile=os.path.expanduser("~/.sympy-history")):
91 code.InteractiveConsole.__init__(self)
92 self.init_history(histfile)
93 self.runcode(self.compile("from __future__ import division",
94 "<input>", "single"))
96 def init_history(self, histfile):
97 readline.parse_and_bind("tab: complete")
98 if hasattr(readline, "read_history_file"):
99 try:
100 readline.read_history_file(histfile)
101 except IOError:
102 pass
103 atexit.register(self.save_history, histfile)
105 def save_history(self, histfile):
106 readline.write_history_file(histfile)
108 sh = HistoryConsole()
109 sh.runcode(init_code)
110 sh.interact(welcome_msg)
111 sys.exit("Exiting ...")
114 from optparse import OptionParser
116 def main():
117 from sympy import __version__ as sympy_version
118 parser = OptionParser("usage: isympy [options]", version=sympy_version )
119 parser.add_option("-c", "--console", dest="console",
120 help="specify a python interactive interpreter, python or ipython")
121 (options, args) = parser.parse_args()
123 if options.console == "python":
124 run_python_interpreter()
125 elif options.console == "ipython":
126 run_ipython_interpreter()
127 else:
128 try:
129 run_ipython_interpreter()
131 except ImportError:
132 run_python_interpreter()
134 if __name__ == "__main__":
135 main()