[16/24] +-Infinity: correct assumptions
[sympy.git] / bin / isympy
blob4fadf7dd3a107e5135ff3ae7d9035586cbf384df
1 #!/usr/bin/python
3 """Python shell for SymPy.
5 This is just a normal Python shell (ipython shell if you have the
6 IPython package installed) that executes the following commands so
7 that you don't have to:
9 >>> from __future__ import division
10 >>> from sympy import *
11 >>> x, y, z = symbols("xyz")
12 >>> k, m, n = symbols("kmn", integer=True)
13 >>> f = Function('f')
15 So starting 'isympy' is equivalent to starting Python (or IPython)
16 and executing the above commands by hand. It is intended for easy
17 and quick experimentation with SymPy.
19 COMMAND LINE OPTIONS
20 --------------------
22 -c CONSOLE, --console=CONSOLE
24 Use the specified python or ipython shell (lower case) as console
25 backend instead of the default one (ipython if present or python
26 otherwise), eg. isympy -c python
28 -q, --quiet
30 Print only Python's and SymPy's versions to stdout at startup.
32 -- IPython's options
34 Additionally you can pass command line options directly to IPython
35 interpreter (standard Python shell is not supported). However you
36 need to add '--' separator between two types of options, eg. to run
37 SymPy without startup banner and colors, issue:
39 isympy -q -- -colors NoColor
41 """
43 import os, sys
45 # common SymPy's code for IPython and Python shells
47 div_code = "from __future__ import division"
48 sym_code = "from sympy.interactive import *"
50 # hook in-tree SymPy into Python path, if possible
52 isympy_dir = os.path.dirname(__file__) # bin/isympy
53 sympy_top = os.path.split(isympy_dir)[0] # ../
54 sympy_dir = os.path.join(sympy_top, 'sympy') # ../sympy/
56 if os.path.isdir(sympy_dir):
57 sys.path.insert(0, sympy_top)
59 # some longer messages
61 long_message = """\
62 These commands were executed:
63 >>> from __future__ import division
64 >>> from sympy import *
65 >>> x, y, z = symbols('xyz')
66 >>> k, m, n = symbols('kmn', integer=True)
67 >>> f = Function("f")
69 Documentation can be found at http://sympy.org/
70 """
72 no_ipython = """\
73 Couldn't locate IPython. Having IPython installed is greatly recommended.
74 See http://ipython.scipy.org for more details. If you use Debian/Ubuntu,
75 just install the 'ipython' package and start isympy again.
76 """
78 def init_ipython(*args, **kwargs):
79 import IPython.Shell
81 ip = IPython.Shell.make_IPython(kwargs.get('argv'))
83 from sympy import pretty
85 def result_display(self, arg):
86 """Pretty-printer display hook.
88 Called for displaying pretty results to the user. Using this
89 handler not only SymPy's expression can be printed but also
90 Python's lists, tuples and dictionaries.
92 This function was adapted from:
94 ipython/IPython/hooks.py:155
96 """
97 if self.rc.pprint:
98 out = pretty(arg)
100 if '\n' in out:
101 print
103 print out
104 else:
105 print repr(arg)
107 ip.set_hook('result_display', result_display)
109 ip.runcode(ip.compile(div_code, '<input>', 'single'))
110 ip.runcode(ip.compile(sym_code, '<input>', 'single'))
112 ip.interact(kwargs.get('message'))
113 sys.exit('Exiting ...')
115 def init_python(*args, **kwargs):
116 import code
118 class HistoryConsole(code.InteractiveConsole):
119 def __init__(self, *args, **kwargs):
120 code.InteractiveConsole.__init__(self)
122 history = kwargs.get('history', '~/.sympy-history')
123 history = os.path.expanduser(history)
125 try:
126 import readline, atexit
128 readline.parse_and_bind('tab: complete')
130 if hasattr(readline, 'read_history_file'):
131 try:
132 readline.read_history_file(history)
133 except IOError:
134 pass
136 def save_history(history):
137 readline.write_history_file(history)
139 atexit.register(save_history, history)
140 except ImportError:
141 pass
143 from sympy import pprint
144 sys.displayhook = pprint
146 console = HistoryConsole(*args, **kwargs)
148 console.runcode(console.compile(div_code, '<input>', 'single'))
149 console.runcode(console.compile(sym_code, '<input>', 'single'))
151 console.interact(kwargs.get('message'))
152 sys.exit('Exiting ...')
154 def main():
155 from sympy import __version__ as sympy_version
156 py_version = "%d.%d.%d" % sys.version_info[:3]
158 usage = 'usage: isympy [options] -- [ipython options]'
160 key_options = ('dest', 'action', 'default', 'help')
162 isympy_options = (
163 # parametric options
164 ('-c', '--console', 'console', 'store', None,
165 'select an interactive console: python | ipython'),
166 # boolean options
167 ('-q', '--quiet', 'quiet', 'store_true', False,
168 'print only version information at startup'),
171 from optparse import OptionParser
173 parser = OptionParser(usage, version=sympy_version)
175 for option in isympy_options:
176 parser.add_option(*option[0:2],
177 **dict(zip(key_options, option[2:])))
179 (options, args) = parser.parse_args()
181 message = "Python %s console for SymPy %s" \
182 % (py_version, sympy_version)
184 if os.getenv('SYMPY_USE_CACHE') == 'no':
185 message += ' (cache: off)\n'
186 else:
187 message += '\n'
189 if not options.quiet:
190 message += '\n' + long_message
192 args = {
193 'message' : message,
194 'argv' : args,
197 shells = {
198 'ipython' : init_ipython,
199 'python' : init_python
202 try:
203 shells[options.console](**args)
204 except KeyError:
205 try:
206 init_ipython(**args)
207 except ImportError:
208 print no_ipython
209 init_python(**args)
211 if __name__ == "__main__":
212 main()