TODO info improved
[sympy.git] / bin / isympy
blob5860bb631ab29ebbc4f140ab8220a11f4e276e65
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 Basic.set_repr_level(2) # 2D output
23 pprint_try_use_unicode() # try to setup unicode pprint
24 """
26 import sys
28 # hook in-tree sympy into python path if running in-tree version of isympy
29 import os.path
30 isympy_dir = os.path.dirname(__file__) # bin/isympy.py
31 sympy_top = os.path.split(isympy_dir)[0] # ../
32 sympy_dir = os.path.join(sympy_top, 'sympy') # ../sympy/
33 if os.path.isdir(sympy_dir):
34 #print 'working in-tree...'
35 sys.path.insert(0, sympy_top)
37 from sympy import __version__ as sympy_version
38 python_version = "%d.%d.%d" % tuple(sys.version_info[:3])
40 def indent(s):
41 """Puts ">>> " in front of each line."""
42 r = ""
43 for l in s.split("\n"):
44 if l != "":
45 r+=">>> "+l+"\n"
46 return r
48 welcome_msg = \
49 "Python %s console for SymPy %s. These commands were executed:\n%s" % \
50 (python_version, sympy_version, indent(init_code))
52 def run_ipython_interpreter():
54 from IPython.Shell import IPShellEmbed
56 #is the -nopprint necessary?
57 #args = ['-nopprint']
58 args = []
59 ipshell = IPShellEmbed(args)
60 api = ipshell.IP.getapi()
61 api.ex(init_code)
62 api.ex('__IP.compile("from __future__ import division", "<input>", "single") in __IP.user_ns')
64 ### create some magic commands
66 #def pretty_print(self, arg):
67 # self.api.ex("print str((%s).__pretty__())" % arg)
69 #api.expose_magic("pprint", pretty_print)
71 # Now start an embedded ipython.
72 ipshell(welcome_msg)
73 sys.exit("Exiting ...")
75 def run_python_interpreter():
76 print """\
77 Couldn't locate IPython. Having IPython installed is greatly recommended. See
78 http://ipython.scipy.org for more details. If you use Debian/Ubuntu, just
79 install the "ipython" package and start isympy again.\n"""
81 import code
82 import readline
83 import atexit
84 import os
87 class HistoryConsole(code.InteractiveConsole):
88 def __init__(self, locals=None, filename="<console>",
89 histfile=os.path.expanduser("~/.sympy-history")):
90 code.InteractiveConsole.__init__(self)
91 self.init_history(histfile)
92 self.runcode(self.compile("from __future__ import division",
93 "<input>", "single"))
95 def init_history(self, histfile):
96 readline.parse_and_bind("tab: complete")
97 if hasattr(readline, "read_history_file"):
98 try:
99 readline.read_history_file(histfile)
100 except IOError:
101 pass
102 atexit.register(self.save_history, histfile)
104 def save_history(self, histfile):
105 readline.write_history_file(histfile)
107 sh = HistoryConsole()
108 sh.runcode(init_code)
109 sh.interact(welcome_msg)
110 sys.exit("Exiting ...")
113 from optparse import OptionParser
115 def main():
116 from sympy import __version__ as sympy_version
117 parser = OptionParser("usage: isympy [options]", version=sympy_version )
118 parser.add_option("-c", "--console", dest="console",
119 help="specify a python interactive interpreter, python or ipython")
120 (options, args) = parser.parse_args()
122 if options.console == "python":
123 run_python_interpreter()
124 elif options.console == "ipython":
125 run_ipython_interpreter()
126 else:
127 try:
128 run_ipython_interpreter()
130 except ImportError:
131 run_python_interpreter()
133 if __name__ == "__main__":
134 main()