polishing function.py, had to fail another 2 tests in pretty printing
[sympy.git] / bin / isympy
blobbdc5819fe68bda80e2770cbc40cb72b6a2c2500f
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 sympy import *
19 x, y, z = symbols('xyz')
20 k, m, n = symbols('kmn', integer=True)
21 """
23 import sys
24 sys.path.append('..')
25 sys.path.append('.')
27 from sympy import __version__ as sympy_version
28 python_version = "%d.%d.%d" % tuple(sys.version_info[:3])
30 def indent(s):
31 """Puts ">>> " in front of each line."""
32 r = ""
33 for l in s.split("\n"):
34 if l != "":
35 r+=">>> "+l+"\n"
36 return r
38 welcome_msg = \
39 "Python %s console for SymPy %s. These commands were executed:\n%s" % \
40 (python_version, sympy_version, indent(init_code))
42 def run_ipython_interpreter():
44 from IPython.Shell import IPShellEmbed
46 #is the -nopprint necessary?
47 #args = ['-nopprint']
48 args = []
49 ipshell = IPShellEmbed(args)
50 api = ipshell.IP.getapi()
51 api.ex(init_code)
52 api.ex('__IP.compile("from __future__ import division", "<input>", "single") in __IP.user_ns')
54 ### create some magic commands
56 #def pretty_print(self, arg):
57 # self.api.ex("print str((%s).__pretty__())" % arg)
59 #api.expose_magic("pprint", pretty_print)
61 # Now start an embedded ipython.
62 ipshell(welcome_msg)
63 sys.exit("Exiting ...")
65 def run_python_interpreter():
66 print """\
67 Couldn't locate IPython. Having IPython installed is greatly recommended. See
68 http://ipython.scipy.org for more details. If you use Debian/Ubuntu, just
69 install the "ipython" package and start isympy again.\n"""
71 import code
72 import readline
73 import atexit
74 import os
77 class HistoryConsole(code.InteractiveConsole):
78 def __init__(self, locals=None, filename="<console>",
79 histfile=os.path.expanduser("~/.sympy-history")):
80 code.InteractiveConsole.__init__(self)
81 self.init_history(histfile)
83 def init_history(self, histfile):
84 readline.parse_and_bind("tab: complete")
85 if hasattr(readline, "read_history_file"):
86 try:
87 readline.read_history_file(histfile)
88 except IOError:
89 pass
90 atexit.register(self.save_history, histfile)
92 def save_history(self, histfile):
93 readline.write_history_file(histfile)
95 sh = HistoryConsole()
96 sh.runcode(init_code)
97 sh.interact(welcome_msg)
98 sys.exit("Exiting ...")
101 from optparse import OptionParser
103 def main():
104 from sympy import __version__ as sympy_version
105 parser = OptionParser("usage: isympy [options]", version=sympy_version )
106 parser.add_option("-c", "--console", dest="console",
107 help="specify a python interactive interpreter, python or ipython")
108 (options, args) = parser.parse_args()
110 if options.console == "python":
111 run_python_interpreter()
112 elif options.console == "ipython":
113 run_ipython_interpreter()
114 else:
115 try:
116 run_ipython_interpreter()
118 except ImportError:
119 run_python_interpreter()
121 if __name__ == "__main__":
122 main()