##// END OF EJS Templates
- new doctest_mode magic to toggle doctest pasting/prompts....
fperez -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,118 +1,122 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Modified input prompt for entering text with >>> or ... at the start.
2 """Modified input prompt for entering text with >>> or ... at the start.
3
3
4 We define a special input line filter to allow typing lines which begin with
4 We define a special input line filter to allow typing lines which begin with
5 '>>> ' or '... '. These two strings, if present at the start of the input
5 '>>> ' or '... '. These two strings, if present at the start of the input
6 line, are stripped. This allows for direct pasting of code from examples such
6 line, are stripped. This allows for direct pasting of code from examples such
7 as those available in the standard Python tutorial.
7 as those available in the standard Python tutorial.
8
8
9 Normally pasting such code is one chunk is impossible because of the
9 Normally pasting such code is one chunk is impossible because of the
10 extraneous >>> and ..., requiring one to do a line by line paste with careful
10 extraneous >>> and ..., requiring one to do a line by line paste with careful
11 removal of those characters. This module allows pasting that kind of
11 removal of those characters. This module allows pasting that kind of
12 multi-line examples in one pass.
12 multi-line examples in one pass.
13
13
14 Here is an 'screenshot' of a section of the tutorial pasted into IPython with
14 Here is an 'screenshot' of a section of the tutorial pasted into IPython with
15 this feature enabled:
15 this feature enabled:
16
16
17 In [1]: >>> def fib2(n): # return Fibonacci series up to n
17 In [1]: >>> def fib2(n): # return Fibonacci series up to n
18 ...: ... '''Return a list containing the Fibonacci series up to n.'''
18 ...: ... '''Return a list containing the Fibonacci series up to n.'''
19 ...: ... result = []
19 ...: ... result = []
20 ...: ... a, b = 0, 1
20 ...: ... a, b = 0, 1
21 ...: ... while b < n:
21 ...: ... while b < n:
22 ...: ... result.append(b) # see below
22 ...: ... result.append(b) # see below
23 ...: ... a, b = b, a+b
23 ...: ... a, b = b, a+b
24 ...: ... return result
24 ...: ... return result
25 ...:
25 ...:
26
26
27 In [2]: fib2(10)
27 In [2]: fib2(10)
28 Out[2]: [1, 1, 2, 3, 5, 8]
28 Out[2]: [1, 1, 2, 3, 5, 8]
29
29
30 The >>> and ... are stripped from the input so that the python interpreter
30 The >>> and ... are stripped from the input so that the python interpreter
31 only sees the real part of the code.
31 only sees the real part of the code.
32
32
33 All other input is processed normally.
33 All other input is processed normally.
34
34
35 Notes
35 Notes
36 =====
36 =====
37
37
38 * You can even paste code that has extra initial spaces, such as is common in
38 * You can even paste code that has extra initial spaces, such as is common in
39 doctests:
39 doctests:
40
40
41 In [3]: >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
41 In [3]: >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
42
42
43 In [4]: >>> for i in range(len(a)):
43 In [4]: >>> for i in range(len(a)):
44 ...: ... print i, a[i]
44 ...: ... print i, a[i]
45 ...: ...
45 ...: ...
46 0 Mary
46 0 Mary
47 1 had
47 1 had
48 2 a
48 2 a
49 3 little
49 3 little
50 4 lamb
50 4 lamb
51 """
51 """
52
52
53 #*****************************************************************************
53 #*****************************************************************************
54 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
54 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
55 #
55 #
56 # Distributed under the terms of the BSD License. The full license is in
56 # Distributed under the terms of the BSD License. The full license is in
57 # the file COPYING, distributed as part of this software.
57 # the file COPYING, distributed as part of this software.
58 #*****************************************************************************
58 #*****************************************************************************
59
59
60 from IPython import Release
60 from IPython import Release
61 __author__ = '%s <%s>' % Release.authors['Fernando']
61 __author__ = '%s <%s>' % Release.authors['Fernando']
62 __license__ = Release.license
62 __license__ = Release.license
63
63
64 # This file is an example of how to modify IPython's line-processing behavior
64 # This file is an example of how to modify IPython's line-processing behavior
65 # without touching the internal code. We'll define an alternate pre-processing
65 # without touching the internal code. We'll define an alternate pre-processing
66 # stage which allows a special form of input (which is invalid Python syntax)
66 # stage which allows a special form of input (which is invalid Python syntax)
67 # for certain quantities, rewrites a line of proper Python in those cases, and
67 # for certain quantities, rewrites a line of proper Python in those cases, and
68 # then passes it off to IPython's normal processor for further work.
68 # then passes it off to IPython's normal processor for further work.
69
69
70 # With this kind of customization, IPython can be adapted for many
70 # With this kind of customization, IPython can be adapted for many
71 # special-purpose scenarios providing alternate input syntaxes.
71 # special-purpose scenarios providing alternate input syntaxes.
72
72
73 # This file can be imported like a regular module.
73 # This file can be imported like a regular module.
74
74
75 # IPython has a prefilter() function that analyzes each input line. We redefine
75 # IPython has a prefilter() function that analyzes each input line. We redefine
76 # it here to first pre-process certain forms of input
76 # it here to first pre-process certain forms of input
77
77
78 # The prototype of any alternate prefilter must be like this one (the name
78 # The prototype of any alternate prefilter must be like this one (the name
79 # doesn't matter):
79 # doesn't matter):
80 # - line is a string containing the user input line.
80 # - line is a string containing the user input line.
81 # - continuation is a parameter which tells us if we are processing a first
81 # - continuation is a parameter which tells us if we are processing a first
82 # line of user input or the second or higher of a multi-line statement.
82 # line of user input or the second or higher of a multi-line statement.
83
83
84 import re
84 import re
85
85
86 from IPython.iplib import InteractiveShell
87
86 PROMPT_RE = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
88 PROMPT_RE = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
87
89
88 def prefilter_paste(self,line,continuation):
90 def prefilter_paste(self,line,continuation):
89 """Alternate prefilter for input of pasted code from an interpreter.
91 """Alternate prefilter for input of pasted code from an interpreter.
90 """
92 """
91 if not line:
93 if not line:
92 return ''
94 return ''
93 m = PROMPT_RE.match(line)
95 m = PROMPT_RE.match(line)
94 if m:
96 if m:
95 # In the end, always call the default IPython _prefilter() function.
97 # In the end, always call the default IPython _prefilter() function.
96 # Note that self must be passed explicitly, b/c we're calling the
98 # Note that self must be passed explicitly, b/c we're calling the
97 # unbound class method (since this method will overwrite the instance
99 # unbound class method (since this method will overwrite the instance
98 # prefilter())
100 # prefilter())
99 return self._prefilter(line[len(m.group(0)):],continuation)
101 return self._prefilter(line[len(m.group(0)):],continuation)
100 elif line.strip() == '...':
102 elif line.strip() == '...':
101 return self._prefilter('',continuation)
103 return self._prefilter('',continuation)
102 elif line.isspace():
104 elif line.isspace():
103 # This allows us to recognize multiple input prompts separated by blank
105 # This allows us to recognize multiple input prompts separated by blank
104 # lines and pasted in a single chunk, very common when pasting doctests
106 # lines and pasted in a single chunk, very common when pasting doctests
105 # or long tutorial passages.
107 # or long tutorial passages.
106 return ''
108 return ''
107 else:
109 else:
108 return self._prefilter(line,continuation)
110 return self._prefilter(line,continuation)
109
110 # Rebind this to be the new IPython prefilter:
111 from IPython.iplib import InteractiveShell
112 InteractiveShell.prefilter = prefilter_paste
113
111
114 # Clean up the namespace.
112 def activate_prefilter():
115 del InteractiveShell,prefilter_paste
113 """Rebind the input-pasting filter to be the new IPython prefilter"""
114 InteractiveShell.prefilter = prefilter_paste
115
116 def deactivate_prefilter():
117 """Reset the filter."""
118 InteractiveShell.prefilter = InteractiveShell._prefilter
116
119
117 # Just a heads up at the console
120 # Just a heads up at the console
121 activate_prefilter()
118 print '*** Pasting of code with ">>>" or "..." has been enabled.'
122 print '*** Pasting of code with ">>>" or "..." has been enabled.'
@@ -1,45 +1,48 b''
1 """Config file for 'doctest' profile.
1 """Config file for 'doctest' profile.
2
2
3 This profile modifies the prompts to be the standard Python ones, so that you
3 This profile modifies the prompts to be the standard Python ones, so that you
4 can generate easily doctests from an IPython session.
4 can generate easily doctests from an IPython session.
5
5
6 But more importantly, it enables pasting of code with '>>>' prompts and
6 But more importantly, it enables pasting of code with '>>>' prompts and
7 arbitrary initial whitespace, as is typical of doctests in reST files and
7 arbitrary initial whitespace, as is typical of doctests in reST files and
8 docstrings. This allows you to easily re-run existing doctests and iteratively
8 docstrings. This allows you to easily re-run existing doctests and iteratively
9 work on them as part of your development workflow.
9 work on them as part of your development workflow.
10
10
11 The exception mode is also set to 'plain' so the generated exceptions are as
11 The exception mode is also set to 'plain' so the generated exceptions are as
12 similar as possible to the default Python ones, for inclusion in doctests."""
12 similar as possible to the default Python ones, for inclusion in doctests."""
13
13
14 # get various stuff that are there for historical / familiarity reasons
14 # get various stuff that are there for historical / familiarity reasons
15 import ipy_legacy
15 import ipy_legacy
16
16
17 from IPython import ipapi
17 from IPython import ipapi
18
18
19 from IPython.Extensions import InterpreterPasteInput
19 from IPython.Extensions import InterpreterPasteInput
20
20
21 def main():
21 def main():
22 ip = ipapi.get()
22 ip = ipapi.get()
23 o = ip.options
23 o = ip.options
24
24
25 # Set the prompts similar to the defaults
25 # Set the prompts similar to the defaults
26 o.prompt_in1 = '>>> '
26 o.prompt_in1 = '>>> '
27 o.prompt_in2 = '... '
27 o.prompt_in2 = '... '
28 o.prompt_out = ''
28 o.prompt_out = ''
29
29
30 # No separation between successive inputs
30 # No separation between successive inputs
31 o.separate_in = ''
31 o.separate_in = ''
32 o.separate_out = ''
32 o.separate_out = ''
33 # But add a blank line after any output, to help separate doctests from
33 # But add a blank line after any output, to help separate doctests from
34 # each other. This is needed by doctest to distinguish each test from the
34 # each other. This is needed by doctest to distinguish each test from the
35 # next.
35 # next.
36 o.separate_out2 = '\n'
36 o.separate_out2 = '\n'
37
37
38 # Disable pprint, so that outputs are printed as similarly to standard
38 # Disable pprint, so that outputs are printed as similarly to standard
39 # python as possible
39 # python as possible
40 o.pprint = False
40 o.pprint = False
41
41
42 # Use plain exceptions, to also resemble normal pyhton.
42 # Use plain exceptions, to also resemble normal pyhton.
43 o.xmode = 'plain'
43 o.xmode = 'plain'
44
44
45 # Store the activity flag in the metadata bag from the running shell
46 ip.IP.meta.doctest_mode = True
47
45 main()
48 main()
@@ -1,25 +1,25 b''
1 """ IPython 'sci' profile
1 """ IPython 'sci' profile
2
2
3 Replaces the old scipy profile.
3 Replaces the old scipy profile.
4
4
5 """
5 """
6
6
7
7
8 import IPython.ipapi
8 import IPython.ipapi
9 import ipy_defaults
9 import ipy_defaults
10
10
11 def main():
11 def main():
12 ip = IPython.ipapi.get()
12 ip = IPython.ipapi.get()
13
13
14 try:
14 try:
15 ip.ex("import scipy")
16 ip.ex("import numpy")
15 ip.ex("import numpy")
16 ip.ex("import scipy")
17
17
18 ip.ex("from scipy import *")
19 ip.ex("from numpy import *")
18 ip.ex("from numpy import *")
19 ip.ex("from scipy import *")
20 print "SciPy profile successfully loaded."
20 print "SciPy profile successfully loaded."
21 except ImportError:
21 except ImportError:
22 print "Unable to start scipy profile, are scipy and numpy installed?"
22 print "Unable to start scipy profile, are scipy and numpy installed?"
23
23
24
24
25 main() No newline at end of file
25 main()
@@ -1,2915 +1,3001 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3
3
4 $Id: Magic.py 2595 2007-08-08 09:41:33Z vivainio $"""
4 $Id: Magic.py 2601 2007-08-10 07:01:29Z fperez $"""
5
5
6 #*****************************************************************************
6 #*****************************************************************************
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 #****************************************************************************
14 #****************************************************************************
15 # Modules and globals
15 # Modules and globals
16
16
17 from IPython import Release
17 from IPython import Release
18 __author__ = '%s <%s>\n%s <%s>' % \
18 __author__ = '%s <%s>\n%s <%s>' % \
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
19 ( Release.authors['Janko'] + Release.authors['Fernando'] )
20 __license__ = Release.license
20 __license__ = Release.license
21
21
22 # Python standard modules
22 # Python standard modules
23 import __builtin__
23 import __builtin__
24 import bdb
24 import bdb
25 import inspect
25 import inspect
26 import os
26 import os
27 import pdb
27 import pdb
28 import pydoc
28 import pydoc
29 import sys
29 import sys
30 import re
30 import re
31 import tempfile
31 import tempfile
32 import time
32 import time
33 import cPickle as pickle
33 import cPickle as pickle
34 import textwrap
34 import textwrap
35 from cStringIO import StringIO
35 from cStringIO import StringIO
36 from getopt import getopt,GetoptError
36 from getopt import getopt,GetoptError
37 from pprint import pprint, pformat
37 from pprint import pprint, pformat
38 from sets import Set
38 from sets import Set
39
39
40 # cProfile was added in Python2.5
40 # cProfile was added in Python2.5
41 try:
41 try:
42 import cProfile as profile
42 import cProfile as profile
43 import pstats
43 import pstats
44 except ImportError:
44 except ImportError:
45 # profile isn't bundled by default in Debian for license reasons
45 # profile isn't bundled by default in Debian for license reasons
46 try:
46 try:
47 import profile,pstats
47 import profile,pstats
48 except ImportError:
48 except ImportError:
49 profile = pstats = None
49 profile = pstats = None
50
50
51 # Homebrewed
51 # Homebrewed
52 import IPython
52 import IPython
53 from IPython import Debugger, OInspect, wildcard
53 from IPython import Debugger, OInspect, wildcard
54 from IPython.FakeModule import FakeModule
54 from IPython.FakeModule import FakeModule
55 from IPython.Itpl import Itpl, itpl, printpl,itplns
55 from IPython.Itpl import Itpl, itpl, printpl,itplns
56 from IPython.PyColorize import Parser
56 from IPython.PyColorize import Parser
57 from IPython.ipstruct import Struct
57 from IPython.ipstruct import Struct
58 from IPython.macro import Macro
58 from IPython.macro import Macro
59 from IPython.genutils import *
59 from IPython.genutils import *
60 from IPython import platutils
60 from IPython import platutils
61 import IPython.generics
61 import IPython.generics
62 import IPython.ipapi
62 import IPython.ipapi
63
63
64 #***************************************************************************
64 #***************************************************************************
65 # Utility functions
65 # Utility functions
66 def on_off(tag):
66 def on_off(tag):
67 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
67 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
68 return ['OFF','ON'][tag]
68 return ['OFF','ON'][tag]
69
69
70 class Bunch: pass
70 class Bunch: pass
71
71
72 def compress_dhist(dh):
72 def compress_dhist(dh):
73 head, tail = dh[:-10], dh[-10:]
73 head, tail = dh[:-10], dh[-10:]
74
74
75 newhead = []
75 newhead = []
76 done = Set()
76 done = Set()
77 for h in head:
77 for h in head:
78 if h in done:
78 if h in done:
79 continue
79 continue
80 newhead.append(h)
80 newhead.append(h)
81 done.add(h)
81 done.add(h)
82
82
83 return newhead + tail
83 return newhead + tail
84
84
85
85
86 #***************************************************************************
86 #***************************************************************************
87 # Main class implementing Magic functionality
87 # Main class implementing Magic functionality
88 class Magic:
88 class Magic:
89 """Magic functions for InteractiveShell.
89 """Magic functions for InteractiveShell.
90
90
91 Shell functions which can be reached as %function_name. All magic
91 Shell functions which can be reached as %function_name. All magic
92 functions should accept a string, which they can parse for their own
92 functions should accept a string, which they can parse for their own
93 needs. This can make some functions easier to type, eg `%cd ../`
93 needs. This can make some functions easier to type, eg `%cd ../`
94 vs. `%cd("../")`
94 vs. `%cd("../")`
95
95
96 ALL definitions MUST begin with the prefix magic_. The user won't need it
96 ALL definitions MUST begin with the prefix magic_. The user won't need it
97 at the command line, but it is is needed in the definition. """
97 at the command line, but it is is needed in the definition. """
98
98
99 # class globals
99 # class globals
100 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
100 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
101 'Automagic is ON, % prefix NOT needed for magic functions.']
101 'Automagic is ON, % prefix NOT needed for magic functions.']
102
102
103 #......................................................................
103 #......................................................................
104 # some utility functions
104 # some utility functions
105
105
106 def __init__(self,shell):
106 def __init__(self,shell):
107
107
108 self.options_table = {}
108 self.options_table = {}
109 if profile is None:
109 if profile is None:
110 self.magic_prun = self.profile_missing_notice
110 self.magic_prun = self.profile_missing_notice
111 self.shell = shell
111 self.shell = shell
112
112
113 # namespace for holding state we may need
113 # namespace for holding state we may need
114 self._magic_state = Bunch()
114 self._magic_state = Bunch()
115
115
116 def profile_missing_notice(self, *args, **kwargs):
116 def profile_missing_notice(self, *args, **kwargs):
117 error("""\
117 error("""\
118 The profile module could not be found. If you are a Debian user,
118 The profile module could not be found. If you are a Debian user,
119 it has been removed from the standard Debian package because of its non-free
119 it has been removed from the standard Debian package because of its non-free
120 license. To use profiling, please install"python2.3-profiler" from non-free.""")
120 license. To use profiling, please install"python2.3-profiler" from non-free.""")
121
121
122 def default_option(self,fn,optstr):
122 def default_option(self,fn,optstr):
123 """Make an entry in the options_table for fn, with value optstr"""
123 """Make an entry in the options_table for fn, with value optstr"""
124
124
125 if fn not in self.lsmagic():
125 if fn not in self.lsmagic():
126 error("%s is not a magic function" % fn)
126 error("%s is not a magic function" % fn)
127 self.options_table[fn] = optstr
127 self.options_table[fn] = optstr
128
128
129 def lsmagic(self):
129 def lsmagic(self):
130 """Return a list of currently available magic functions.
130 """Return a list of currently available magic functions.
131
131
132 Gives a list of the bare names after mangling (['ls','cd', ...], not
132 Gives a list of the bare names after mangling (['ls','cd', ...], not
133 ['magic_ls','magic_cd',...]"""
133 ['magic_ls','magic_cd',...]"""
134
134
135 # FIXME. This needs a cleanup, in the way the magics list is built.
135 # FIXME. This needs a cleanup, in the way the magics list is built.
136
136
137 # magics in class definition
137 # magics in class definition
138 class_magic = lambda fn: fn.startswith('magic_') and \
138 class_magic = lambda fn: fn.startswith('magic_') and \
139 callable(Magic.__dict__[fn])
139 callable(Magic.__dict__[fn])
140 # in instance namespace (run-time user additions)
140 # in instance namespace (run-time user additions)
141 inst_magic = lambda fn: fn.startswith('magic_') and \
141 inst_magic = lambda fn: fn.startswith('magic_') and \
142 callable(self.__dict__[fn])
142 callable(self.__dict__[fn])
143 # and bound magics by user (so they can access self):
143 # and bound magics by user (so they can access self):
144 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
144 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
145 callable(self.__class__.__dict__[fn])
145 callable(self.__class__.__dict__[fn])
146 magics = filter(class_magic,Magic.__dict__.keys()) + \
146 magics = filter(class_magic,Magic.__dict__.keys()) + \
147 filter(inst_magic,self.__dict__.keys()) + \
147 filter(inst_magic,self.__dict__.keys()) + \
148 filter(inst_bound_magic,self.__class__.__dict__.keys())
148 filter(inst_bound_magic,self.__class__.__dict__.keys())
149 out = []
149 out = []
150 for fn in magics:
150 for fn in magics:
151 out.append(fn.replace('magic_','',1))
151 out.append(fn.replace('magic_','',1))
152 out.sort()
152 out.sort()
153 return out
153 return out
154
154
155 def extract_input_slices(self,slices,raw=False):
155 def extract_input_slices(self,slices,raw=False):
156 """Return as a string a set of input history slices.
156 """Return as a string a set of input history slices.
157
157
158 Inputs:
158 Inputs:
159
159
160 - slices: the set of slices is given as a list of strings (like
160 - slices: the set of slices is given as a list of strings (like
161 ['1','4:8','9'], since this function is for use by magic functions
161 ['1','4:8','9'], since this function is for use by magic functions
162 which get their arguments as strings.
162 which get their arguments as strings.
163
163
164 Optional inputs:
164 Optional inputs:
165
165
166 - raw(False): by default, the processed input is used. If this is
166 - raw(False): by default, the processed input is used. If this is
167 true, the raw input history is used instead.
167 true, the raw input history is used instead.
168
168
169 Note that slices can be called with two notations:
169 Note that slices can be called with two notations:
170
170
171 N:M -> standard python form, means including items N...(M-1).
171 N:M -> standard python form, means including items N...(M-1).
172
172
173 N-M -> include items N..M (closed endpoint)."""
173 N-M -> include items N..M (closed endpoint)."""
174
174
175 if raw:
175 if raw:
176 hist = self.shell.input_hist_raw
176 hist = self.shell.input_hist_raw
177 else:
177 else:
178 hist = self.shell.input_hist
178 hist = self.shell.input_hist
179
179
180 cmds = []
180 cmds = []
181 for chunk in slices:
181 for chunk in slices:
182 if ':' in chunk:
182 if ':' in chunk:
183 ini,fin = map(int,chunk.split(':'))
183 ini,fin = map(int,chunk.split(':'))
184 elif '-' in chunk:
184 elif '-' in chunk:
185 ini,fin = map(int,chunk.split('-'))
185 ini,fin = map(int,chunk.split('-'))
186 fin += 1
186 fin += 1
187 else:
187 else:
188 ini = int(chunk)
188 ini = int(chunk)
189 fin = ini+1
189 fin = ini+1
190 cmds.append(hist[ini:fin])
190 cmds.append(hist[ini:fin])
191 return cmds
191 return cmds
192
192
193 def _ofind(self, oname, namespaces=None):
193 def _ofind(self, oname, namespaces=None):
194 """Find an object in the available namespaces.
194 """Find an object in the available namespaces.
195
195
196 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
196 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
197
197
198 Has special code to detect magic functions.
198 Has special code to detect magic functions.
199 """
199 """
200
200
201 oname = oname.strip()
201 oname = oname.strip()
202
202
203 alias_ns = None
203 alias_ns = None
204 if namespaces is None:
204 if namespaces is None:
205 # Namespaces to search in:
205 # Namespaces to search in:
206 # Put them in a list. The order is important so that we
206 # Put them in a list. The order is important so that we
207 # find things in the same order that Python finds them.
207 # find things in the same order that Python finds them.
208 namespaces = [ ('Interactive', self.shell.user_ns),
208 namespaces = [ ('Interactive', self.shell.user_ns),
209 ('IPython internal', self.shell.internal_ns),
209 ('IPython internal', self.shell.internal_ns),
210 ('Python builtin', __builtin__.__dict__),
210 ('Python builtin', __builtin__.__dict__),
211 ('Alias', self.shell.alias_table),
211 ('Alias', self.shell.alias_table),
212 ]
212 ]
213 alias_ns = self.shell.alias_table
213 alias_ns = self.shell.alias_table
214
214
215 # initialize results to 'null'
215 # initialize results to 'null'
216 found = 0; obj = None; ospace = None; ds = None;
216 found = 0; obj = None; ospace = None; ds = None;
217 ismagic = 0; isalias = 0; parent = None
217 ismagic = 0; isalias = 0; parent = None
218
218
219 # Look for the given name by splitting it in parts. If the head is
219 # Look for the given name by splitting it in parts. If the head is
220 # found, then we look for all the remaining parts as members, and only
220 # found, then we look for all the remaining parts as members, and only
221 # declare success if we can find them all.
221 # declare success if we can find them all.
222 oname_parts = oname.split('.')
222 oname_parts = oname.split('.')
223 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
223 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
224 for nsname,ns in namespaces:
224 for nsname,ns in namespaces:
225 try:
225 try:
226 obj = ns[oname_head]
226 obj = ns[oname_head]
227 except KeyError:
227 except KeyError:
228 continue
228 continue
229 else:
229 else:
230 #print 'oname_rest:', oname_rest # dbg
230 #print 'oname_rest:', oname_rest # dbg
231 for part in oname_rest:
231 for part in oname_rest:
232 try:
232 try:
233 parent = obj
233 parent = obj
234 obj = getattr(obj,part)
234 obj = getattr(obj,part)
235 except:
235 except:
236 # Blanket except b/c some badly implemented objects
236 # Blanket except b/c some badly implemented objects
237 # allow __getattr__ to raise exceptions other than
237 # allow __getattr__ to raise exceptions other than
238 # AttributeError, which then crashes IPython.
238 # AttributeError, which then crashes IPython.
239 break
239 break
240 else:
240 else:
241 # If we finish the for loop (no break), we got all members
241 # If we finish the for loop (no break), we got all members
242 found = 1
242 found = 1
243 ospace = nsname
243 ospace = nsname
244 if ns == alias_ns:
244 if ns == alias_ns:
245 isalias = 1
245 isalias = 1
246 break # namespace loop
246 break # namespace loop
247
247
248 # Try to see if it's magic
248 # Try to see if it's magic
249 if not found:
249 if not found:
250 if oname.startswith(self.shell.ESC_MAGIC):
250 if oname.startswith(self.shell.ESC_MAGIC):
251 oname = oname[1:]
251 oname = oname[1:]
252 obj = getattr(self,'magic_'+oname,None)
252 obj = getattr(self,'magic_'+oname,None)
253 if obj is not None:
253 if obj is not None:
254 found = 1
254 found = 1
255 ospace = 'IPython internal'
255 ospace = 'IPython internal'
256 ismagic = 1
256 ismagic = 1
257
257
258 # Last try: special-case some literals like '', [], {}, etc:
258 # Last try: special-case some literals like '', [], {}, etc:
259 if not found and oname_head in ["''",'""','[]','{}','()']:
259 if not found and oname_head in ["''",'""','[]','{}','()']:
260 obj = eval(oname_head)
260 obj = eval(oname_head)
261 found = 1
261 found = 1
262 ospace = 'Interactive'
262 ospace = 'Interactive'
263
263
264 return {'found':found, 'obj':obj, 'namespace':ospace,
264 return {'found':found, 'obj':obj, 'namespace':ospace,
265 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
265 'ismagic':ismagic, 'isalias':isalias, 'parent':parent}
266
266
267 def arg_err(self,func):
267 def arg_err(self,func):
268 """Print docstring if incorrect arguments were passed"""
268 """Print docstring if incorrect arguments were passed"""
269 print 'Error in arguments:'
269 print 'Error in arguments:'
270 print OInspect.getdoc(func)
270 print OInspect.getdoc(func)
271
271
272 def format_latex(self,strng):
272 def format_latex(self,strng):
273 """Format a string for latex inclusion."""
273 """Format a string for latex inclusion."""
274
274
275 # Characters that need to be escaped for latex:
275 # Characters that need to be escaped for latex:
276 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
276 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
277 # Magic command names as headers:
277 # Magic command names as headers:
278 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
278 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
279 re.MULTILINE)
279 re.MULTILINE)
280 # Magic commands
280 # Magic commands
281 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
281 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
282 re.MULTILINE)
282 re.MULTILINE)
283 # Paragraph continue
283 # Paragraph continue
284 par_re = re.compile(r'\\$',re.MULTILINE)
284 par_re = re.compile(r'\\$',re.MULTILINE)
285
285
286 # The "\n" symbol
286 # The "\n" symbol
287 newline_re = re.compile(r'\\n')
287 newline_re = re.compile(r'\\n')
288
288
289 # Now build the string for output:
289 # Now build the string for output:
290 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
290 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
291 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
291 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
292 strng)
292 strng)
293 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
293 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
294 strng = par_re.sub(r'\\\\',strng)
294 strng = par_re.sub(r'\\\\',strng)
295 strng = escape_re.sub(r'\\\1',strng)
295 strng = escape_re.sub(r'\\\1',strng)
296 strng = newline_re.sub(r'\\textbackslash{}n',strng)
296 strng = newline_re.sub(r'\\textbackslash{}n',strng)
297 return strng
297 return strng
298
298
299 def format_screen(self,strng):
299 def format_screen(self,strng):
300 """Format a string for screen printing.
300 """Format a string for screen printing.
301
301
302 This removes some latex-type format codes."""
302 This removes some latex-type format codes."""
303 # Paragraph continue
303 # Paragraph continue
304 par_re = re.compile(r'\\$',re.MULTILINE)
304 par_re = re.compile(r'\\$',re.MULTILINE)
305 strng = par_re.sub('',strng)
305 strng = par_re.sub('',strng)
306 return strng
306 return strng
307
307
308 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
308 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
309 """Parse options passed to an argument string.
309 """Parse options passed to an argument string.
310
310
311 The interface is similar to that of getopt(), but it returns back a
311 The interface is similar to that of getopt(), but it returns back a
312 Struct with the options as keys and the stripped argument string still
312 Struct with the options as keys and the stripped argument string still
313 as a string.
313 as a string.
314
314
315 arg_str is quoted as a true sys.argv vector by using shlex.split.
315 arg_str is quoted as a true sys.argv vector by using shlex.split.
316 This allows us to easily expand variables, glob files, quote
316 This allows us to easily expand variables, glob files, quote
317 arguments, etc.
317 arguments, etc.
318
318
319 Options:
319 Options:
320 -mode: default 'string'. If given as 'list', the argument string is
320 -mode: default 'string'. If given as 'list', the argument string is
321 returned as a list (split on whitespace) instead of a string.
321 returned as a list (split on whitespace) instead of a string.
322
322
323 -list_all: put all option values in lists. Normally only options
323 -list_all: put all option values in lists. Normally only options
324 appearing more than once are put in a list.
324 appearing more than once are put in a list.
325
325
326 -posix (True): whether to split the input line in POSIX mode or not,
326 -posix (True): whether to split the input line in POSIX mode or not,
327 as per the conventions outlined in the shlex module from the
327 as per the conventions outlined in the shlex module from the
328 standard library."""
328 standard library."""
329
329
330 # inject default options at the beginning of the input line
330 # inject default options at the beginning of the input line
331 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
331 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
332 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
332 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
333
333
334 mode = kw.get('mode','string')
334 mode = kw.get('mode','string')
335 if mode not in ['string','list']:
335 if mode not in ['string','list']:
336 raise ValueError,'incorrect mode given: %s' % mode
336 raise ValueError,'incorrect mode given: %s' % mode
337 # Get options
337 # Get options
338 list_all = kw.get('list_all',0)
338 list_all = kw.get('list_all',0)
339 posix = kw.get('posix',True)
339 posix = kw.get('posix',True)
340
340
341 # Check if we have more than one argument to warrant extra processing:
341 # Check if we have more than one argument to warrant extra processing:
342 odict = {} # Dictionary with options
342 odict = {} # Dictionary with options
343 args = arg_str.split()
343 args = arg_str.split()
344 if len(args) >= 1:
344 if len(args) >= 1:
345 # If the list of inputs only has 0 or 1 thing in it, there's no
345 # If the list of inputs only has 0 or 1 thing in it, there's no
346 # need to look for options
346 # need to look for options
347 argv = arg_split(arg_str,posix)
347 argv = arg_split(arg_str,posix)
348 # Do regular option processing
348 # Do regular option processing
349 try:
349 try:
350 opts,args = getopt(argv,opt_str,*long_opts)
350 opts,args = getopt(argv,opt_str,*long_opts)
351 except GetoptError,e:
351 except GetoptError,e:
352 raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
352 raise GetoptError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
353 " ".join(long_opts)))
353 " ".join(long_opts)))
354 for o,a in opts:
354 for o,a in opts:
355 if o.startswith('--'):
355 if o.startswith('--'):
356 o = o[2:]
356 o = o[2:]
357 else:
357 else:
358 o = o[1:]
358 o = o[1:]
359 try:
359 try:
360 odict[o].append(a)
360 odict[o].append(a)
361 except AttributeError:
361 except AttributeError:
362 odict[o] = [odict[o],a]
362 odict[o] = [odict[o],a]
363 except KeyError:
363 except KeyError:
364 if list_all:
364 if list_all:
365 odict[o] = [a]
365 odict[o] = [a]
366 else:
366 else:
367 odict[o] = a
367 odict[o] = a
368
368
369 # Prepare opts,args for return
369 # Prepare opts,args for return
370 opts = Struct(odict)
370 opts = Struct(odict)
371 if mode == 'string':
371 if mode == 'string':
372 args = ' '.join(args)
372 args = ' '.join(args)
373
373
374 return opts,args
374 return opts,args
375
375
376 #......................................................................
376 #......................................................................
377 # And now the actual magic functions
377 # And now the actual magic functions
378
378
379 # Functions for IPython shell work (vars,funcs, config, etc)
379 # Functions for IPython shell work (vars,funcs, config, etc)
380 def magic_lsmagic(self, parameter_s = ''):
380 def magic_lsmagic(self, parameter_s = ''):
381 """List currently available magic functions."""
381 """List currently available magic functions."""
382 mesc = self.shell.ESC_MAGIC
382 mesc = self.shell.ESC_MAGIC
383 print 'Available magic functions:\n'+mesc+\
383 print 'Available magic functions:\n'+mesc+\
384 (' '+mesc).join(self.lsmagic())
384 (' '+mesc).join(self.lsmagic())
385 print '\n' + Magic.auto_status[self.shell.rc.automagic]
385 print '\n' + Magic.auto_status[self.shell.rc.automagic]
386 return None
386 return None
387
387
388 def magic_magic(self, parameter_s = ''):
388 def magic_magic(self, parameter_s = ''):
389 """Print information about the magic function system."""
389 """Print information about the magic function system."""
390
390
391 mode = ''
391 mode = ''
392 try:
392 try:
393 if parameter_s.split()[0] == '-latex':
393 if parameter_s.split()[0] == '-latex':
394 mode = 'latex'
394 mode = 'latex'
395 if parameter_s.split()[0] == '-brief':
395 if parameter_s.split()[0] == '-brief':
396 mode = 'brief'
396 mode = 'brief'
397 except:
397 except:
398 pass
398 pass
399
399
400 magic_docs = []
400 magic_docs = []
401 for fname in self.lsmagic():
401 for fname in self.lsmagic():
402 mname = 'magic_' + fname
402 mname = 'magic_' + fname
403 for space in (Magic,self,self.__class__):
403 for space in (Magic,self,self.__class__):
404 try:
404 try:
405 fn = space.__dict__[mname]
405 fn = space.__dict__[mname]
406 except KeyError:
406 except KeyError:
407 pass
407 pass
408 else:
408 else:
409 break
409 break
410 if mode == 'brief':
410 if mode == 'brief':
411 # only first line
411 # only first line
412 fndoc = fn.__doc__.split('\n',1)[0]
412 fndoc = fn.__doc__.split('\n',1)[0]
413 else:
413 else:
414 fndoc = fn.__doc__
414 fndoc = fn.__doc__
415
415
416 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
416 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
417 fname,fndoc))
417 fname,fndoc))
418 magic_docs = ''.join(magic_docs)
418 magic_docs = ''.join(magic_docs)
419
419
420 if mode == 'latex':
420 if mode == 'latex':
421 print self.format_latex(magic_docs)
421 print self.format_latex(magic_docs)
422 return
422 return
423 else:
423 else:
424 magic_docs = self.format_screen(magic_docs)
424 magic_docs = self.format_screen(magic_docs)
425 if mode == 'brief':
425 if mode == 'brief':
426 return magic_docs
426 return magic_docs
427
427
428 outmsg = """
428 outmsg = """
429 IPython's 'magic' functions
429 IPython's 'magic' functions
430 ===========================
430 ===========================
431
431
432 The magic function system provides a series of functions which allow you to
432 The magic function system provides a series of functions which allow you to
433 control the behavior of IPython itself, plus a lot of system-type
433 control the behavior of IPython itself, plus a lot of system-type
434 features. All these functions are prefixed with a % character, but parameters
434 features. All these functions are prefixed with a % character, but parameters
435 are given without parentheses or quotes.
435 are given without parentheses or quotes.
436
436
437 NOTE: If you have 'automagic' enabled (via the command line option or with the
437 NOTE: If you have 'automagic' enabled (via the command line option or with the
438 %automagic function), you don't need to type in the % explicitly. By default,
438 %automagic function), you don't need to type in the % explicitly. By default,
439 IPython ships with automagic on, so you should only rarely need the % escape.
439 IPython ships with automagic on, so you should only rarely need the % escape.
440
440
441 Example: typing '%cd mydir' (without the quotes) changes you working directory
441 Example: typing '%cd mydir' (without the quotes) changes you working directory
442 to 'mydir', if it exists.
442 to 'mydir', if it exists.
443
443
444 You can define your own magic functions to extend the system. See the supplied
444 You can define your own magic functions to extend the system. See the supplied
445 ipythonrc and example-magic.py files for details (in your ipython
445 ipythonrc and example-magic.py files for details (in your ipython
446 configuration directory, typically $HOME/.ipython/).
446 configuration directory, typically $HOME/.ipython/).
447
447
448 You can also define your own aliased names for magic functions. In your
448 You can also define your own aliased names for magic functions. In your
449 ipythonrc file, placing a line like:
449 ipythonrc file, placing a line like:
450
450
451 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
451 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
452
452
453 will define %pf as a new name for %profile.
453 will define %pf as a new name for %profile.
454
454
455 You can also call magics in code using the ipmagic() function, which IPython
455 You can also call magics in code using the ipmagic() function, which IPython
456 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
456 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
457
457
458 For a list of the available magic functions, use %lsmagic. For a description
458 For a list of the available magic functions, use %lsmagic. For a description
459 of any of them, type %magic_name?, e.g. '%cd?'.
459 of any of them, type %magic_name?, e.g. '%cd?'.
460
460
461 Currently the magic system has the following functions:\n"""
461 Currently the magic system has the following functions:\n"""
462
462
463 mesc = self.shell.ESC_MAGIC
463 mesc = self.shell.ESC_MAGIC
464 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
464 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
465 "\n\n%s%s\n\n%s" % (outmsg,
465 "\n\n%s%s\n\n%s" % (outmsg,
466 magic_docs,mesc,mesc,
466 magic_docs,mesc,mesc,
467 (' '+mesc).join(self.lsmagic()),
467 (' '+mesc).join(self.lsmagic()),
468 Magic.auto_status[self.shell.rc.automagic] ) )
468 Magic.auto_status[self.shell.rc.automagic] ) )
469
469
470 page(outmsg,screen_lines=self.shell.rc.screen_length)
470 page(outmsg,screen_lines=self.shell.rc.screen_length)
471
471
472
472
473 def magic_autoindent(self, parameter_s = ''):
473 def magic_autoindent(self, parameter_s = ''):
474 """Toggle autoindent on/off (if available)."""
474 """Toggle autoindent on/off (if available)."""
475
475
476 self.shell.set_autoindent()
476 self.shell.set_autoindent()
477 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
477 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
478
478
479 def magic_system_verbose(self, parameter_s = ''):
479 def magic_system_verbose(self, parameter_s = ''):
480 """Set verbose printing of system calls.
480 """Set verbose printing of system calls.
481
481
482 If called without an argument, act as a toggle"""
482 If called without an argument, act as a toggle"""
483
483
484 if parameter_s:
484 if parameter_s:
485 val = bool(eval(parameter_s))
485 val = bool(eval(parameter_s))
486 else:
486 else:
487 val = None
487 val = None
488
488
489 self.shell.rc_set_toggle('system_verbose',val)
489 self.shell.rc_set_toggle('system_verbose',val)
490 print "System verbose printing is:",\
490 print "System verbose printing is:",\
491 ['OFF','ON'][self.shell.rc.system_verbose]
491 ['OFF','ON'][self.shell.rc.system_verbose]
492
492
493
493
494 def magic_page(self, parameter_s=''):
494 def magic_page(self, parameter_s=''):
495 """Pretty print the object and display it through a pager.
495 """Pretty print the object and display it through a pager.
496
496
497 %page [options] OBJECT
497 %page [options] OBJECT
498
498
499 If no object is given, use _ (last output).
499 If no object is given, use _ (last output).
500
500
501 Options:
501 Options:
502
502
503 -r: page str(object), don't pretty-print it."""
503 -r: page str(object), don't pretty-print it."""
504
504
505 # After a function contributed by Olivier Aubert, slightly modified.
505 # After a function contributed by Olivier Aubert, slightly modified.
506
506
507 # Process options/args
507 # Process options/args
508 opts,args = self.parse_options(parameter_s,'r')
508 opts,args = self.parse_options(parameter_s,'r')
509 raw = 'r' in opts
509 raw = 'r' in opts
510
510
511 oname = args and args or '_'
511 oname = args and args or '_'
512 info = self._ofind(oname)
512 info = self._ofind(oname)
513 if info['found']:
513 if info['found']:
514 txt = (raw and str or pformat)( info['obj'] )
514 txt = (raw and str or pformat)( info['obj'] )
515 page(txt)
515 page(txt)
516 else:
516 else:
517 print 'Object `%s` not found' % oname
517 print 'Object `%s` not found' % oname
518
518
519 def magic_profile(self, parameter_s=''):
519 def magic_profile(self, parameter_s=''):
520 """Print your currently active IPyhton profile."""
520 """Print your currently active IPyhton profile."""
521 if self.shell.rc.profile:
521 if self.shell.rc.profile:
522 printpl('Current IPython profile: $self.shell.rc.profile.')
522 printpl('Current IPython profile: $self.shell.rc.profile.')
523 else:
523 else:
524 print 'No profile active.'
524 print 'No profile active.'
525
525
526 def magic_pinfo(self, parameter_s='', namespaces=None):
526 def magic_pinfo(self, parameter_s='', namespaces=None):
527 """Provide detailed information about an object.
527 """Provide detailed information about an object.
528
528
529 '%pinfo object' is just a synonym for object? or ?object."""
529 '%pinfo object' is just a synonym for object? or ?object."""
530
530
531 #print 'pinfo par: <%s>' % parameter_s # dbg
531 #print 'pinfo par: <%s>' % parameter_s # dbg
532
532
533
533
534 # detail_level: 0 -> obj? , 1 -> obj??
534 # detail_level: 0 -> obj? , 1 -> obj??
535 detail_level = 0
535 detail_level = 0
536 # We need to detect if we got called as 'pinfo pinfo foo', which can
536 # We need to detect if we got called as 'pinfo pinfo foo', which can
537 # happen if the user types 'pinfo foo?' at the cmd line.
537 # happen if the user types 'pinfo foo?' at the cmd line.
538 pinfo,qmark1,oname,qmark2 = \
538 pinfo,qmark1,oname,qmark2 = \
539 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
539 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
540 if pinfo or qmark1 or qmark2:
540 if pinfo or qmark1 or qmark2:
541 detail_level = 1
541 detail_level = 1
542 if "*" in oname:
542 if "*" in oname:
543 self.magic_psearch(oname)
543 self.magic_psearch(oname)
544 else:
544 else:
545 self._inspect('pinfo', oname, detail_level=detail_level,
545 self._inspect('pinfo', oname, detail_level=detail_level,
546 namespaces=namespaces)
546 namespaces=namespaces)
547
547
548 def _inspect(self,meth,oname,namespaces=None,**kw):
548 def _inspect(self,meth,oname,namespaces=None,**kw):
549 """Generic interface to the inspector system.
549 """Generic interface to the inspector system.
550
550
551 This function is meant to be called by pdef, pdoc & friends."""
551 This function is meant to be called by pdef, pdoc & friends."""
552
552
553 #oname = oname.strip()
553 #oname = oname.strip()
554 #print '1- oname: <%r>' % oname # dbg
554 #print '1- oname: <%r>' % oname # dbg
555 try:
555 try:
556 oname = oname.strip().encode('ascii')
556 oname = oname.strip().encode('ascii')
557 #print '2- oname: <%r>' % oname # dbg
557 #print '2- oname: <%r>' % oname # dbg
558 except UnicodeEncodeError:
558 except UnicodeEncodeError:
559 print 'Python identifiers can only contain ascii characters.'
559 print 'Python identifiers can only contain ascii characters.'
560 return 'not found'
560 return 'not found'
561
561
562 info = Struct(self._ofind(oname, namespaces))
562 info = Struct(self._ofind(oname, namespaces))
563
563
564 if info.found:
564 if info.found:
565 try:
565 try:
566 IPython.generics.inspect_object(info.obj)
566 IPython.generics.inspect_object(info.obj)
567 return
567 return
568 except IPython.ipapi.TryNext:
568 except IPython.ipapi.TryNext:
569 pass
569 pass
570 # Get the docstring of the class property if it exists.
570 # Get the docstring of the class property if it exists.
571 path = oname.split('.')
571 path = oname.split('.')
572 root = '.'.join(path[:-1])
572 root = '.'.join(path[:-1])
573 if info.parent is not None:
573 if info.parent is not None:
574 try:
574 try:
575 target = getattr(info.parent, '__class__')
575 target = getattr(info.parent, '__class__')
576 # The object belongs to a class instance.
576 # The object belongs to a class instance.
577 try:
577 try:
578 target = getattr(target, path[-1])
578 target = getattr(target, path[-1])
579 # The class defines the object.
579 # The class defines the object.
580 if isinstance(target, property):
580 if isinstance(target, property):
581 oname = root + '.__class__.' + path[-1]
581 oname = root + '.__class__.' + path[-1]
582 info = Struct(self._ofind(oname))
582 info = Struct(self._ofind(oname))
583 except AttributeError: pass
583 except AttributeError: pass
584 except AttributeError: pass
584 except AttributeError: pass
585
585
586 pmethod = getattr(self.shell.inspector,meth)
586 pmethod = getattr(self.shell.inspector,meth)
587 formatter = info.ismagic and self.format_screen or None
587 formatter = info.ismagic and self.format_screen or None
588 if meth == 'pdoc':
588 if meth == 'pdoc':
589 pmethod(info.obj,oname,formatter)
589 pmethod(info.obj,oname,formatter)
590 elif meth == 'pinfo':
590 elif meth == 'pinfo':
591 pmethod(info.obj,oname,formatter,info,**kw)
591 pmethod(info.obj,oname,formatter,info,**kw)
592 else:
592 else:
593 pmethod(info.obj,oname)
593 pmethod(info.obj,oname)
594 else:
594 else:
595 print 'Object `%s` not found.' % oname
595 print 'Object `%s` not found.' % oname
596 return 'not found' # so callers can take other action
596 return 'not found' # so callers can take other action
597
597
598 def magic_psearch(self, parameter_s=''):
598 def magic_psearch(self, parameter_s=''):
599 """Search for object in namespaces by wildcard.
599 """Search for object in namespaces by wildcard.
600
600
601 %psearch [options] PATTERN [OBJECT TYPE]
601 %psearch [options] PATTERN [OBJECT TYPE]
602
602
603 Note: ? can be used as a synonym for %psearch, at the beginning or at
603 Note: ? can be used as a synonym for %psearch, at the beginning or at
604 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
604 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
605 rest of the command line must be unchanged (options come first), so
605 rest of the command line must be unchanged (options come first), so
606 for example the following forms are equivalent
606 for example the following forms are equivalent
607
607
608 %psearch -i a* function
608 %psearch -i a* function
609 -i a* function?
609 -i a* function?
610 ?-i a* function
610 ?-i a* function
611
611
612 Arguments:
612 Arguments:
613
613
614 PATTERN
614 PATTERN
615
615
616 where PATTERN is a string containing * as a wildcard similar to its
616 where PATTERN is a string containing * as a wildcard similar to its
617 use in a shell. The pattern is matched in all namespaces on the
617 use in a shell. The pattern is matched in all namespaces on the
618 search path. By default objects starting with a single _ are not
618 search path. By default objects starting with a single _ are not
619 matched, many IPython generated objects have a single
619 matched, many IPython generated objects have a single
620 underscore. The default is case insensitive matching. Matching is
620 underscore. The default is case insensitive matching. Matching is
621 also done on the attributes of objects and not only on the objects
621 also done on the attributes of objects and not only on the objects
622 in a module.
622 in a module.
623
623
624 [OBJECT TYPE]
624 [OBJECT TYPE]
625
625
626 Is the name of a python type from the types module. The name is
626 Is the name of a python type from the types module. The name is
627 given in lowercase without the ending type, ex. StringType is
627 given in lowercase without the ending type, ex. StringType is
628 written string. By adding a type here only objects matching the
628 written string. By adding a type here only objects matching the
629 given type are matched. Using all here makes the pattern match all
629 given type are matched. Using all here makes the pattern match all
630 types (this is the default).
630 types (this is the default).
631
631
632 Options:
632 Options:
633
633
634 -a: makes the pattern match even objects whose names start with a
634 -a: makes the pattern match even objects whose names start with a
635 single underscore. These names are normally ommitted from the
635 single underscore. These names are normally ommitted from the
636 search.
636 search.
637
637
638 -i/-c: make the pattern case insensitive/sensitive. If neither of
638 -i/-c: make the pattern case insensitive/sensitive. If neither of
639 these options is given, the default is read from your ipythonrc
639 these options is given, the default is read from your ipythonrc
640 file. The option name which sets this value is
640 file. The option name which sets this value is
641 'wildcards_case_sensitive'. If this option is not specified in your
641 'wildcards_case_sensitive'. If this option is not specified in your
642 ipythonrc file, IPython's internal default is to do a case sensitive
642 ipythonrc file, IPython's internal default is to do a case sensitive
643 search.
643 search.
644
644
645 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
645 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
646 specifiy can be searched in any of the following namespaces:
646 specifiy can be searched in any of the following namespaces:
647 'builtin', 'user', 'user_global','internal', 'alias', where
647 'builtin', 'user', 'user_global','internal', 'alias', where
648 'builtin' and 'user' are the search defaults. Note that you should
648 'builtin' and 'user' are the search defaults. Note that you should
649 not use quotes when specifying namespaces.
649 not use quotes when specifying namespaces.
650
650
651 'Builtin' contains the python module builtin, 'user' contains all
651 'Builtin' contains the python module builtin, 'user' contains all
652 user data, 'alias' only contain the shell aliases and no python
652 user data, 'alias' only contain the shell aliases and no python
653 objects, 'internal' contains objects used by IPython. The
653 objects, 'internal' contains objects used by IPython. The
654 'user_global' namespace is only used by embedded IPython instances,
654 'user_global' namespace is only used by embedded IPython instances,
655 and it contains module-level globals. You can add namespaces to the
655 and it contains module-level globals. You can add namespaces to the
656 search with -s or exclude them with -e (these options can be given
656 search with -s or exclude them with -e (these options can be given
657 more than once).
657 more than once).
658
658
659 Examples:
659 Examples:
660
660
661 %psearch a* -> objects beginning with an a
661 %psearch a* -> objects beginning with an a
662 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
662 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
663 %psearch a* function -> all functions beginning with an a
663 %psearch a* function -> all functions beginning with an a
664 %psearch re.e* -> objects beginning with an e in module re
664 %psearch re.e* -> objects beginning with an e in module re
665 %psearch r*.e* -> objects that start with e in modules starting in r
665 %psearch r*.e* -> objects that start with e in modules starting in r
666 %psearch r*.* string -> all strings in modules beginning with r
666 %psearch r*.* string -> all strings in modules beginning with r
667
667
668 Case sensitve search:
668 Case sensitve search:
669
669
670 %psearch -c a* list all object beginning with lower case a
670 %psearch -c a* list all object beginning with lower case a
671
671
672 Show objects beginning with a single _:
672 Show objects beginning with a single _:
673
673
674 %psearch -a _* list objects beginning with a single underscore"""
674 %psearch -a _* list objects beginning with a single underscore"""
675 try:
675 try:
676 parameter_s = parameter_s.encode('ascii')
676 parameter_s = parameter_s.encode('ascii')
677 except UnicodeEncodeError:
677 except UnicodeEncodeError:
678 print 'Python identifiers can only contain ascii characters.'
678 print 'Python identifiers can only contain ascii characters.'
679 return
679 return
680
680
681 # default namespaces to be searched
681 # default namespaces to be searched
682 def_search = ['user','builtin']
682 def_search = ['user','builtin']
683
683
684 # Process options/args
684 # Process options/args
685 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
685 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
686 opt = opts.get
686 opt = opts.get
687 shell = self.shell
687 shell = self.shell
688 psearch = shell.inspector.psearch
688 psearch = shell.inspector.psearch
689
689
690 # select case options
690 # select case options
691 if opts.has_key('i'):
691 if opts.has_key('i'):
692 ignore_case = True
692 ignore_case = True
693 elif opts.has_key('c'):
693 elif opts.has_key('c'):
694 ignore_case = False
694 ignore_case = False
695 else:
695 else:
696 ignore_case = not shell.rc.wildcards_case_sensitive
696 ignore_case = not shell.rc.wildcards_case_sensitive
697
697
698 # Build list of namespaces to search from user options
698 # Build list of namespaces to search from user options
699 def_search.extend(opt('s',[]))
699 def_search.extend(opt('s',[]))
700 ns_exclude = ns_exclude=opt('e',[])
700 ns_exclude = ns_exclude=opt('e',[])
701 ns_search = [nm for nm in def_search if nm not in ns_exclude]
701 ns_search = [nm for nm in def_search if nm not in ns_exclude]
702
702
703 # Call the actual search
703 # Call the actual search
704 try:
704 try:
705 psearch(args,shell.ns_table,ns_search,
705 psearch(args,shell.ns_table,ns_search,
706 show_all=opt('a'),ignore_case=ignore_case)
706 show_all=opt('a'),ignore_case=ignore_case)
707 except:
707 except:
708 shell.showtraceback()
708 shell.showtraceback()
709
709
710 def magic_who_ls(self, parameter_s=''):
710 def magic_who_ls(self, parameter_s=''):
711 """Return a sorted list of all interactive variables.
711 """Return a sorted list of all interactive variables.
712
712
713 If arguments are given, only variables of types matching these
713 If arguments are given, only variables of types matching these
714 arguments are returned."""
714 arguments are returned."""
715
715
716 user_ns = self.shell.user_ns
716 user_ns = self.shell.user_ns
717 internal_ns = self.shell.internal_ns
717 internal_ns = self.shell.internal_ns
718 user_config_ns = self.shell.user_config_ns
718 user_config_ns = self.shell.user_config_ns
719 out = []
719 out = []
720 typelist = parameter_s.split()
720 typelist = parameter_s.split()
721
721
722 for i in user_ns:
722 for i in user_ns:
723 if not (i.startswith('_') or i.startswith('_i')) \
723 if not (i.startswith('_') or i.startswith('_i')) \
724 and not (i in internal_ns or i in user_config_ns):
724 and not (i in internal_ns or i in user_config_ns):
725 if typelist:
725 if typelist:
726 if type(user_ns[i]).__name__ in typelist:
726 if type(user_ns[i]).__name__ in typelist:
727 out.append(i)
727 out.append(i)
728 else:
728 else:
729 out.append(i)
729 out.append(i)
730 out.sort()
730 out.sort()
731 return out
731 return out
732
732
733 def magic_who(self, parameter_s=''):
733 def magic_who(self, parameter_s=''):
734 """Print all interactive variables, with some minimal formatting.
734 """Print all interactive variables, with some minimal formatting.
735
735
736 If any arguments are given, only variables whose type matches one of
736 If any arguments are given, only variables whose type matches one of
737 these are printed. For example:
737 these are printed. For example:
738
738
739 %who function str
739 %who function str
740
740
741 will only list functions and strings, excluding all other types of
741 will only list functions and strings, excluding all other types of
742 variables. To find the proper type names, simply use type(var) at a
742 variables. To find the proper type names, simply use type(var) at a
743 command line to see how python prints type names. For example:
743 command line to see how python prints type names. For example:
744
744
745 In [1]: type('hello')\\
745 In [1]: type('hello')\\
746 Out[1]: <type 'str'>
746 Out[1]: <type 'str'>
747
747
748 indicates that the type name for strings is 'str'.
748 indicates that the type name for strings is 'str'.
749
749
750 %who always excludes executed names loaded through your configuration
750 %who always excludes executed names loaded through your configuration
751 file and things which are internal to IPython.
751 file and things which are internal to IPython.
752
752
753 This is deliberate, as typically you may load many modules and the
753 This is deliberate, as typically you may load many modules and the
754 purpose of %who is to show you only what you've manually defined."""
754 purpose of %who is to show you only what you've manually defined."""
755
755
756 varlist = self.magic_who_ls(parameter_s)
756 varlist = self.magic_who_ls(parameter_s)
757 if not varlist:
757 if not varlist:
758 if parameter_s:
758 if parameter_s:
759 print 'No variables match your requested type.'
759 print 'No variables match your requested type.'
760 else:
760 else:
761 print 'Interactive namespace is empty.'
761 print 'Interactive namespace is empty.'
762 return
762 return
763
763
764 # if we have variables, move on...
764 # if we have variables, move on...
765 count = 0
765 count = 0
766 for i in varlist:
766 for i in varlist:
767 print i+'\t',
767 print i+'\t',
768 count += 1
768 count += 1
769 if count > 8:
769 if count > 8:
770 count = 0
770 count = 0
771 print
771 print
772 print
772 print
773
773
774 def magic_whos(self, parameter_s=''):
774 def magic_whos(self, parameter_s=''):
775 """Like %who, but gives some extra information about each variable.
775 """Like %who, but gives some extra information about each variable.
776
776
777 The same type filtering of %who can be applied here.
777 The same type filtering of %who can be applied here.
778
778
779 For all variables, the type is printed. Additionally it prints:
779 For all variables, the type is printed. Additionally it prints:
780
780
781 - For {},[],(): their length.
781 - For {},[],(): their length.
782
782
783 - For numpy and Numeric arrays, a summary with shape, number of
783 - For numpy and Numeric arrays, a summary with shape, number of
784 elements, typecode and size in memory.
784 elements, typecode and size in memory.
785
785
786 - Everything else: a string representation, snipping their middle if
786 - Everything else: a string representation, snipping their middle if
787 too long."""
787 too long."""
788
788
789 varnames = self.magic_who_ls(parameter_s)
789 varnames = self.magic_who_ls(parameter_s)
790 if not varnames:
790 if not varnames:
791 if parameter_s:
791 if parameter_s:
792 print 'No variables match your requested type.'
792 print 'No variables match your requested type.'
793 else:
793 else:
794 print 'Interactive namespace is empty.'
794 print 'Interactive namespace is empty.'
795 return
795 return
796
796
797 # if we have variables, move on...
797 # if we have variables, move on...
798
798
799 # for these types, show len() instead of data:
799 # for these types, show len() instead of data:
800 seq_types = [types.DictType,types.ListType,types.TupleType]
800 seq_types = [types.DictType,types.ListType,types.TupleType]
801
801
802 # for numpy/Numeric arrays, display summary info
802 # for numpy/Numeric arrays, display summary info
803 try:
803 try:
804 import numpy
804 import numpy
805 except ImportError:
805 except ImportError:
806 ndarray_type = None
806 ndarray_type = None
807 else:
807 else:
808 ndarray_type = numpy.ndarray.__name__
808 ndarray_type = numpy.ndarray.__name__
809 try:
809 try:
810 import Numeric
810 import Numeric
811 except ImportError:
811 except ImportError:
812 array_type = None
812 array_type = None
813 else:
813 else:
814 array_type = Numeric.ArrayType.__name__
814 array_type = Numeric.ArrayType.__name__
815
815
816 # Find all variable names and types so we can figure out column sizes
816 # Find all variable names and types so we can figure out column sizes
817 def get_vars(i):
817 def get_vars(i):
818 return self.shell.user_ns[i]
818 return self.shell.user_ns[i]
819
819
820 # some types are well known and can be shorter
820 # some types are well known and can be shorter
821 abbrevs = {'IPython.macro.Macro' : 'Macro'}
821 abbrevs = {'IPython.macro.Macro' : 'Macro'}
822 def type_name(v):
822 def type_name(v):
823 tn = type(v).__name__
823 tn = type(v).__name__
824 return abbrevs.get(tn,tn)
824 return abbrevs.get(tn,tn)
825
825
826 varlist = map(get_vars,varnames)
826 varlist = map(get_vars,varnames)
827
827
828 typelist = []
828 typelist = []
829 for vv in varlist:
829 for vv in varlist:
830 tt = type_name(vv)
830 tt = type_name(vv)
831
831
832 if tt=='instance':
832 if tt=='instance':
833 typelist.append( abbrevs.get(str(vv.__class__),
833 typelist.append( abbrevs.get(str(vv.__class__),
834 str(vv.__class__)))
834 str(vv.__class__)))
835 else:
835 else:
836 typelist.append(tt)
836 typelist.append(tt)
837
837
838 # column labels and # of spaces as separator
838 # column labels and # of spaces as separator
839 varlabel = 'Variable'
839 varlabel = 'Variable'
840 typelabel = 'Type'
840 typelabel = 'Type'
841 datalabel = 'Data/Info'
841 datalabel = 'Data/Info'
842 colsep = 3
842 colsep = 3
843 # variable format strings
843 # variable format strings
844 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
844 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
845 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
845 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
846 aformat = "%s: %s elems, type `%s`, %s bytes"
846 aformat = "%s: %s elems, type `%s`, %s bytes"
847 # find the size of the columns to format the output nicely
847 # find the size of the columns to format the output nicely
848 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
848 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
849 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
849 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
850 # table header
850 # table header
851 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
851 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
852 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
852 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
853 # and the table itself
853 # and the table itself
854 kb = 1024
854 kb = 1024
855 Mb = 1048576 # kb**2
855 Mb = 1048576 # kb**2
856 for vname,var,vtype in zip(varnames,varlist,typelist):
856 for vname,var,vtype in zip(varnames,varlist,typelist):
857 print itpl(vformat),
857 print itpl(vformat),
858 if vtype in seq_types:
858 if vtype in seq_types:
859 print len(var)
859 print len(var)
860 elif vtype in [array_type,ndarray_type]:
860 elif vtype in [array_type,ndarray_type]:
861 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
861 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
862 if vtype==ndarray_type:
862 if vtype==ndarray_type:
863 # numpy
863 # numpy
864 vsize = var.size
864 vsize = var.size
865 vbytes = vsize*var.itemsize
865 vbytes = vsize*var.itemsize
866 vdtype = var.dtype
866 vdtype = var.dtype
867 else:
867 else:
868 # Numeric
868 # Numeric
869 vsize = Numeric.size(var)
869 vsize = Numeric.size(var)
870 vbytes = vsize*var.itemsize()
870 vbytes = vsize*var.itemsize()
871 vdtype = var.typecode()
871 vdtype = var.typecode()
872
872
873 if vbytes < 100000:
873 if vbytes < 100000:
874 print aformat % (vshape,vsize,vdtype,vbytes)
874 print aformat % (vshape,vsize,vdtype,vbytes)
875 else:
875 else:
876 print aformat % (vshape,vsize,vdtype,vbytes),
876 print aformat % (vshape,vsize,vdtype,vbytes),
877 if vbytes < Mb:
877 if vbytes < Mb:
878 print '(%s kb)' % (vbytes/kb,)
878 print '(%s kb)' % (vbytes/kb,)
879 else:
879 else:
880 print '(%s Mb)' % (vbytes/Mb,)
880 print '(%s Mb)' % (vbytes/Mb,)
881 else:
881 else:
882 try:
882 try:
883 vstr = str(var)
883 vstr = str(var)
884 except UnicodeEncodeError:
884 except UnicodeEncodeError:
885 vstr = unicode(var).encode(sys.getdefaultencoding(),
885 vstr = unicode(var).encode(sys.getdefaultencoding(),
886 'backslashreplace')
886 'backslashreplace')
887 vstr = vstr.replace('\n','\\n')
887 vstr = vstr.replace('\n','\\n')
888 if len(vstr) < 50:
888 if len(vstr) < 50:
889 print vstr
889 print vstr
890 else:
890 else:
891 printpl(vfmt_short)
891 printpl(vfmt_short)
892
892
893 def magic_reset(self, parameter_s=''):
893 def magic_reset(self, parameter_s=''):
894 """Resets the namespace by removing all names defined by the user.
894 """Resets the namespace by removing all names defined by the user.
895
895
896 Input/Output history are left around in case you need them."""
896 Input/Output history are left around in case you need them."""
897
897
898 ans = self.shell.ask_yes_no(
898 ans = self.shell.ask_yes_no(
899 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
899 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
900 if not ans:
900 if not ans:
901 print 'Nothing done.'
901 print 'Nothing done.'
902 return
902 return
903 user_ns = self.shell.user_ns
903 user_ns = self.shell.user_ns
904 for i in self.magic_who_ls():
904 for i in self.magic_who_ls():
905 del(user_ns[i])
905 del(user_ns[i])
906
906
907 def magic_logstart(self,parameter_s=''):
907 def magic_logstart(self,parameter_s=''):
908 """Start logging anywhere in a session.
908 """Start logging anywhere in a session.
909
909
910 %logstart [-o|-r|-t] [log_name [log_mode]]
910 %logstart [-o|-r|-t] [log_name [log_mode]]
911
911
912 If no name is given, it defaults to a file named 'ipython_log.py' in your
912 If no name is given, it defaults to a file named 'ipython_log.py' in your
913 current directory, in 'rotate' mode (see below).
913 current directory, in 'rotate' mode (see below).
914
914
915 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
915 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
916 history up to that point and then continues logging.
916 history up to that point and then continues logging.
917
917
918 %logstart takes a second optional parameter: logging mode. This can be one
918 %logstart takes a second optional parameter: logging mode. This can be one
919 of (note that the modes are given unquoted):\\
919 of (note that the modes are given unquoted):\\
920 append: well, that says it.\\
920 append: well, that says it.\\
921 backup: rename (if exists) to name~ and start name.\\
921 backup: rename (if exists) to name~ and start name.\\
922 global: single logfile in your home dir, appended to.\\
922 global: single logfile in your home dir, appended to.\\
923 over : overwrite existing log.\\
923 over : overwrite existing log.\\
924 rotate: create rotating logs name.1~, name.2~, etc.
924 rotate: create rotating logs name.1~, name.2~, etc.
925
925
926 Options:
926 Options:
927
927
928 -o: log also IPython's output. In this mode, all commands which
928 -o: log also IPython's output. In this mode, all commands which
929 generate an Out[NN] prompt are recorded to the logfile, right after
929 generate an Out[NN] prompt are recorded to the logfile, right after
930 their corresponding input line. The output lines are always
930 their corresponding input line. The output lines are always
931 prepended with a '#[Out]# ' marker, so that the log remains valid
931 prepended with a '#[Out]# ' marker, so that the log remains valid
932 Python code.
932 Python code.
933
933
934 Since this marker is always the same, filtering only the output from
934 Since this marker is always the same, filtering only the output from
935 a log is very easy, using for example a simple awk call:
935 a log is very easy, using for example a simple awk call:
936
936
937 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
937 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
938
938
939 -r: log 'raw' input. Normally, IPython's logs contain the processed
939 -r: log 'raw' input. Normally, IPython's logs contain the processed
940 input, so that user lines are logged in their final form, converted
940 input, so that user lines are logged in their final form, converted
941 into valid Python. For example, %Exit is logged as
941 into valid Python. For example, %Exit is logged as
942 '_ip.magic("Exit"). If the -r flag is given, all input is logged
942 '_ip.magic("Exit"). If the -r flag is given, all input is logged
943 exactly as typed, with no transformations applied.
943 exactly as typed, with no transformations applied.
944
944
945 -t: put timestamps before each input line logged (these are put in
945 -t: put timestamps before each input line logged (these are put in
946 comments)."""
946 comments)."""
947
947
948 opts,par = self.parse_options(parameter_s,'ort')
948 opts,par = self.parse_options(parameter_s,'ort')
949 log_output = 'o' in opts
949 log_output = 'o' in opts
950 log_raw_input = 'r' in opts
950 log_raw_input = 'r' in opts
951 timestamp = 't' in opts
951 timestamp = 't' in opts
952
952
953 rc = self.shell.rc
953 rc = self.shell.rc
954 logger = self.shell.logger
954 logger = self.shell.logger
955
955
956 # if no args are given, the defaults set in the logger constructor by
956 # if no args are given, the defaults set in the logger constructor by
957 # ipytohn remain valid
957 # ipytohn remain valid
958 if par:
958 if par:
959 try:
959 try:
960 logfname,logmode = par.split()
960 logfname,logmode = par.split()
961 except:
961 except:
962 logfname = par
962 logfname = par
963 logmode = 'backup'
963 logmode = 'backup'
964 else:
964 else:
965 logfname = logger.logfname
965 logfname = logger.logfname
966 logmode = logger.logmode
966 logmode = logger.logmode
967 # put logfname into rc struct as if it had been called on the command
967 # put logfname into rc struct as if it had been called on the command
968 # line, so it ends up saved in the log header Save it in case we need
968 # line, so it ends up saved in the log header Save it in case we need
969 # to restore it...
969 # to restore it...
970 old_logfile = rc.opts.get('logfile','')
970 old_logfile = rc.opts.get('logfile','')
971 if logfname:
971 if logfname:
972 logfname = os.path.expanduser(logfname)
972 logfname = os.path.expanduser(logfname)
973 rc.opts.logfile = logfname
973 rc.opts.logfile = logfname
974 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
974 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
975 try:
975 try:
976 started = logger.logstart(logfname,loghead,logmode,
976 started = logger.logstart(logfname,loghead,logmode,
977 log_output,timestamp,log_raw_input)
977 log_output,timestamp,log_raw_input)
978 except:
978 except:
979 rc.opts.logfile = old_logfile
979 rc.opts.logfile = old_logfile
980 warn("Couldn't start log: %s" % sys.exc_info()[1])
980 warn("Couldn't start log: %s" % sys.exc_info()[1])
981 else:
981 else:
982 # log input history up to this point, optionally interleaving
982 # log input history up to this point, optionally interleaving
983 # output if requested
983 # output if requested
984
984
985 if timestamp:
985 if timestamp:
986 # disable timestamping for the previous history, since we've
986 # disable timestamping for the previous history, since we've
987 # lost those already (no time machine here).
987 # lost those already (no time machine here).
988 logger.timestamp = False
988 logger.timestamp = False
989
989
990 if log_raw_input:
990 if log_raw_input:
991 input_hist = self.shell.input_hist_raw
991 input_hist = self.shell.input_hist_raw
992 else:
992 else:
993 input_hist = self.shell.input_hist
993 input_hist = self.shell.input_hist
994
994
995 if log_output:
995 if log_output:
996 log_write = logger.log_write
996 log_write = logger.log_write
997 output_hist = self.shell.output_hist
997 output_hist = self.shell.output_hist
998 for n in range(1,len(input_hist)-1):
998 for n in range(1,len(input_hist)-1):
999 log_write(input_hist[n].rstrip())
999 log_write(input_hist[n].rstrip())
1000 if n in output_hist:
1000 if n in output_hist:
1001 log_write(repr(output_hist[n]),'output')
1001 log_write(repr(output_hist[n]),'output')
1002 else:
1002 else:
1003 logger.log_write(input_hist[1:])
1003 logger.log_write(input_hist[1:])
1004 if timestamp:
1004 if timestamp:
1005 # re-enable timestamping
1005 # re-enable timestamping
1006 logger.timestamp = True
1006 logger.timestamp = True
1007
1007
1008 print ('Activating auto-logging. '
1008 print ('Activating auto-logging. '
1009 'Current session state plus future input saved.')
1009 'Current session state plus future input saved.')
1010 logger.logstate()
1010 logger.logstate()
1011
1011
1012 def magic_logoff(self,parameter_s=''):
1012 def magic_logoff(self,parameter_s=''):
1013 """Temporarily stop logging.
1013 """Temporarily stop logging.
1014
1014
1015 You must have previously started logging."""
1015 You must have previously started logging."""
1016 self.shell.logger.switch_log(0)
1016 self.shell.logger.switch_log(0)
1017
1017
1018 def magic_logon(self,parameter_s=''):
1018 def magic_logon(self,parameter_s=''):
1019 """Restart logging.
1019 """Restart logging.
1020
1020
1021 This function is for restarting logging which you've temporarily
1021 This function is for restarting logging which you've temporarily
1022 stopped with %logoff. For starting logging for the first time, you
1022 stopped with %logoff. For starting logging for the first time, you
1023 must use the %logstart function, which allows you to specify an
1023 must use the %logstart function, which allows you to specify an
1024 optional log filename."""
1024 optional log filename."""
1025
1025
1026 self.shell.logger.switch_log(1)
1026 self.shell.logger.switch_log(1)
1027
1027
1028 def magic_logstate(self,parameter_s=''):
1028 def magic_logstate(self,parameter_s=''):
1029 """Print the status of the logging system."""
1029 """Print the status of the logging system."""
1030
1030
1031 self.shell.logger.logstate()
1031 self.shell.logger.logstate()
1032
1032
1033 def magic_pdb(self, parameter_s=''):
1033 def magic_pdb(self, parameter_s=''):
1034 """Control the automatic calling of the pdb interactive debugger.
1034 """Control the automatic calling of the pdb interactive debugger.
1035
1035
1036 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1036 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1037 argument it works as a toggle.
1037 argument it works as a toggle.
1038
1038
1039 When an exception is triggered, IPython can optionally call the
1039 When an exception is triggered, IPython can optionally call the
1040 interactive pdb debugger after the traceback printout. %pdb toggles
1040 interactive pdb debugger after the traceback printout. %pdb toggles
1041 this feature on and off.
1041 this feature on and off.
1042
1042
1043 The initial state of this feature is set in your ipythonrc
1043 The initial state of this feature is set in your ipythonrc
1044 configuration file (the variable is called 'pdb').
1044 configuration file (the variable is called 'pdb').
1045
1045
1046 If you want to just activate the debugger AFTER an exception has fired,
1046 If you want to just activate the debugger AFTER an exception has fired,
1047 without having to type '%pdb on' and rerunning your code, you can use
1047 without having to type '%pdb on' and rerunning your code, you can use
1048 the %debug magic."""
1048 the %debug magic."""
1049
1049
1050 par = parameter_s.strip().lower()
1050 par = parameter_s.strip().lower()
1051
1051
1052 if par:
1052 if par:
1053 try:
1053 try:
1054 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1054 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1055 except KeyError:
1055 except KeyError:
1056 print ('Incorrect argument. Use on/1, off/0, '
1056 print ('Incorrect argument. Use on/1, off/0, '
1057 'or nothing for a toggle.')
1057 'or nothing for a toggle.')
1058 return
1058 return
1059 else:
1059 else:
1060 # toggle
1060 # toggle
1061 new_pdb = not self.shell.call_pdb
1061 new_pdb = not self.shell.call_pdb
1062
1062
1063 # set on the shell
1063 # set on the shell
1064 self.shell.call_pdb = new_pdb
1064 self.shell.call_pdb = new_pdb
1065 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1065 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1066
1066
1067 def magic_debug(self, parameter_s=''):
1067 def magic_debug(self, parameter_s=''):
1068 """Activate the interactive debugger in post-mortem mode.
1068 """Activate the interactive debugger in post-mortem mode.
1069
1069
1070 If an exception has just occurred, this lets you inspect its stack
1070 If an exception has just occurred, this lets you inspect its stack
1071 frames interactively. Note that this will always work only on the last
1071 frames interactively. Note that this will always work only on the last
1072 traceback that occurred, so you must call this quickly after an
1072 traceback that occurred, so you must call this quickly after an
1073 exception that you wish to inspect has fired, because if another one
1073 exception that you wish to inspect has fired, because if another one
1074 occurs, it clobbers the previous one.
1074 occurs, it clobbers the previous one.
1075
1075
1076 If you want IPython to automatically do this on every exception, see
1076 If you want IPython to automatically do this on every exception, see
1077 the %pdb magic for more details.
1077 the %pdb magic for more details.
1078 """
1078 """
1079
1079
1080 self.shell.debugger(force=True)
1080 self.shell.debugger(force=True)
1081
1081
1082 def magic_prun(self, parameter_s ='',user_mode=1,
1082 def magic_prun(self, parameter_s ='',user_mode=1,
1083 opts=None,arg_lst=None,prog_ns=None):
1083 opts=None,arg_lst=None,prog_ns=None):
1084
1084
1085 """Run a statement through the python code profiler.
1085 """Run a statement through the python code profiler.
1086
1086
1087 Usage:\\
1087 Usage:\\
1088 %prun [options] statement
1088 %prun [options] statement
1089
1089
1090 The given statement (which doesn't require quote marks) is run via the
1090 The given statement (which doesn't require quote marks) is run via the
1091 python profiler in a manner similar to the profile.run() function.
1091 python profiler in a manner similar to the profile.run() function.
1092 Namespaces are internally managed to work correctly; profile.run
1092 Namespaces are internally managed to work correctly; profile.run
1093 cannot be used in IPython because it makes certain assumptions about
1093 cannot be used in IPython because it makes certain assumptions about
1094 namespaces which do not hold under IPython.
1094 namespaces which do not hold under IPython.
1095
1095
1096 Options:
1096 Options:
1097
1097
1098 -l <limit>: you can place restrictions on what or how much of the
1098 -l <limit>: you can place restrictions on what or how much of the
1099 profile gets printed. The limit value can be:
1099 profile gets printed. The limit value can be:
1100
1100
1101 * A string: only information for function names containing this string
1101 * A string: only information for function names containing this string
1102 is printed.
1102 is printed.
1103
1103
1104 * An integer: only these many lines are printed.
1104 * An integer: only these many lines are printed.
1105
1105
1106 * A float (between 0 and 1): this fraction of the report is printed
1106 * A float (between 0 and 1): this fraction of the report is printed
1107 (for example, use a limit of 0.4 to see the topmost 40% only).
1107 (for example, use a limit of 0.4 to see the topmost 40% only).
1108
1108
1109 You can combine several limits with repeated use of the option. For
1109 You can combine several limits with repeated use of the option. For
1110 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1110 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1111 information about class constructors.
1111 information about class constructors.
1112
1112
1113 -r: return the pstats.Stats object generated by the profiling. This
1113 -r: return the pstats.Stats object generated by the profiling. This
1114 object has all the information about the profile in it, and you can
1114 object has all the information about the profile in it, and you can
1115 later use it for further analysis or in other functions.
1115 later use it for further analysis or in other functions.
1116
1116
1117 -s <key>: sort profile by given key. You can provide more than one key
1117 -s <key>: sort profile by given key. You can provide more than one key
1118 by using the option several times: '-s key1 -s key2 -s key3...'. The
1118 by using the option several times: '-s key1 -s key2 -s key3...'. The
1119 default sorting key is 'time'.
1119 default sorting key is 'time'.
1120
1120
1121 The following is copied verbatim from the profile documentation
1121 The following is copied verbatim from the profile documentation
1122 referenced below:
1122 referenced below:
1123
1123
1124 When more than one key is provided, additional keys are used as
1124 When more than one key is provided, additional keys are used as
1125 secondary criteria when the there is equality in all keys selected
1125 secondary criteria when the there is equality in all keys selected
1126 before them.
1126 before them.
1127
1127
1128 Abbreviations can be used for any key names, as long as the
1128 Abbreviations can be used for any key names, as long as the
1129 abbreviation is unambiguous. The following are the keys currently
1129 abbreviation is unambiguous. The following are the keys currently
1130 defined:
1130 defined:
1131
1131
1132 Valid Arg Meaning\\
1132 Valid Arg Meaning\\
1133 "calls" call count\\
1133 "calls" call count\\
1134 "cumulative" cumulative time\\
1134 "cumulative" cumulative time\\
1135 "file" file name\\
1135 "file" file name\\
1136 "module" file name\\
1136 "module" file name\\
1137 "pcalls" primitive call count\\
1137 "pcalls" primitive call count\\
1138 "line" line number\\
1138 "line" line number\\
1139 "name" function name\\
1139 "name" function name\\
1140 "nfl" name/file/line\\
1140 "nfl" name/file/line\\
1141 "stdname" standard name\\
1141 "stdname" standard name\\
1142 "time" internal time
1142 "time" internal time
1143
1143
1144 Note that all sorts on statistics are in descending order (placing
1144 Note that all sorts on statistics are in descending order (placing
1145 most time consuming items first), where as name, file, and line number
1145 most time consuming items first), where as name, file, and line number
1146 searches are in ascending order (i.e., alphabetical). The subtle
1146 searches are in ascending order (i.e., alphabetical). The subtle
1147 distinction between "nfl" and "stdname" is that the standard name is a
1147 distinction between "nfl" and "stdname" is that the standard name is a
1148 sort of the name as printed, which means that the embedded line
1148 sort of the name as printed, which means that the embedded line
1149 numbers get compared in an odd way. For example, lines 3, 20, and 40
1149 numbers get compared in an odd way. For example, lines 3, 20, and 40
1150 would (if the file names were the same) appear in the string order
1150 would (if the file names were the same) appear in the string order
1151 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1151 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1152 line numbers. In fact, sort_stats("nfl") is the same as
1152 line numbers. In fact, sort_stats("nfl") is the same as
1153 sort_stats("name", "file", "line").
1153 sort_stats("name", "file", "line").
1154
1154
1155 -T <filename>: save profile results as shown on screen to a text
1155 -T <filename>: save profile results as shown on screen to a text
1156 file. The profile is still shown on screen.
1156 file. The profile is still shown on screen.
1157
1157
1158 -D <filename>: save (via dump_stats) profile statistics to given
1158 -D <filename>: save (via dump_stats) profile statistics to given
1159 filename. This data is in a format understod by the pstats module, and
1159 filename. This data is in a format understod by the pstats module, and
1160 is generated by a call to the dump_stats() method of profile
1160 is generated by a call to the dump_stats() method of profile
1161 objects. The profile is still shown on screen.
1161 objects. The profile is still shown on screen.
1162
1162
1163 If you want to run complete programs under the profiler's control, use
1163 If you want to run complete programs under the profiler's control, use
1164 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1164 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1165 contains profiler specific options as described here.
1165 contains profiler specific options as described here.
1166
1166
1167 You can read the complete documentation for the profile module with:\\
1167 You can read the complete documentation for the profile module with:\\
1168 In [1]: import profile; profile.help() """
1168 In [1]: import profile; profile.help() """
1169
1169
1170 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1170 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1171 # protect user quote marks
1171 # protect user quote marks
1172 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1172 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1173
1173
1174 if user_mode: # regular user call
1174 if user_mode: # regular user call
1175 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1175 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1176 list_all=1)
1176 list_all=1)
1177 namespace = self.shell.user_ns
1177 namespace = self.shell.user_ns
1178 else: # called to run a program by %run -p
1178 else: # called to run a program by %run -p
1179 try:
1179 try:
1180 filename = get_py_filename(arg_lst[0])
1180 filename = get_py_filename(arg_lst[0])
1181 except IOError,msg:
1181 except IOError,msg:
1182 error(msg)
1182 error(msg)
1183 return
1183 return
1184
1184
1185 arg_str = 'execfile(filename,prog_ns)'
1185 arg_str = 'execfile(filename,prog_ns)'
1186 namespace = locals()
1186 namespace = locals()
1187
1187
1188 opts.merge(opts_def)
1188 opts.merge(opts_def)
1189
1189
1190 prof = profile.Profile()
1190 prof = profile.Profile()
1191 try:
1191 try:
1192 prof = prof.runctx(arg_str,namespace,namespace)
1192 prof = prof.runctx(arg_str,namespace,namespace)
1193 sys_exit = ''
1193 sys_exit = ''
1194 except SystemExit:
1194 except SystemExit:
1195 sys_exit = """*** SystemExit exception caught in code being profiled."""
1195 sys_exit = """*** SystemExit exception caught in code being profiled."""
1196
1196
1197 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1197 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1198
1198
1199 lims = opts.l
1199 lims = opts.l
1200 if lims:
1200 if lims:
1201 lims = [] # rebuild lims with ints/floats/strings
1201 lims = [] # rebuild lims with ints/floats/strings
1202 for lim in opts.l:
1202 for lim in opts.l:
1203 try:
1203 try:
1204 lims.append(int(lim))
1204 lims.append(int(lim))
1205 except ValueError:
1205 except ValueError:
1206 try:
1206 try:
1207 lims.append(float(lim))
1207 lims.append(float(lim))
1208 except ValueError:
1208 except ValueError:
1209 lims.append(lim)
1209 lims.append(lim)
1210
1210
1211 # Trap output.
1211 # Trap output.
1212 stdout_trap = StringIO()
1212 stdout_trap = StringIO()
1213
1213
1214 if hasattr(stats,'stream'):
1214 if hasattr(stats,'stream'):
1215 # In newer versions of python, the stats object has a 'stream'
1215 # In newer versions of python, the stats object has a 'stream'
1216 # attribute to write into.
1216 # attribute to write into.
1217 stats.stream = stdout_trap
1217 stats.stream = stdout_trap
1218 stats.print_stats(*lims)
1218 stats.print_stats(*lims)
1219 else:
1219 else:
1220 # For older versions, we manually redirect stdout during printing
1220 # For older versions, we manually redirect stdout during printing
1221 sys_stdout = sys.stdout
1221 sys_stdout = sys.stdout
1222 try:
1222 try:
1223 sys.stdout = stdout_trap
1223 sys.stdout = stdout_trap
1224 stats.print_stats(*lims)
1224 stats.print_stats(*lims)
1225 finally:
1225 finally:
1226 sys.stdout = sys_stdout
1226 sys.stdout = sys_stdout
1227
1227
1228 output = stdout_trap.getvalue()
1228 output = stdout_trap.getvalue()
1229 output = output.rstrip()
1229 output = output.rstrip()
1230
1230
1231 page(output,screen_lines=self.shell.rc.screen_length)
1231 page(output,screen_lines=self.shell.rc.screen_length)
1232 print sys_exit,
1232 print sys_exit,
1233
1233
1234 dump_file = opts.D[0]
1234 dump_file = opts.D[0]
1235 text_file = opts.T[0]
1235 text_file = opts.T[0]
1236 if dump_file:
1236 if dump_file:
1237 prof.dump_stats(dump_file)
1237 prof.dump_stats(dump_file)
1238 print '\n*** Profile stats marshalled to file',\
1238 print '\n*** Profile stats marshalled to file',\
1239 `dump_file`+'.',sys_exit
1239 `dump_file`+'.',sys_exit
1240 if text_file:
1240 if text_file:
1241 pfile = file(text_file,'w')
1241 pfile = file(text_file,'w')
1242 pfile.write(output)
1242 pfile.write(output)
1243 pfile.close()
1243 pfile.close()
1244 print '\n*** Profile printout saved to text file',\
1244 print '\n*** Profile printout saved to text file',\
1245 `text_file`+'.',sys_exit
1245 `text_file`+'.',sys_exit
1246
1246
1247 if opts.has_key('r'):
1247 if opts.has_key('r'):
1248 return stats
1248 return stats
1249 else:
1249 else:
1250 return None
1250 return None
1251
1251
1252 def magic_run(self, parameter_s ='',runner=None):
1252 def magic_run(self, parameter_s ='',runner=None):
1253 """Run the named file inside IPython as a program.
1253 """Run the named file inside IPython as a program.
1254
1254
1255 Usage:\\
1255 Usage:\\
1256 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1256 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1257
1257
1258 Parameters after the filename are passed as command-line arguments to
1258 Parameters after the filename are passed as command-line arguments to
1259 the program (put in sys.argv). Then, control returns to IPython's
1259 the program (put in sys.argv). Then, control returns to IPython's
1260 prompt.
1260 prompt.
1261
1261
1262 This is similar to running at a system prompt:\\
1262 This is similar to running at a system prompt:\\
1263 $ python file args\\
1263 $ python file args\\
1264 but with the advantage of giving you IPython's tracebacks, and of
1264 but with the advantage of giving you IPython's tracebacks, and of
1265 loading all variables into your interactive namespace for further use
1265 loading all variables into your interactive namespace for further use
1266 (unless -p is used, see below).
1266 (unless -p is used, see below).
1267
1267
1268 The file is executed in a namespace initially consisting only of
1268 The file is executed in a namespace initially consisting only of
1269 __name__=='__main__' and sys.argv constructed as indicated. It thus
1269 __name__=='__main__' and sys.argv constructed as indicated. It thus
1270 sees its environment as if it were being run as a stand-alone
1270 sees its environment as if it were being run as a stand-alone
1271 program. But after execution, the IPython interactive namespace gets
1271 program. But after execution, the IPython interactive namespace gets
1272 updated with all variables defined in the program (except for __name__
1272 updated with all variables defined in the program (except for __name__
1273 and sys.argv). This allows for very convenient loading of code for
1273 and sys.argv). This allows for very convenient loading of code for
1274 interactive work, while giving each program a 'clean sheet' to run in.
1274 interactive work, while giving each program a 'clean sheet' to run in.
1275
1275
1276 Options:
1276 Options:
1277
1277
1278 -n: __name__ is NOT set to '__main__', but to the running file's name
1278 -n: __name__ is NOT set to '__main__', but to the running file's name
1279 without extension (as python does under import). This allows running
1279 without extension (as python does under import). This allows running
1280 scripts and reloading the definitions in them without calling code
1280 scripts and reloading the definitions in them without calling code
1281 protected by an ' if __name__ == "__main__" ' clause.
1281 protected by an ' if __name__ == "__main__" ' clause.
1282
1282
1283 -i: run the file in IPython's namespace instead of an empty one. This
1283 -i: run the file in IPython's namespace instead of an empty one. This
1284 is useful if you are experimenting with code written in a text editor
1284 is useful if you are experimenting with code written in a text editor
1285 which depends on variables defined interactively.
1285 which depends on variables defined interactively.
1286
1286
1287 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1287 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1288 being run. This is particularly useful if IPython is being used to
1288 being run. This is particularly useful if IPython is being used to
1289 run unittests, which always exit with a sys.exit() call. In such
1289 run unittests, which always exit with a sys.exit() call. In such
1290 cases you are interested in the output of the test results, not in
1290 cases you are interested in the output of the test results, not in
1291 seeing a traceback of the unittest module.
1291 seeing a traceback of the unittest module.
1292
1292
1293 -t: print timing information at the end of the run. IPython will give
1293 -t: print timing information at the end of the run. IPython will give
1294 you an estimated CPU time consumption for your script, which under
1294 you an estimated CPU time consumption for your script, which under
1295 Unix uses the resource module to avoid the wraparound problems of
1295 Unix uses the resource module to avoid the wraparound problems of
1296 time.clock(). Under Unix, an estimate of time spent on system tasks
1296 time.clock(). Under Unix, an estimate of time spent on system tasks
1297 is also given (for Windows platforms this is reported as 0.0).
1297 is also given (for Windows platforms this is reported as 0.0).
1298
1298
1299 If -t is given, an additional -N<N> option can be given, where <N>
1299 If -t is given, an additional -N<N> option can be given, where <N>
1300 must be an integer indicating how many times you want the script to
1300 must be an integer indicating how many times you want the script to
1301 run. The final timing report will include total and per run results.
1301 run. The final timing report will include total and per run results.
1302
1302
1303 For example (testing the script uniq_stable.py):
1303 For example (testing the script uniq_stable.py):
1304
1304
1305 In [1]: run -t uniq_stable
1305 In [1]: run -t uniq_stable
1306
1306
1307 IPython CPU timings (estimated):\\
1307 IPython CPU timings (estimated):\\
1308 User : 0.19597 s.\\
1308 User : 0.19597 s.\\
1309 System: 0.0 s.\\
1309 System: 0.0 s.\\
1310
1310
1311 In [2]: run -t -N5 uniq_stable
1311 In [2]: run -t -N5 uniq_stable
1312
1312
1313 IPython CPU timings (estimated):\\
1313 IPython CPU timings (estimated):\\
1314 Total runs performed: 5\\
1314 Total runs performed: 5\\
1315 Times : Total Per run\\
1315 Times : Total Per run\\
1316 User : 0.910862 s, 0.1821724 s.\\
1316 User : 0.910862 s, 0.1821724 s.\\
1317 System: 0.0 s, 0.0 s.
1317 System: 0.0 s, 0.0 s.
1318
1318
1319 -d: run your program under the control of pdb, the Python debugger.
1319 -d: run your program under the control of pdb, the Python debugger.
1320 This allows you to execute your program step by step, watch variables,
1320 This allows you to execute your program step by step, watch variables,
1321 etc. Internally, what IPython does is similar to calling:
1321 etc. Internally, what IPython does is similar to calling:
1322
1322
1323 pdb.run('execfile("YOURFILENAME")')
1323 pdb.run('execfile("YOURFILENAME")')
1324
1324
1325 with a breakpoint set on line 1 of your file. You can change the line
1325 with a breakpoint set on line 1 of your file. You can change the line
1326 number for this automatic breakpoint to be <N> by using the -bN option
1326 number for this automatic breakpoint to be <N> by using the -bN option
1327 (where N must be an integer). For example:
1327 (where N must be an integer). For example:
1328
1328
1329 %run -d -b40 myscript
1329 %run -d -b40 myscript
1330
1330
1331 will set the first breakpoint at line 40 in myscript.py. Note that
1331 will set the first breakpoint at line 40 in myscript.py. Note that
1332 the first breakpoint must be set on a line which actually does
1332 the first breakpoint must be set on a line which actually does
1333 something (not a comment or docstring) for it to stop execution.
1333 something (not a comment or docstring) for it to stop execution.
1334
1334
1335 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1335 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1336 first enter 'c' (without qoutes) to start execution up to the first
1336 first enter 'c' (without qoutes) to start execution up to the first
1337 breakpoint.
1337 breakpoint.
1338
1338
1339 Entering 'help' gives information about the use of the debugger. You
1339 Entering 'help' gives information about the use of the debugger. You
1340 can easily see pdb's full documentation with "import pdb;pdb.help()"
1340 can easily see pdb's full documentation with "import pdb;pdb.help()"
1341 at a prompt.
1341 at a prompt.
1342
1342
1343 -p: run program under the control of the Python profiler module (which
1343 -p: run program under the control of the Python profiler module (which
1344 prints a detailed report of execution times, function calls, etc).
1344 prints a detailed report of execution times, function calls, etc).
1345
1345
1346 You can pass other options after -p which affect the behavior of the
1346 You can pass other options after -p which affect the behavior of the
1347 profiler itself. See the docs for %prun for details.
1347 profiler itself. See the docs for %prun for details.
1348
1348
1349 In this mode, the program's variables do NOT propagate back to the
1349 In this mode, the program's variables do NOT propagate back to the
1350 IPython interactive namespace (because they remain in the namespace
1350 IPython interactive namespace (because they remain in the namespace
1351 where the profiler executes them).
1351 where the profiler executes them).
1352
1352
1353 Internally this triggers a call to %prun, see its documentation for
1353 Internally this triggers a call to %prun, see its documentation for
1354 details on the options available specifically for profiling.
1354 details on the options available specifically for profiling.
1355
1355
1356 There is one special usage for which the text above doesn't apply:
1356 There is one special usage for which the text above doesn't apply:
1357 if the filename ends with .ipy, the file is run as ipython script,
1357 if the filename ends with .ipy, the file is run as ipython script,
1358 just as if the commands were written on IPython prompt.
1358 just as if the commands were written on IPython prompt.
1359 """
1359 """
1360
1360
1361 # get arguments and set sys.argv for program to be run.
1361 # get arguments and set sys.argv for program to be run.
1362 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1362 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1363 mode='list',list_all=1)
1363 mode='list',list_all=1)
1364
1364
1365 try:
1365 try:
1366 filename = get_py_filename(arg_lst[0])
1366 filename = get_py_filename(arg_lst[0])
1367 except IndexError:
1367 except IndexError:
1368 warn('you must provide at least a filename.')
1368 warn('you must provide at least a filename.')
1369 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1369 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1370 return
1370 return
1371 except IOError,msg:
1371 except IOError,msg:
1372 error(msg)
1372 error(msg)
1373 return
1373 return
1374
1374
1375 if filename.lower().endswith('.ipy'):
1375 if filename.lower().endswith('.ipy'):
1376 self.api.runlines(open(filename).read())
1376 self.api.runlines(open(filename).read())
1377 return
1377 return
1378
1378
1379 # Control the response to exit() calls made by the script being run
1379 # Control the response to exit() calls made by the script being run
1380 exit_ignore = opts.has_key('e')
1380 exit_ignore = opts.has_key('e')
1381
1381
1382 # Make sure that the running script gets a proper sys.argv as if it
1382 # Make sure that the running script gets a proper sys.argv as if it
1383 # were run from a system shell.
1383 # were run from a system shell.
1384 save_argv = sys.argv # save it for later restoring
1384 save_argv = sys.argv # save it for later restoring
1385 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1385 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1386
1386
1387 if opts.has_key('i'):
1387 if opts.has_key('i'):
1388 prog_ns = self.shell.user_ns
1388 prog_ns = self.shell.user_ns
1389 __name__save = self.shell.user_ns['__name__']
1389 __name__save = self.shell.user_ns['__name__']
1390 prog_ns['__name__'] = '__main__'
1390 prog_ns['__name__'] = '__main__'
1391 else:
1391 else:
1392 if opts.has_key('n'):
1392 if opts.has_key('n'):
1393 name = os.path.splitext(os.path.basename(filename))[0]
1393 name = os.path.splitext(os.path.basename(filename))[0]
1394 else:
1394 else:
1395 name = '__main__'
1395 name = '__main__'
1396 prog_ns = {'__name__':name}
1396 prog_ns = {'__name__':name}
1397
1397
1398 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1398 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1399 # set the __file__ global in the script's namespace
1399 # set the __file__ global in the script's namespace
1400 prog_ns['__file__'] = filename
1400 prog_ns['__file__'] = filename
1401
1401
1402 # pickle fix. See iplib for an explanation. But we need to make sure
1402 # pickle fix. See iplib for an explanation. But we need to make sure
1403 # that, if we overwrite __main__, we replace it at the end
1403 # that, if we overwrite __main__, we replace it at the end
1404 if prog_ns['__name__'] == '__main__':
1404 if prog_ns['__name__'] == '__main__':
1405 restore_main = sys.modules['__main__']
1405 restore_main = sys.modules['__main__']
1406 else:
1406 else:
1407 restore_main = False
1407 restore_main = False
1408
1408
1409 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1409 sys.modules[prog_ns['__name__']] = FakeModule(prog_ns)
1410
1410
1411 stats = None
1411 stats = None
1412 try:
1412 try:
1413 if self.shell.has_readline:
1413 if self.shell.has_readline:
1414 self.shell.savehist()
1414 self.shell.savehist()
1415
1415
1416 if opts.has_key('p'):
1416 if opts.has_key('p'):
1417 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1417 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1418 else:
1418 else:
1419 if opts.has_key('d'):
1419 if opts.has_key('d'):
1420 deb = Debugger.Pdb(self.shell.rc.colors)
1420 deb = Debugger.Pdb(self.shell.rc.colors)
1421 # reset Breakpoint state, which is moronically kept
1421 # reset Breakpoint state, which is moronically kept
1422 # in a class
1422 # in a class
1423 bdb.Breakpoint.next = 1
1423 bdb.Breakpoint.next = 1
1424 bdb.Breakpoint.bplist = {}
1424 bdb.Breakpoint.bplist = {}
1425 bdb.Breakpoint.bpbynumber = [None]
1425 bdb.Breakpoint.bpbynumber = [None]
1426 # Set an initial breakpoint to stop execution
1426 # Set an initial breakpoint to stop execution
1427 maxtries = 10
1427 maxtries = 10
1428 bp = int(opts.get('b',[1])[0])
1428 bp = int(opts.get('b',[1])[0])
1429 checkline = deb.checkline(filename,bp)
1429 checkline = deb.checkline(filename,bp)
1430 if not checkline:
1430 if not checkline:
1431 for bp in range(bp+1,bp+maxtries+1):
1431 for bp in range(bp+1,bp+maxtries+1):
1432 if deb.checkline(filename,bp):
1432 if deb.checkline(filename,bp):
1433 break
1433 break
1434 else:
1434 else:
1435 msg = ("\nI failed to find a valid line to set "
1435 msg = ("\nI failed to find a valid line to set "
1436 "a breakpoint\n"
1436 "a breakpoint\n"
1437 "after trying up to line: %s.\n"
1437 "after trying up to line: %s.\n"
1438 "Please set a valid breakpoint manually "
1438 "Please set a valid breakpoint manually "
1439 "with the -b option." % bp)
1439 "with the -b option." % bp)
1440 error(msg)
1440 error(msg)
1441 return
1441 return
1442 # if we find a good linenumber, set the breakpoint
1442 # if we find a good linenumber, set the breakpoint
1443 deb.do_break('%s:%s' % (filename,bp))
1443 deb.do_break('%s:%s' % (filename,bp))
1444 # Start file run
1444 # Start file run
1445 print "NOTE: Enter 'c' at the",
1445 print "NOTE: Enter 'c' at the",
1446 print "%s prompt to start your script." % deb.prompt
1446 print "%s prompt to start your script." % deb.prompt
1447 try:
1447 try:
1448 deb.run('execfile("%s")' % filename,prog_ns)
1448 deb.run('execfile("%s")' % filename,prog_ns)
1449
1449
1450 except:
1450 except:
1451 etype, value, tb = sys.exc_info()
1451 etype, value, tb = sys.exc_info()
1452 # Skip three frames in the traceback: the %run one,
1452 # Skip three frames in the traceback: the %run one,
1453 # one inside bdb.py, and the command-line typed by the
1453 # one inside bdb.py, and the command-line typed by the
1454 # user (run by exec in pdb itself).
1454 # user (run by exec in pdb itself).
1455 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1455 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1456 else:
1456 else:
1457 if runner is None:
1457 if runner is None:
1458 runner = self.shell.safe_execfile
1458 runner = self.shell.safe_execfile
1459 if opts.has_key('t'):
1459 if opts.has_key('t'):
1460 try:
1460 try:
1461 nruns = int(opts['N'][0])
1461 nruns = int(opts['N'][0])
1462 if nruns < 1:
1462 if nruns < 1:
1463 error('Number of runs must be >=1')
1463 error('Number of runs must be >=1')
1464 return
1464 return
1465 except (KeyError):
1465 except (KeyError):
1466 nruns = 1
1466 nruns = 1
1467 if nruns == 1:
1467 if nruns == 1:
1468 t0 = clock2()
1468 t0 = clock2()
1469 runner(filename,prog_ns,prog_ns,
1469 runner(filename,prog_ns,prog_ns,
1470 exit_ignore=exit_ignore)
1470 exit_ignore=exit_ignore)
1471 t1 = clock2()
1471 t1 = clock2()
1472 t_usr = t1[0]-t0[0]
1472 t_usr = t1[0]-t0[0]
1473 t_sys = t1[1]-t1[1]
1473 t_sys = t1[1]-t1[1]
1474 print "\nIPython CPU timings (estimated):"
1474 print "\nIPython CPU timings (estimated):"
1475 print " User : %10s s." % t_usr
1475 print " User : %10s s." % t_usr
1476 print " System: %10s s." % t_sys
1476 print " System: %10s s." % t_sys
1477 else:
1477 else:
1478 runs = range(nruns)
1478 runs = range(nruns)
1479 t0 = clock2()
1479 t0 = clock2()
1480 for nr in runs:
1480 for nr in runs:
1481 runner(filename,prog_ns,prog_ns,
1481 runner(filename,prog_ns,prog_ns,
1482 exit_ignore=exit_ignore)
1482 exit_ignore=exit_ignore)
1483 t1 = clock2()
1483 t1 = clock2()
1484 t_usr = t1[0]-t0[0]
1484 t_usr = t1[0]-t0[0]
1485 t_sys = t1[1]-t1[1]
1485 t_sys = t1[1]-t1[1]
1486 print "\nIPython CPU timings (estimated):"
1486 print "\nIPython CPU timings (estimated):"
1487 print "Total runs performed:",nruns
1487 print "Total runs performed:",nruns
1488 print " Times : %10s %10s" % ('Total','Per run')
1488 print " Times : %10s %10s" % ('Total','Per run')
1489 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1489 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1490 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1490 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1491
1491
1492 else:
1492 else:
1493 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1493 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1494 if opts.has_key('i'):
1494 if opts.has_key('i'):
1495 self.shell.user_ns['__name__'] = __name__save
1495 self.shell.user_ns['__name__'] = __name__save
1496 else:
1496 else:
1497 # update IPython interactive namespace
1497 # update IPython interactive namespace
1498 del prog_ns['__name__']
1498 del prog_ns['__name__']
1499 self.shell.user_ns.update(prog_ns)
1499 self.shell.user_ns.update(prog_ns)
1500 finally:
1500 finally:
1501 sys.argv = save_argv
1501 sys.argv = save_argv
1502 if restore_main:
1502 if restore_main:
1503 sys.modules['__main__'] = restore_main
1503 sys.modules['__main__'] = restore_main
1504 self.shell.reloadhist()
1504 self.shell.reloadhist()
1505
1505
1506 return stats
1506 return stats
1507
1507
1508 def magic_runlog(self, parameter_s =''):
1508 def magic_runlog(self, parameter_s =''):
1509 """Run files as logs.
1509 """Run files as logs.
1510
1510
1511 Usage:\\
1511 Usage:\\
1512 %runlog file1 file2 ...
1512 %runlog file1 file2 ...
1513
1513
1514 Run the named files (treating them as log files) in sequence inside
1514 Run the named files (treating them as log files) in sequence inside
1515 the interpreter, and return to the prompt. This is much slower than
1515 the interpreter, and return to the prompt. This is much slower than
1516 %run because each line is executed in a try/except block, but it
1516 %run because each line is executed in a try/except block, but it
1517 allows running files with syntax errors in them.
1517 allows running files with syntax errors in them.
1518
1518
1519 Normally IPython will guess when a file is one of its own logfiles, so
1519 Normally IPython will guess when a file is one of its own logfiles, so
1520 you can typically use %run even for logs. This shorthand allows you to
1520 you can typically use %run even for logs. This shorthand allows you to
1521 force any file to be treated as a log file."""
1521 force any file to be treated as a log file."""
1522
1522
1523 for f in parameter_s.split():
1523 for f in parameter_s.split():
1524 self.shell.safe_execfile(f,self.shell.user_ns,
1524 self.shell.safe_execfile(f,self.shell.user_ns,
1525 self.shell.user_ns,islog=1)
1525 self.shell.user_ns,islog=1)
1526
1526
1527 def magic_timeit(self, parameter_s =''):
1527 def magic_timeit(self, parameter_s =''):
1528 """Time execution of a Python statement or expression
1528 """Time execution of a Python statement or expression
1529
1529
1530 Usage:\\
1530 Usage:\\
1531 %timeit [-n<N> -r<R> [-t|-c]] statement
1531 %timeit [-n<N> -r<R> [-t|-c]] statement
1532
1532
1533 Time execution of a Python statement or expression using the timeit
1533 Time execution of a Python statement or expression using the timeit
1534 module.
1534 module.
1535
1535
1536 Options:
1536 Options:
1537 -n<N>: execute the given statement <N> times in a loop. If this value
1537 -n<N>: execute the given statement <N> times in a loop. If this value
1538 is not given, a fitting value is chosen.
1538 is not given, a fitting value is chosen.
1539
1539
1540 -r<R>: repeat the loop iteration <R> times and take the best result.
1540 -r<R>: repeat the loop iteration <R> times and take the best result.
1541 Default: 3
1541 Default: 3
1542
1542
1543 -t: use time.time to measure the time, which is the default on Unix.
1543 -t: use time.time to measure the time, which is the default on Unix.
1544 This function measures wall time.
1544 This function measures wall time.
1545
1545
1546 -c: use time.clock to measure the time, which is the default on
1546 -c: use time.clock to measure the time, which is the default on
1547 Windows and measures wall time. On Unix, resource.getrusage is used
1547 Windows and measures wall time. On Unix, resource.getrusage is used
1548 instead and returns the CPU user time.
1548 instead and returns the CPU user time.
1549
1549
1550 -p<P>: use a precision of <P> digits to display the timing result.
1550 -p<P>: use a precision of <P> digits to display the timing result.
1551 Default: 3
1551 Default: 3
1552
1552
1553
1553
1554 Examples:\\
1554 Examples:\\
1555 In [1]: %timeit pass
1555 In [1]: %timeit pass
1556 10000000 loops, best of 3: 53.3 ns per loop
1556 10000000 loops, best of 3: 53.3 ns per loop
1557
1557
1558 In [2]: u = None
1558 In [2]: u = None
1559
1559
1560 In [3]: %timeit u is None
1560 In [3]: %timeit u is None
1561 10000000 loops, best of 3: 184 ns per loop
1561 10000000 loops, best of 3: 184 ns per loop
1562
1562
1563 In [4]: %timeit -r 4 u == None
1563 In [4]: %timeit -r 4 u == None
1564 1000000 loops, best of 4: 242 ns per loop
1564 1000000 loops, best of 4: 242 ns per loop
1565
1565
1566 In [5]: import time
1566 In [5]: import time
1567
1567
1568 In [6]: %timeit -n1 time.sleep(2)
1568 In [6]: %timeit -n1 time.sleep(2)
1569 1 loops, best of 3: 2 s per loop
1569 1 loops, best of 3: 2 s per loop
1570
1570
1571
1571
1572 The times reported by %timeit will be slightly higher than those
1572 The times reported by %timeit will be slightly higher than those
1573 reported by the timeit.py script when variables are accessed. This is
1573 reported by the timeit.py script when variables are accessed. This is
1574 due to the fact that %timeit executes the statement in the namespace
1574 due to the fact that %timeit executes the statement in the namespace
1575 of the shell, compared with timeit.py, which uses a single setup
1575 of the shell, compared with timeit.py, which uses a single setup
1576 statement to import function or create variables. Generally, the bias
1576 statement to import function or create variables. Generally, the bias
1577 does not matter as long as results from timeit.py are not mixed with
1577 does not matter as long as results from timeit.py are not mixed with
1578 those from %timeit."""
1578 those from %timeit."""
1579
1579
1580 import timeit
1580 import timeit
1581 import math
1581 import math
1582
1582
1583 units = ["s", "ms", "\xc2\xb5s", "ns"]
1583 units = ["s", "ms", "\xc2\xb5s", "ns"]
1584 scaling = [1, 1e3, 1e6, 1e9]
1584 scaling = [1, 1e3, 1e6, 1e9]
1585
1585
1586 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1586 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1587 posix=False)
1587 posix=False)
1588 if stmt == "":
1588 if stmt == "":
1589 return
1589 return
1590 timefunc = timeit.default_timer
1590 timefunc = timeit.default_timer
1591 number = int(getattr(opts, "n", 0))
1591 number = int(getattr(opts, "n", 0))
1592 repeat = int(getattr(opts, "r", timeit.default_repeat))
1592 repeat = int(getattr(opts, "r", timeit.default_repeat))
1593 precision = int(getattr(opts, "p", 3))
1593 precision = int(getattr(opts, "p", 3))
1594 if hasattr(opts, "t"):
1594 if hasattr(opts, "t"):
1595 timefunc = time.time
1595 timefunc = time.time
1596 if hasattr(opts, "c"):
1596 if hasattr(opts, "c"):
1597 timefunc = clock
1597 timefunc = clock
1598
1598
1599 timer = timeit.Timer(timer=timefunc)
1599 timer = timeit.Timer(timer=timefunc)
1600 # this code has tight coupling to the inner workings of timeit.Timer,
1600 # this code has tight coupling to the inner workings of timeit.Timer,
1601 # but is there a better way to achieve that the code stmt has access
1601 # but is there a better way to achieve that the code stmt has access
1602 # to the shell namespace?
1602 # to the shell namespace?
1603
1603
1604 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1604 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1605 'setup': "pass"}
1605 'setup': "pass"}
1606 code = compile(src, "<magic-timeit>", "exec")
1606 code = compile(src, "<magic-timeit>", "exec")
1607 ns = {}
1607 ns = {}
1608 exec code in self.shell.user_ns, ns
1608 exec code in self.shell.user_ns, ns
1609 timer.inner = ns["inner"]
1609 timer.inner = ns["inner"]
1610
1610
1611 if number == 0:
1611 if number == 0:
1612 # determine number so that 0.2 <= total time < 2.0
1612 # determine number so that 0.2 <= total time < 2.0
1613 number = 1
1613 number = 1
1614 for i in range(1, 10):
1614 for i in range(1, 10):
1615 number *= 10
1615 number *= 10
1616 if timer.timeit(number) >= 0.2:
1616 if timer.timeit(number) >= 0.2:
1617 break
1617 break
1618
1618
1619 best = min(timer.repeat(repeat, number)) / number
1619 best = min(timer.repeat(repeat, number)) / number
1620
1620
1621 if best > 0.0:
1621 if best > 0.0:
1622 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1622 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1623 else:
1623 else:
1624 order = 3
1624 order = 3
1625 print "%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1625 print "%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1626 precision,
1626 precision,
1627 best * scaling[order],
1627 best * scaling[order],
1628 units[order])
1628 units[order])
1629
1629
1630 def magic_time(self,parameter_s = ''):
1630 def magic_time(self,parameter_s = ''):
1631 """Time execution of a Python statement or expression.
1631 """Time execution of a Python statement or expression.
1632
1632
1633 The CPU and wall clock times are printed, and the value of the
1633 The CPU and wall clock times are printed, and the value of the
1634 expression (if any) is returned. Note that under Win32, system time
1634 expression (if any) is returned. Note that under Win32, system time
1635 is always reported as 0, since it can not be measured.
1635 is always reported as 0, since it can not be measured.
1636
1636
1637 This function provides very basic timing functionality. In Python
1637 This function provides very basic timing functionality. In Python
1638 2.3, the timeit module offers more control and sophistication, so this
1638 2.3, the timeit module offers more control and sophistication, so this
1639 could be rewritten to use it (patches welcome).
1639 could be rewritten to use it (patches welcome).
1640
1640
1641 Some examples:
1641 Some examples:
1642
1642
1643 In [1]: time 2**128
1643 In [1]: time 2**128
1644 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1644 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1645 Wall time: 0.00
1645 Wall time: 0.00
1646 Out[1]: 340282366920938463463374607431768211456L
1646 Out[1]: 340282366920938463463374607431768211456L
1647
1647
1648 In [2]: n = 1000000
1648 In [2]: n = 1000000
1649
1649
1650 In [3]: time sum(range(n))
1650 In [3]: time sum(range(n))
1651 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1651 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1652 Wall time: 1.37
1652 Wall time: 1.37
1653 Out[3]: 499999500000L
1653 Out[3]: 499999500000L
1654
1654
1655 In [4]: time print 'hello world'
1655 In [4]: time print 'hello world'
1656 hello world
1656 hello world
1657 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1657 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1658 Wall time: 0.00
1658 Wall time: 0.00
1659 """
1659 """
1660
1660
1661 # fail immediately if the given expression can't be compiled
1661 # fail immediately if the given expression can't be compiled
1662 try:
1662 try:
1663 mode = 'eval'
1663 mode = 'eval'
1664 code = compile(parameter_s,'<timed eval>',mode)
1664 code = compile(parameter_s,'<timed eval>',mode)
1665 except SyntaxError:
1665 except SyntaxError:
1666 mode = 'exec'
1666 mode = 'exec'
1667 code = compile(parameter_s,'<timed exec>',mode)
1667 code = compile(parameter_s,'<timed exec>',mode)
1668 # skew measurement as little as possible
1668 # skew measurement as little as possible
1669 glob = self.shell.user_ns
1669 glob = self.shell.user_ns
1670 clk = clock2
1670 clk = clock2
1671 wtime = time.time
1671 wtime = time.time
1672 # time execution
1672 # time execution
1673 wall_st = wtime()
1673 wall_st = wtime()
1674 if mode=='eval':
1674 if mode=='eval':
1675 st = clk()
1675 st = clk()
1676 out = eval(code,glob)
1676 out = eval(code,glob)
1677 end = clk()
1677 end = clk()
1678 else:
1678 else:
1679 st = clk()
1679 st = clk()
1680 exec code in glob
1680 exec code in glob
1681 end = clk()
1681 end = clk()
1682 out = None
1682 out = None
1683 wall_end = wtime()
1683 wall_end = wtime()
1684 # Compute actual times and report
1684 # Compute actual times and report
1685 wall_time = wall_end-wall_st
1685 wall_time = wall_end-wall_st
1686 cpu_user = end[0]-st[0]
1686 cpu_user = end[0]-st[0]
1687 cpu_sys = end[1]-st[1]
1687 cpu_sys = end[1]-st[1]
1688 cpu_tot = cpu_user+cpu_sys
1688 cpu_tot = cpu_user+cpu_sys
1689 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1689 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1690 (cpu_user,cpu_sys,cpu_tot)
1690 (cpu_user,cpu_sys,cpu_tot)
1691 print "Wall time: %.2f" % wall_time
1691 print "Wall time: %.2f" % wall_time
1692 return out
1692 return out
1693
1693
1694 def magic_macro(self,parameter_s = ''):
1694 def magic_macro(self,parameter_s = ''):
1695 """Define a set of input lines as a macro for future re-execution.
1695 """Define a set of input lines as a macro for future re-execution.
1696
1696
1697 Usage:\\
1697 Usage:\\
1698 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1698 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1699
1699
1700 Options:
1700 Options:
1701
1701
1702 -r: use 'raw' input. By default, the 'processed' history is used,
1702 -r: use 'raw' input. By default, the 'processed' history is used,
1703 so that magics are loaded in their transformed version to valid
1703 so that magics are loaded in their transformed version to valid
1704 Python. If this option is given, the raw input as typed as the
1704 Python. If this option is given, the raw input as typed as the
1705 command line is used instead.
1705 command line is used instead.
1706
1706
1707 This will define a global variable called `name` which is a string
1707 This will define a global variable called `name` which is a string
1708 made of joining the slices and lines you specify (n1,n2,... numbers
1708 made of joining the slices and lines you specify (n1,n2,... numbers
1709 above) from your input history into a single string. This variable
1709 above) from your input history into a single string. This variable
1710 acts like an automatic function which re-executes those lines as if
1710 acts like an automatic function which re-executes those lines as if
1711 you had typed them. You just type 'name' at the prompt and the code
1711 you had typed them. You just type 'name' at the prompt and the code
1712 executes.
1712 executes.
1713
1713
1714 The notation for indicating number ranges is: n1-n2 means 'use line
1714 The notation for indicating number ranges is: n1-n2 means 'use line
1715 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1715 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1716 using the lines numbered 5,6 and 7.
1716 using the lines numbered 5,6 and 7.
1717
1717
1718 Note: as a 'hidden' feature, you can also use traditional python slice
1718 Note: as a 'hidden' feature, you can also use traditional python slice
1719 notation, where N:M means numbers N through M-1.
1719 notation, where N:M means numbers N through M-1.
1720
1720
1721 For example, if your history contains (%hist prints it):
1721 For example, if your history contains (%hist prints it):
1722
1722
1723 44: x=1\\
1723 44: x=1\\
1724 45: y=3\\
1724 45: y=3\\
1725 46: z=x+y\\
1725 46: z=x+y\\
1726 47: print x\\
1726 47: print x\\
1727 48: a=5\\
1727 48: a=5\\
1728 49: print 'x',x,'y',y\\
1728 49: print 'x',x,'y',y\\
1729
1729
1730 you can create a macro with lines 44 through 47 (included) and line 49
1730 you can create a macro with lines 44 through 47 (included) and line 49
1731 called my_macro with:
1731 called my_macro with:
1732
1732
1733 In [51]: %macro my_macro 44-47 49
1733 In [51]: %macro my_macro 44-47 49
1734
1734
1735 Now, typing `my_macro` (without quotes) will re-execute all this code
1735 Now, typing `my_macro` (without quotes) will re-execute all this code
1736 in one pass.
1736 in one pass.
1737
1737
1738 You don't need to give the line-numbers in order, and any given line
1738 You don't need to give the line-numbers in order, and any given line
1739 number can appear multiple times. You can assemble macros with any
1739 number can appear multiple times. You can assemble macros with any
1740 lines from your input history in any order.
1740 lines from your input history in any order.
1741
1741
1742 The macro is a simple object which holds its value in an attribute,
1742 The macro is a simple object which holds its value in an attribute,
1743 but IPython's display system checks for macros and executes them as
1743 but IPython's display system checks for macros and executes them as
1744 code instead of printing them when you type their name.
1744 code instead of printing them when you type their name.
1745
1745
1746 You can view a macro's contents by explicitly printing it with:
1746 You can view a macro's contents by explicitly printing it with:
1747
1747
1748 'print macro_name'.
1748 'print macro_name'.
1749
1749
1750 For one-off cases which DON'T contain magic function calls in them you
1750 For one-off cases which DON'T contain magic function calls in them you
1751 can obtain similar results by explicitly executing slices from your
1751 can obtain similar results by explicitly executing slices from your
1752 input history with:
1752 input history with:
1753
1753
1754 In [60]: exec In[44:48]+In[49]"""
1754 In [60]: exec In[44:48]+In[49]"""
1755
1755
1756 opts,args = self.parse_options(parameter_s,'r',mode='list')
1756 opts,args = self.parse_options(parameter_s,'r',mode='list')
1757 if not args:
1757 if not args:
1758 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
1758 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
1759 macs.sort()
1759 macs.sort()
1760 return macs
1760 return macs
1761 name,ranges = args[0], args[1:]
1761 name,ranges = args[0], args[1:]
1762 #print 'rng',ranges # dbg
1762 #print 'rng',ranges # dbg
1763 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1763 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1764 macro = Macro(lines)
1764 macro = Macro(lines)
1765 self.shell.user_ns.update({name:macro})
1765 self.shell.user_ns.update({name:macro})
1766 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1766 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1767 print 'Macro contents:'
1767 print 'Macro contents:'
1768 print macro,
1768 print macro,
1769
1769
1770 def magic_save(self,parameter_s = ''):
1770 def magic_save(self,parameter_s = ''):
1771 """Save a set of lines to a given filename.
1771 """Save a set of lines to a given filename.
1772
1772
1773 Usage:\\
1773 Usage:\\
1774 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1774 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1775
1775
1776 Options:
1776 Options:
1777
1777
1778 -r: use 'raw' input. By default, the 'processed' history is used,
1778 -r: use 'raw' input. By default, the 'processed' history is used,
1779 so that magics are loaded in their transformed version to valid
1779 so that magics are loaded in their transformed version to valid
1780 Python. If this option is given, the raw input as typed as the
1780 Python. If this option is given, the raw input as typed as the
1781 command line is used instead.
1781 command line is used instead.
1782
1782
1783 This function uses the same syntax as %macro for line extraction, but
1783 This function uses the same syntax as %macro for line extraction, but
1784 instead of creating a macro it saves the resulting string to the
1784 instead of creating a macro it saves the resulting string to the
1785 filename you specify.
1785 filename you specify.
1786
1786
1787 It adds a '.py' extension to the file if you don't do so yourself, and
1787 It adds a '.py' extension to the file if you don't do so yourself, and
1788 it asks for confirmation before overwriting existing files."""
1788 it asks for confirmation before overwriting existing files."""
1789
1789
1790 opts,args = self.parse_options(parameter_s,'r',mode='list')
1790 opts,args = self.parse_options(parameter_s,'r',mode='list')
1791 fname,ranges = args[0], args[1:]
1791 fname,ranges = args[0], args[1:]
1792 if not fname.endswith('.py'):
1792 if not fname.endswith('.py'):
1793 fname += '.py'
1793 fname += '.py'
1794 if os.path.isfile(fname):
1794 if os.path.isfile(fname):
1795 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1795 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
1796 if ans.lower() not in ['y','yes']:
1796 if ans.lower() not in ['y','yes']:
1797 print 'Operation cancelled.'
1797 print 'Operation cancelled.'
1798 return
1798 return
1799 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
1799 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
1800 f = file(fname,'w')
1800 f = file(fname,'w')
1801 f.write(cmds)
1801 f.write(cmds)
1802 f.close()
1802 f.close()
1803 print 'The following commands were written to file `%s`:' % fname
1803 print 'The following commands were written to file `%s`:' % fname
1804 print cmds
1804 print cmds
1805
1805
1806 def _edit_macro(self,mname,macro):
1806 def _edit_macro(self,mname,macro):
1807 """open an editor with the macro data in a file"""
1807 """open an editor with the macro data in a file"""
1808 filename = self.shell.mktempfile(macro.value)
1808 filename = self.shell.mktempfile(macro.value)
1809 self.shell.hooks.editor(filename)
1809 self.shell.hooks.editor(filename)
1810
1810
1811 # and make a new macro object, to replace the old one
1811 # and make a new macro object, to replace the old one
1812 mfile = open(filename)
1812 mfile = open(filename)
1813 mvalue = mfile.read()
1813 mvalue = mfile.read()
1814 mfile.close()
1814 mfile.close()
1815 self.shell.user_ns[mname] = Macro(mvalue)
1815 self.shell.user_ns[mname] = Macro(mvalue)
1816
1816
1817 def magic_ed(self,parameter_s=''):
1817 def magic_ed(self,parameter_s=''):
1818 """Alias to %edit."""
1818 """Alias to %edit."""
1819 return self.magic_edit(parameter_s)
1819 return self.magic_edit(parameter_s)
1820
1820
1821 def magic_edit(self,parameter_s='',last_call=['','']):
1821 def magic_edit(self,parameter_s='',last_call=['','']):
1822 """Bring up an editor and execute the resulting code.
1822 """Bring up an editor and execute the resulting code.
1823
1823
1824 Usage:
1824 Usage:
1825 %edit [options] [args]
1825 %edit [options] [args]
1826
1826
1827 %edit runs IPython's editor hook. The default version of this hook is
1827 %edit runs IPython's editor hook. The default version of this hook is
1828 set to call the __IPYTHON__.rc.editor command. This is read from your
1828 set to call the __IPYTHON__.rc.editor command. This is read from your
1829 environment variable $EDITOR. If this isn't found, it will default to
1829 environment variable $EDITOR. If this isn't found, it will default to
1830 vi under Linux/Unix and to notepad under Windows. See the end of this
1830 vi under Linux/Unix and to notepad under Windows. See the end of this
1831 docstring for how to change the editor hook.
1831 docstring for how to change the editor hook.
1832
1832
1833 You can also set the value of this editor via the command line option
1833 You can also set the value of this editor via the command line option
1834 '-editor' or in your ipythonrc file. This is useful if you wish to use
1834 '-editor' or in your ipythonrc file. This is useful if you wish to use
1835 specifically for IPython an editor different from your typical default
1835 specifically for IPython an editor different from your typical default
1836 (and for Windows users who typically don't set environment variables).
1836 (and for Windows users who typically don't set environment variables).
1837
1837
1838 This command allows you to conveniently edit multi-line code right in
1838 This command allows you to conveniently edit multi-line code right in
1839 your IPython session.
1839 your IPython session.
1840
1840
1841 If called without arguments, %edit opens up an empty editor with a
1841 If called without arguments, %edit opens up an empty editor with a
1842 temporary file and will execute the contents of this file when you
1842 temporary file and will execute the contents of this file when you
1843 close it (don't forget to save it!).
1843 close it (don't forget to save it!).
1844
1844
1845
1845
1846 Options:
1846 Options:
1847
1847
1848 -n <number>: open the editor at a specified line number. By default,
1848 -n <number>: open the editor at a specified line number. By default,
1849 the IPython editor hook uses the unix syntax 'editor +N filename', but
1849 the IPython editor hook uses the unix syntax 'editor +N filename', but
1850 you can configure this by providing your own modified hook if your
1850 you can configure this by providing your own modified hook if your
1851 favorite editor supports line-number specifications with a different
1851 favorite editor supports line-number specifications with a different
1852 syntax.
1852 syntax.
1853
1853
1854 -p: this will call the editor with the same data as the previous time
1854 -p: this will call the editor with the same data as the previous time
1855 it was used, regardless of how long ago (in your current session) it
1855 it was used, regardless of how long ago (in your current session) it
1856 was.
1856 was.
1857
1857
1858 -r: use 'raw' input. This option only applies to input taken from the
1858 -r: use 'raw' input. This option only applies to input taken from the
1859 user's history. By default, the 'processed' history is used, so that
1859 user's history. By default, the 'processed' history is used, so that
1860 magics are loaded in their transformed version to valid Python. If
1860 magics are loaded in their transformed version to valid Python. If
1861 this option is given, the raw input as typed as the command line is
1861 this option is given, the raw input as typed as the command line is
1862 used instead. When you exit the editor, it will be executed by
1862 used instead. When you exit the editor, it will be executed by
1863 IPython's own processor.
1863 IPython's own processor.
1864
1864
1865 -x: do not execute the edited code immediately upon exit. This is
1865 -x: do not execute the edited code immediately upon exit. This is
1866 mainly useful if you are editing programs which need to be called with
1866 mainly useful if you are editing programs which need to be called with
1867 command line arguments, which you can then do using %run.
1867 command line arguments, which you can then do using %run.
1868
1868
1869
1869
1870 Arguments:
1870 Arguments:
1871
1871
1872 If arguments are given, the following possibilites exist:
1872 If arguments are given, the following possibilites exist:
1873
1873
1874 - The arguments are numbers or pairs of colon-separated numbers (like
1874 - The arguments are numbers or pairs of colon-separated numbers (like
1875 1 4:8 9). These are interpreted as lines of previous input to be
1875 1 4:8 9). These are interpreted as lines of previous input to be
1876 loaded into the editor. The syntax is the same of the %macro command.
1876 loaded into the editor. The syntax is the same of the %macro command.
1877
1877
1878 - If the argument doesn't start with a number, it is evaluated as a
1878 - If the argument doesn't start with a number, it is evaluated as a
1879 variable and its contents loaded into the editor. You can thus edit
1879 variable and its contents loaded into the editor. You can thus edit
1880 any string which contains python code (including the result of
1880 any string which contains python code (including the result of
1881 previous edits).
1881 previous edits).
1882
1882
1883 - If the argument is the name of an object (other than a string),
1883 - If the argument is the name of an object (other than a string),
1884 IPython will try to locate the file where it was defined and open the
1884 IPython will try to locate the file where it was defined and open the
1885 editor at the point where it is defined. You can use `%edit function`
1885 editor at the point where it is defined. You can use `%edit function`
1886 to load an editor exactly at the point where 'function' is defined,
1886 to load an editor exactly at the point where 'function' is defined,
1887 edit it and have the file be executed automatically.
1887 edit it and have the file be executed automatically.
1888
1888
1889 If the object is a macro (see %macro for details), this opens up your
1889 If the object is a macro (see %macro for details), this opens up your
1890 specified editor with a temporary file containing the macro's data.
1890 specified editor with a temporary file containing the macro's data.
1891 Upon exit, the macro is reloaded with the contents of the file.
1891 Upon exit, the macro is reloaded with the contents of the file.
1892
1892
1893 Note: opening at an exact line is only supported under Unix, and some
1893 Note: opening at an exact line is only supported under Unix, and some
1894 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1894 editors (like kedit and gedit up to Gnome 2.8) do not understand the
1895 '+NUMBER' parameter necessary for this feature. Good editors like
1895 '+NUMBER' parameter necessary for this feature. Good editors like
1896 (X)Emacs, vi, jed, pico and joe all do.
1896 (X)Emacs, vi, jed, pico and joe all do.
1897
1897
1898 - If the argument is not found as a variable, IPython will look for a
1898 - If the argument is not found as a variable, IPython will look for a
1899 file with that name (adding .py if necessary) and load it into the
1899 file with that name (adding .py if necessary) and load it into the
1900 editor. It will execute its contents with execfile() when you exit,
1900 editor. It will execute its contents with execfile() when you exit,
1901 loading any code in the file into your interactive namespace.
1901 loading any code in the file into your interactive namespace.
1902
1902
1903 After executing your code, %edit will return as output the code you
1903 After executing your code, %edit will return as output the code you
1904 typed in the editor (except when it was an existing file). This way
1904 typed in the editor (except when it was an existing file). This way
1905 you can reload the code in further invocations of %edit as a variable,
1905 you can reload the code in further invocations of %edit as a variable,
1906 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1906 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
1907 the output.
1907 the output.
1908
1908
1909 Note that %edit is also available through the alias %ed.
1909 Note that %edit is also available through the alias %ed.
1910
1910
1911 This is an example of creating a simple function inside the editor and
1911 This is an example of creating a simple function inside the editor and
1912 then modifying it. First, start up the editor:
1912 then modifying it. First, start up the editor:
1913
1913
1914 In [1]: ed\\
1914 In [1]: ed\\
1915 Editing... done. Executing edited code...\\
1915 Editing... done. Executing edited code...\\
1916 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1916 Out[1]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
1917
1917
1918 We can then call the function foo():
1918 We can then call the function foo():
1919
1919
1920 In [2]: foo()\\
1920 In [2]: foo()\\
1921 foo() was defined in an editing session
1921 foo() was defined in an editing session
1922
1922
1923 Now we edit foo. IPython automatically loads the editor with the
1923 Now we edit foo. IPython automatically loads the editor with the
1924 (temporary) file where foo() was previously defined:
1924 (temporary) file where foo() was previously defined:
1925
1925
1926 In [3]: ed foo\\
1926 In [3]: ed foo\\
1927 Editing... done. Executing edited code...
1927 Editing... done. Executing edited code...
1928
1928
1929 And if we call foo() again we get the modified version:
1929 And if we call foo() again we get the modified version:
1930
1930
1931 In [4]: foo()\\
1931 In [4]: foo()\\
1932 foo() has now been changed!
1932 foo() has now been changed!
1933
1933
1934 Here is an example of how to edit a code snippet successive
1934 Here is an example of how to edit a code snippet successive
1935 times. First we call the editor:
1935 times. First we call the editor:
1936
1936
1937 In [8]: ed\\
1937 In [8]: ed\\
1938 Editing... done. Executing edited code...\\
1938 Editing... done. Executing edited code...\\
1939 hello\\
1939 hello\\
1940 Out[8]: "print 'hello'\\n"
1940 Out[8]: "print 'hello'\\n"
1941
1941
1942 Now we call it again with the previous output (stored in _):
1942 Now we call it again with the previous output (stored in _):
1943
1943
1944 In [9]: ed _\\
1944 In [9]: ed _\\
1945 Editing... done. Executing edited code...\\
1945 Editing... done. Executing edited code...\\
1946 hello world\\
1946 hello world\\
1947 Out[9]: "print 'hello world'\\n"
1947 Out[9]: "print 'hello world'\\n"
1948
1948
1949 Now we call it with the output #8 (stored in _8, also as Out[8]):
1949 Now we call it with the output #8 (stored in _8, also as Out[8]):
1950
1950
1951 In [10]: ed _8\\
1951 In [10]: ed _8\\
1952 Editing... done. Executing edited code...\\
1952 Editing... done. Executing edited code...\\
1953 hello again\\
1953 hello again\\
1954 Out[10]: "print 'hello again'\\n"
1954 Out[10]: "print 'hello again'\\n"
1955
1955
1956
1956
1957 Changing the default editor hook:
1957 Changing the default editor hook:
1958
1958
1959 If you wish to write your own editor hook, you can put it in a
1959 If you wish to write your own editor hook, you can put it in a
1960 configuration file which you load at startup time. The default hook
1960 configuration file which you load at startup time. The default hook
1961 is defined in the IPython.hooks module, and you can use that as a
1961 is defined in the IPython.hooks module, and you can use that as a
1962 starting example for further modifications. That file also has
1962 starting example for further modifications. That file also has
1963 general instructions on how to set a new hook for use once you've
1963 general instructions on how to set a new hook for use once you've
1964 defined it."""
1964 defined it."""
1965
1965
1966 # FIXME: This function has become a convoluted mess. It needs a
1966 # FIXME: This function has become a convoluted mess. It needs a
1967 # ground-up rewrite with clean, simple logic.
1967 # ground-up rewrite with clean, simple logic.
1968
1968
1969 def make_filename(arg):
1969 def make_filename(arg):
1970 "Make a filename from the given args"
1970 "Make a filename from the given args"
1971 try:
1971 try:
1972 filename = get_py_filename(arg)
1972 filename = get_py_filename(arg)
1973 except IOError:
1973 except IOError:
1974 if args.endswith('.py'):
1974 if args.endswith('.py'):
1975 filename = arg
1975 filename = arg
1976 else:
1976 else:
1977 filename = None
1977 filename = None
1978 return filename
1978 return filename
1979
1979
1980 # custom exceptions
1980 # custom exceptions
1981 class DataIsObject(Exception): pass
1981 class DataIsObject(Exception): pass
1982
1982
1983 opts,args = self.parse_options(parameter_s,'prxn:')
1983 opts,args = self.parse_options(parameter_s,'prxn:')
1984 # Set a few locals from the options for convenience:
1984 # Set a few locals from the options for convenience:
1985 opts_p = opts.has_key('p')
1985 opts_p = opts.has_key('p')
1986 opts_r = opts.has_key('r')
1986 opts_r = opts.has_key('r')
1987
1987
1988 # Default line number value
1988 # Default line number value
1989 lineno = opts.get('n',None)
1989 lineno = opts.get('n',None)
1990
1990
1991 if opts_p:
1991 if opts_p:
1992 args = '_%s' % last_call[0]
1992 args = '_%s' % last_call[0]
1993 if not self.shell.user_ns.has_key(args):
1993 if not self.shell.user_ns.has_key(args):
1994 args = last_call[1]
1994 args = last_call[1]
1995
1995
1996 # use last_call to remember the state of the previous call, but don't
1996 # use last_call to remember the state of the previous call, but don't
1997 # let it be clobbered by successive '-p' calls.
1997 # let it be clobbered by successive '-p' calls.
1998 try:
1998 try:
1999 last_call[0] = self.shell.outputcache.prompt_count
1999 last_call[0] = self.shell.outputcache.prompt_count
2000 if not opts_p:
2000 if not opts_p:
2001 last_call[1] = parameter_s
2001 last_call[1] = parameter_s
2002 except:
2002 except:
2003 pass
2003 pass
2004
2004
2005 # by default this is done with temp files, except when the given
2005 # by default this is done with temp files, except when the given
2006 # arg is a filename
2006 # arg is a filename
2007 use_temp = 1
2007 use_temp = 1
2008
2008
2009 if re.match(r'\d',args):
2009 if re.match(r'\d',args):
2010 # Mode where user specifies ranges of lines, like in %macro.
2010 # Mode where user specifies ranges of lines, like in %macro.
2011 # This means that you can't edit files whose names begin with
2011 # This means that you can't edit files whose names begin with
2012 # numbers this way. Tough.
2012 # numbers this way. Tough.
2013 ranges = args.split()
2013 ranges = args.split()
2014 data = ''.join(self.extract_input_slices(ranges,opts_r))
2014 data = ''.join(self.extract_input_slices(ranges,opts_r))
2015 elif args.endswith('.py'):
2015 elif args.endswith('.py'):
2016 filename = make_filename(args)
2016 filename = make_filename(args)
2017 data = ''
2017 data = ''
2018 use_temp = 0
2018 use_temp = 0
2019 elif args:
2019 elif args:
2020 try:
2020 try:
2021 # Load the parameter given as a variable. If not a string,
2021 # Load the parameter given as a variable. If not a string,
2022 # process it as an object instead (below)
2022 # process it as an object instead (below)
2023
2023
2024 #print '*** args',args,'type',type(args) # dbg
2024 #print '*** args',args,'type',type(args) # dbg
2025 data = eval(args,self.shell.user_ns)
2025 data = eval(args,self.shell.user_ns)
2026 if not type(data) in StringTypes:
2026 if not type(data) in StringTypes:
2027 raise DataIsObject
2027 raise DataIsObject
2028
2028
2029 except (NameError,SyntaxError):
2029 except (NameError,SyntaxError):
2030 # given argument is not a variable, try as a filename
2030 # given argument is not a variable, try as a filename
2031 filename = make_filename(args)
2031 filename = make_filename(args)
2032 if filename is None:
2032 if filename is None:
2033 warn("Argument given (%s) can't be found as a variable "
2033 warn("Argument given (%s) can't be found as a variable "
2034 "or as a filename." % args)
2034 "or as a filename." % args)
2035 return
2035 return
2036
2036
2037 data = ''
2037 data = ''
2038 use_temp = 0
2038 use_temp = 0
2039 except DataIsObject:
2039 except DataIsObject:
2040
2040
2041 # macros have a special edit function
2041 # macros have a special edit function
2042 if isinstance(data,Macro):
2042 if isinstance(data,Macro):
2043 self._edit_macro(args,data)
2043 self._edit_macro(args,data)
2044 return
2044 return
2045
2045
2046 # For objects, try to edit the file where they are defined
2046 # For objects, try to edit the file where they are defined
2047 try:
2047 try:
2048 filename = inspect.getabsfile(data)
2048 filename = inspect.getabsfile(data)
2049 datafile = 1
2049 datafile = 1
2050 except TypeError:
2050 except TypeError:
2051 filename = make_filename(args)
2051 filename = make_filename(args)
2052 datafile = 1
2052 datafile = 1
2053 warn('Could not find file where `%s` is defined.\n'
2053 warn('Could not find file where `%s` is defined.\n'
2054 'Opening a file named `%s`' % (args,filename))
2054 'Opening a file named `%s`' % (args,filename))
2055 # Now, make sure we can actually read the source (if it was in
2055 # Now, make sure we can actually read the source (if it was in
2056 # a temp file it's gone by now).
2056 # a temp file it's gone by now).
2057 if datafile:
2057 if datafile:
2058 try:
2058 try:
2059 if lineno is None:
2059 if lineno is None:
2060 lineno = inspect.getsourcelines(data)[1]
2060 lineno = inspect.getsourcelines(data)[1]
2061 except IOError:
2061 except IOError:
2062 filename = make_filename(args)
2062 filename = make_filename(args)
2063 if filename is None:
2063 if filename is None:
2064 warn('The file `%s` where `%s` was defined cannot '
2064 warn('The file `%s` where `%s` was defined cannot '
2065 'be read.' % (filename,data))
2065 'be read.' % (filename,data))
2066 return
2066 return
2067 use_temp = 0
2067 use_temp = 0
2068 else:
2068 else:
2069 data = ''
2069 data = ''
2070
2070
2071 if use_temp:
2071 if use_temp:
2072 filename = self.shell.mktempfile(data)
2072 filename = self.shell.mktempfile(data)
2073 print 'IPython will make a temporary file named:',filename
2073 print 'IPython will make a temporary file named:',filename
2074
2074
2075 # do actual editing here
2075 # do actual editing here
2076 print 'Editing...',
2076 print 'Editing...',
2077 sys.stdout.flush()
2077 sys.stdout.flush()
2078 self.shell.hooks.editor(filename,lineno)
2078 self.shell.hooks.editor(filename,lineno)
2079 if opts.has_key('x'): # -x prevents actual execution
2079 if opts.has_key('x'): # -x prevents actual execution
2080 print
2080 print
2081 else:
2081 else:
2082 print 'done. Executing edited code...'
2082 print 'done. Executing edited code...'
2083 if opts_r:
2083 if opts_r:
2084 self.shell.runlines(file_read(filename))
2084 self.shell.runlines(file_read(filename))
2085 else:
2085 else:
2086 self.shell.safe_execfile(filename,self.shell.user_ns,
2086 self.shell.safe_execfile(filename,self.shell.user_ns,
2087 self.shell.user_ns)
2087 self.shell.user_ns)
2088 if use_temp:
2088 if use_temp:
2089 try:
2089 try:
2090 return open(filename).read()
2090 return open(filename).read()
2091 except IOError,msg:
2091 except IOError,msg:
2092 if msg.filename == filename:
2092 if msg.filename == filename:
2093 warn('File not found. Did you forget to save?')
2093 warn('File not found. Did you forget to save?')
2094 return
2094 return
2095 else:
2095 else:
2096 self.shell.showtraceback()
2096 self.shell.showtraceback()
2097
2097
2098 def magic_xmode(self,parameter_s = ''):
2098 def magic_xmode(self,parameter_s = ''):
2099 """Switch modes for the exception handlers.
2099 """Switch modes for the exception handlers.
2100
2100
2101 Valid modes: Plain, Context and Verbose.
2101 Valid modes: Plain, Context and Verbose.
2102
2102
2103 If called without arguments, acts as a toggle."""
2103 If called without arguments, acts as a toggle."""
2104
2104
2105 def xmode_switch_err(name):
2105 def xmode_switch_err(name):
2106 warn('Error changing %s exception modes.\n%s' %
2106 warn('Error changing %s exception modes.\n%s' %
2107 (name,sys.exc_info()[1]))
2107 (name,sys.exc_info()[1]))
2108
2108
2109 shell = self.shell
2109 shell = self.shell
2110 new_mode = parameter_s.strip().capitalize()
2110 new_mode = parameter_s.strip().capitalize()
2111 try:
2111 try:
2112 shell.InteractiveTB.set_mode(mode=new_mode)
2112 shell.InteractiveTB.set_mode(mode=new_mode)
2113 print 'Exception reporting mode:',shell.InteractiveTB.mode
2113 print 'Exception reporting mode:',shell.InteractiveTB.mode
2114 except:
2114 except:
2115 xmode_switch_err('user')
2115 xmode_switch_err('user')
2116
2116
2117 # threaded shells use a special handler in sys.excepthook
2117 # threaded shells use a special handler in sys.excepthook
2118 if shell.isthreaded:
2118 if shell.isthreaded:
2119 try:
2119 try:
2120 shell.sys_excepthook.set_mode(mode=new_mode)
2120 shell.sys_excepthook.set_mode(mode=new_mode)
2121 except:
2121 except:
2122 xmode_switch_err('threaded')
2122 xmode_switch_err('threaded')
2123
2123
2124 def magic_colors(self,parameter_s = ''):
2124 def magic_colors(self,parameter_s = ''):
2125 """Switch color scheme for prompts, info system and exception handlers.
2125 """Switch color scheme for prompts, info system and exception handlers.
2126
2126
2127 Currently implemented schemes: NoColor, Linux, LightBG.
2127 Currently implemented schemes: NoColor, Linux, LightBG.
2128
2128
2129 Color scheme names are not case-sensitive."""
2129 Color scheme names are not case-sensitive."""
2130
2130
2131 def color_switch_err(name):
2131 def color_switch_err(name):
2132 warn('Error changing %s color schemes.\n%s' %
2132 warn('Error changing %s color schemes.\n%s' %
2133 (name,sys.exc_info()[1]))
2133 (name,sys.exc_info()[1]))
2134
2134
2135
2135
2136 new_scheme = parameter_s.strip()
2136 new_scheme = parameter_s.strip()
2137 if not new_scheme:
2137 if not new_scheme:
2138 print 'You must specify a color scheme.'
2138 print 'You must specify a color scheme.'
2139 return
2139 return
2140 import IPython.rlineimpl as readline
2140 import IPython.rlineimpl as readline
2141 if not readline.have_readline and sys.platform == "win32":
2141 if not readline.have_readline and sys.platform == "win32":
2142 msg = """\
2142 msg = """\
2143 Proper color support under MS Windows requires the pyreadline library.
2143 Proper color support under MS Windows requires the pyreadline library.
2144 You can find it at:
2144 You can find it at:
2145 http://ipython.scipy.org/moin/PyReadline/Intro
2145 http://ipython.scipy.org/moin/PyReadline/Intro
2146 Gary's readline needs the ctypes module, from:
2146 Gary's readline needs the ctypes module, from:
2147 http://starship.python.net/crew/theller/ctypes
2147 http://starship.python.net/crew/theller/ctypes
2148 (Note that ctypes is already part of Python versions 2.5 and newer).
2148 (Note that ctypes is already part of Python versions 2.5 and newer).
2149
2149
2150 Defaulting color scheme to 'NoColor'"""
2150 Defaulting color scheme to 'NoColor'"""
2151 new_scheme = 'NoColor'
2151 new_scheme = 'NoColor'
2152 warn(msg)
2152 warn(msg)
2153 # local shortcut
2153 # local shortcut
2154 shell = self.shell
2154 shell = self.shell
2155
2155
2156 # Set prompt colors
2156 # Set prompt colors
2157 try:
2157 try:
2158 shell.outputcache.set_colors(new_scheme)
2158 shell.outputcache.set_colors(new_scheme)
2159 except:
2159 except:
2160 color_switch_err('prompt')
2160 color_switch_err('prompt')
2161 else:
2161 else:
2162 shell.rc.colors = \
2162 shell.rc.colors = \
2163 shell.outputcache.color_table.active_scheme_name
2163 shell.outputcache.color_table.active_scheme_name
2164 # Set exception colors
2164 # Set exception colors
2165 try:
2165 try:
2166 shell.InteractiveTB.set_colors(scheme = new_scheme)
2166 shell.InteractiveTB.set_colors(scheme = new_scheme)
2167 shell.SyntaxTB.set_colors(scheme = new_scheme)
2167 shell.SyntaxTB.set_colors(scheme = new_scheme)
2168 except:
2168 except:
2169 color_switch_err('exception')
2169 color_switch_err('exception')
2170
2170
2171 # threaded shells use a verbose traceback in sys.excepthook
2171 # threaded shells use a verbose traceback in sys.excepthook
2172 if shell.isthreaded:
2172 if shell.isthreaded:
2173 try:
2173 try:
2174 shell.sys_excepthook.set_colors(scheme=new_scheme)
2174 shell.sys_excepthook.set_colors(scheme=new_scheme)
2175 except:
2175 except:
2176 color_switch_err('system exception handler')
2176 color_switch_err('system exception handler')
2177
2177
2178 # Set info (for 'object?') colors
2178 # Set info (for 'object?') colors
2179 if shell.rc.color_info:
2179 if shell.rc.color_info:
2180 try:
2180 try:
2181 shell.inspector.set_active_scheme(new_scheme)
2181 shell.inspector.set_active_scheme(new_scheme)
2182 except:
2182 except:
2183 color_switch_err('object inspector')
2183 color_switch_err('object inspector')
2184 else:
2184 else:
2185 shell.inspector.set_active_scheme('NoColor')
2185 shell.inspector.set_active_scheme('NoColor')
2186
2186
2187 def magic_color_info(self,parameter_s = ''):
2187 def magic_color_info(self,parameter_s = ''):
2188 """Toggle color_info.
2188 """Toggle color_info.
2189
2189
2190 The color_info configuration parameter controls whether colors are
2190 The color_info configuration parameter controls whether colors are
2191 used for displaying object details (by things like %psource, %pfile or
2191 used for displaying object details (by things like %psource, %pfile or
2192 the '?' system). This function toggles this value with each call.
2192 the '?' system). This function toggles this value with each call.
2193
2193
2194 Note that unless you have a fairly recent pager (less works better
2194 Note that unless you have a fairly recent pager (less works better
2195 than more) in your system, using colored object information displays
2195 than more) in your system, using colored object information displays
2196 will not work properly. Test it and see."""
2196 will not work properly. Test it and see."""
2197
2197
2198 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2198 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2199 self.magic_colors(self.shell.rc.colors)
2199 self.magic_colors(self.shell.rc.colors)
2200 print 'Object introspection functions have now coloring:',
2200 print 'Object introspection functions have now coloring:',
2201 print ['OFF','ON'][self.shell.rc.color_info]
2201 print ['OFF','ON'][self.shell.rc.color_info]
2202
2202
2203 def magic_Pprint(self, parameter_s=''):
2203 def magic_Pprint(self, parameter_s=''):
2204 """Toggle pretty printing on/off."""
2204 """Toggle pretty printing on/off."""
2205
2205
2206 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2206 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2207 print 'Pretty printing has been turned', \
2207 print 'Pretty printing has been turned', \
2208 ['OFF','ON'][self.shell.rc.pprint]
2208 ['OFF','ON'][self.shell.rc.pprint]
2209
2209
2210 def magic_exit(self, parameter_s=''):
2210 def magic_exit(self, parameter_s=''):
2211 """Exit IPython, confirming if configured to do so.
2211 """Exit IPython, confirming if configured to do so.
2212
2212
2213 You can configure whether IPython asks for confirmation upon exit by
2213 You can configure whether IPython asks for confirmation upon exit by
2214 setting the confirm_exit flag in the ipythonrc file."""
2214 setting the confirm_exit flag in the ipythonrc file."""
2215
2215
2216 self.shell.exit()
2216 self.shell.exit()
2217
2217
2218 def magic_quit(self, parameter_s=''):
2218 def magic_quit(self, parameter_s=''):
2219 """Exit IPython, confirming if configured to do so (like %exit)"""
2219 """Exit IPython, confirming if configured to do so (like %exit)"""
2220
2220
2221 self.shell.exit()
2221 self.shell.exit()
2222
2222
2223 def magic_Exit(self, parameter_s=''):
2223 def magic_Exit(self, parameter_s=''):
2224 """Exit IPython without confirmation."""
2224 """Exit IPython without confirmation."""
2225
2225
2226 self.shell.exit_now = True
2226 self.shell.exit_now = True
2227
2227
2228 #......................................................................
2228 #......................................................................
2229 # Functions to implement unix shell-type things
2229 # Functions to implement unix shell-type things
2230
2230
2231 def magic_alias(self, parameter_s = ''):
2231 def magic_alias(self, parameter_s = ''):
2232 """Define an alias for a system command.
2232 """Define an alias for a system command.
2233
2233
2234 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2234 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2235
2235
2236 Then, typing 'alias_name params' will execute the system command 'cmd
2236 Then, typing 'alias_name params' will execute the system command 'cmd
2237 params' (from your underlying operating system).
2237 params' (from your underlying operating system).
2238
2238
2239 Aliases have lower precedence than magic functions and Python normal
2239 Aliases have lower precedence than magic functions and Python normal
2240 variables, so if 'foo' is both a Python variable and an alias, the
2240 variables, so if 'foo' is both a Python variable and an alias, the
2241 alias can not be executed until 'del foo' removes the Python variable.
2241 alias can not be executed until 'del foo' removes the Python variable.
2242
2242
2243 You can use the %l specifier in an alias definition to represent the
2243 You can use the %l specifier in an alias definition to represent the
2244 whole line when the alias is called. For example:
2244 whole line when the alias is called. For example:
2245
2245
2246 In [2]: alias all echo "Input in brackets: <%l>"\\
2246 In [2]: alias all echo "Input in brackets: <%l>"\\
2247 In [3]: all hello world\\
2247 In [3]: all hello world\\
2248 Input in brackets: <hello world>
2248 Input in brackets: <hello world>
2249
2249
2250 You can also define aliases with parameters using %s specifiers (one
2250 You can also define aliases with parameters using %s specifiers (one
2251 per parameter):
2251 per parameter):
2252
2252
2253 In [1]: alias parts echo first %s second %s\\
2253 In [1]: alias parts echo first %s second %s\\
2254 In [2]: %parts A B\\
2254 In [2]: %parts A B\\
2255 first A second B\\
2255 first A second B\\
2256 In [3]: %parts A\\
2256 In [3]: %parts A\\
2257 Incorrect number of arguments: 2 expected.\\
2257 Incorrect number of arguments: 2 expected.\\
2258 parts is an alias to: 'echo first %s second %s'
2258 parts is an alias to: 'echo first %s second %s'
2259
2259
2260 Note that %l and %s are mutually exclusive. You can only use one or
2260 Note that %l and %s are mutually exclusive. You can only use one or
2261 the other in your aliases.
2261 the other in your aliases.
2262
2262
2263 Aliases expand Python variables just like system calls using ! or !!
2263 Aliases expand Python variables just like system calls using ! or !!
2264 do: all expressions prefixed with '$' get expanded. For details of
2264 do: all expressions prefixed with '$' get expanded. For details of
2265 the semantic rules, see PEP-215:
2265 the semantic rules, see PEP-215:
2266 http://www.python.org/peps/pep-0215.html. This is the library used by
2266 http://www.python.org/peps/pep-0215.html. This is the library used by
2267 IPython for variable expansion. If you want to access a true shell
2267 IPython for variable expansion. If you want to access a true shell
2268 variable, an extra $ is necessary to prevent its expansion by IPython:
2268 variable, an extra $ is necessary to prevent its expansion by IPython:
2269
2269
2270 In [6]: alias show echo\\
2270 In [6]: alias show echo\\
2271 In [7]: PATH='A Python string'\\
2271 In [7]: PATH='A Python string'\\
2272 In [8]: show $PATH\\
2272 In [8]: show $PATH\\
2273 A Python string\\
2273 A Python string\\
2274 In [9]: show $$PATH\\
2274 In [9]: show $$PATH\\
2275 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2275 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2276
2276
2277 You can use the alias facility to acess all of $PATH. See the %rehash
2277 You can use the alias facility to acess all of $PATH. See the %rehash
2278 and %rehashx functions, which automatically create aliases for the
2278 and %rehashx functions, which automatically create aliases for the
2279 contents of your $PATH.
2279 contents of your $PATH.
2280
2280
2281 If called with no parameters, %alias prints the current alias table."""
2281 If called with no parameters, %alias prints the current alias table."""
2282
2282
2283 par = parameter_s.strip()
2283 par = parameter_s.strip()
2284 if not par:
2284 if not par:
2285 stored = self.db.get('stored_aliases', {} )
2285 stored = self.db.get('stored_aliases', {} )
2286 atab = self.shell.alias_table
2286 atab = self.shell.alias_table
2287 aliases = atab.keys()
2287 aliases = atab.keys()
2288 aliases.sort()
2288 aliases.sort()
2289 res = []
2289 res = []
2290 showlast = []
2290 showlast = []
2291 for alias in aliases:
2291 for alias in aliases:
2292 tgt = atab[alias][1]
2292 tgt = atab[alias][1]
2293 # 'interesting' aliases
2293 # 'interesting' aliases
2294 if (alias in stored or
2294 if (alias in stored or
2295 alias.lower() != os.path.splitext(tgt)[0].lower() or
2295 alias.lower() != os.path.splitext(tgt)[0].lower() or
2296 ' ' in tgt):
2296 ' ' in tgt):
2297 showlast.append((alias, tgt))
2297 showlast.append((alias, tgt))
2298 else:
2298 else:
2299 res.append((alias, tgt ))
2299 res.append((alias, tgt ))
2300
2300
2301 # show most interesting aliases last
2301 # show most interesting aliases last
2302 res.extend(showlast)
2302 res.extend(showlast)
2303 print "Total number of aliases:",len(aliases)
2303 print "Total number of aliases:",len(aliases)
2304 return res
2304 return res
2305 try:
2305 try:
2306 alias,cmd = par.split(None,1)
2306 alias,cmd = par.split(None,1)
2307 except:
2307 except:
2308 print OInspect.getdoc(self.magic_alias)
2308 print OInspect.getdoc(self.magic_alias)
2309 else:
2309 else:
2310 nargs = cmd.count('%s')
2310 nargs = cmd.count('%s')
2311 if nargs>0 and cmd.find('%l')>=0:
2311 if nargs>0 and cmd.find('%l')>=0:
2312 error('The %s and %l specifiers are mutually exclusive '
2312 error('The %s and %l specifiers are mutually exclusive '
2313 'in alias definitions.')
2313 'in alias definitions.')
2314 else: # all looks OK
2314 else: # all looks OK
2315 self.shell.alias_table[alias] = (nargs,cmd)
2315 self.shell.alias_table[alias] = (nargs,cmd)
2316 self.shell.alias_table_validate(verbose=0)
2316 self.shell.alias_table_validate(verbose=0)
2317 # end magic_alias
2317 # end magic_alias
2318
2318
2319 def magic_unalias(self, parameter_s = ''):
2319 def magic_unalias(self, parameter_s = ''):
2320 """Remove an alias"""
2320 """Remove an alias"""
2321
2321
2322 aname = parameter_s.strip()
2322 aname = parameter_s.strip()
2323 if aname in self.shell.alias_table:
2323 if aname in self.shell.alias_table:
2324 del self.shell.alias_table[aname]
2324 del self.shell.alias_table[aname]
2325 stored = self.db.get('stored_aliases', {} )
2325 stored = self.db.get('stored_aliases', {} )
2326 if aname in stored:
2326 if aname in stored:
2327 print "Removing %stored alias",aname
2327 print "Removing %stored alias",aname
2328 del stored[aname]
2328 del stored[aname]
2329 self.db['stored_aliases'] = stored
2329 self.db['stored_aliases'] = stored
2330
2330
2331
2331
2332 def magic_rehashx(self, parameter_s = ''):
2332 def magic_rehashx(self, parameter_s = ''):
2333 """Update the alias table with all executable files in $PATH.
2333 """Update the alias table with all executable files in $PATH.
2334
2334
2335 This version explicitly checks that every entry in $PATH is a file
2335 This version explicitly checks that every entry in $PATH is a file
2336 with execute access (os.X_OK), so it is much slower than %rehash.
2336 with execute access (os.X_OK), so it is much slower than %rehash.
2337
2337
2338 Under Windows, it checks executability as a match agains a
2338 Under Windows, it checks executability as a match agains a
2339 '|'-separated string of extensions, stored in the IPython config
2339 '|'-separated string of extensions, stored in the IPython config
2340 variable win_exec_ext. This defaults to 'exe|com|bat'.
2340 variable win_exec_ext. This defaults to 'exe|com|bat'.
2341
2341
2342 This function also resets the root module cache of module completer,
2342 This function also resets the root module cache of module completer,
2343 used on slow filesystems.
2343 used on slow filesystems.
2344 """
2344 """
2345
2345
2346
2346
2347 ip = self.api
2347 ip = self.api
2348
2348
2349 # for the benefit of module completer in ipy_completers.py
2349 # for the benefit of module completer in ipy_completers.py
2350 del ip.db['rootmodules']
2350 del ip.db['rootmodules']
2351
2351
2352 path = [os.path.abspath(os.path.expanduser(p)) for p in
2352 path = [os.path.abspath(os.path.expanduser(p)) for p in
2353 os.environ.get('PATH','').split(os.pathsep)]
2353 os.environ.get('PATH','').split(os.pathsep)]
2354 path = filter(os.path.isdir,path)
2354 path = filter(os.path.isdir,path)
2355
2355
2356 alias_table = self.shell.alias_table
2356 alias_table = self.shell.alias_table
2357 syscmdlist = []
2357 syscmdlist = []
2358 if os.name == 'posix':
2358 if os.name == 'posix':
2359 isexec = lambda fname:os.path.isfile(fname) and \
2359 isexec = lambda fname:os.path.isfile(fname) and \
2360 os.access(fname,os.X_OK)
2360 os.access(fname,os.X_OK)
2361 else:
2361 else:
2362
2362
2363 try:
2363 try:
2364 winext = os.environ['pathext'].replace(';','|').replace('.','')
2364 winext = os.environ['pathext'].replace(';','|').replace('.','')
2365 except KeyError:
2365 except KeyError:
2366 winext = 'exe|com|bat|py'
2366 winext = 'exe|com|bat|py'
2367 if 'py' not in winext:
2367 if 'py' not in winext:
2368 winext += '|py'
2368 winext += '|py'
2369 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2369 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2370 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2370 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2371 savedir = os.getcwd()
2371 savedir = os.getcwd()
2372 try:
2372 try:
2373 # write the whole loop for posix/Windows so we don't have an if in
2373 # write the whole loop for posix/Windows so we don't have an if in
2374 # the innermost part
2374 # the innermost part
2375 if os.name == 'posix':
2375 if os.name == 'posix':
2376 for pdir in path:
2376 for pdir in path:
2377 os.chdir(pdir)
2377 os.chdir(pdir)
2378 for ff in os.listdir(pdir):
2378 for ff in os.listdir(pdir):
2379 if isexec(ff) and ff not in self.shell.no_alias:
2379 if isexec(ff) and ff not in self.shell.no_alias:
2380 # each entry in the alias table must be (N,name),
2380 # each entry in the alias table must be (N,name),
2381 # where N is the number of positional arguments of the
2381 # where N is the number of positional arguments of the
2382 # alias.
2382 # alias.
2383 alias_table[ff] = (0,ff)
2383 alias_table[ff] = (0,ff)
2384 syscmdlist.append(ff)
2384 syscmdlist.append(ff)
2385 else:
2385 else:
2386 for pdir in path:
2386 for pdir in path:
2387 os.chdir(pdir)
2387 os.chdir(pdir)
2388 for ff in os.listdir(pdir):
2388 for ff in os.listdir(pdir):
2389 base, ext = os.path.splitext(ff)
2389 base, ext = os.path.splitext(ff)
2390 if isexec(ff) and base not in self.shell.no_alias:
2390 if isexec(ff) and base not in self.shell.no_alias:
2391 if ext.lower() == '.exe':
2391 if ext.lower() == '.exe':
2392 ff = base
2392 ff = base
2393 alias_table[base.lower()] = (0,ff)
2393 alias_table[base.lower()] = (0,ff)
2394 syscmdlist.append(ff)
2394 syscmdlist.append(ff)
2395 # Make sure the alias table doesn't contain keywords or builtins
2395 # Make sure the alias table doesn't contain keywords or builtins
2396 self.shell.alias_table_validate()
2396 self.shell.alias_table_validate()
2397 # Call again init_auto_alias() so we get 'rm -i' and other
2397 # Call again init_auto_alias() so we get 'rm -i' and other
2398 # modified aliases since %rehashx will probably clobber them
2398 # modified aliases since %rehashx will probably clobber them
2399
2399
2400 # no, we don't want them. if %rehashx clobbers them, good,
2400 # no, we don't want them. if %rehashx clobbers them, good,
2401 # we'll probably get better versions
2401 # we'll probably get better versions
2402 # self.shell.init_auto_alias()
2402 # self.shell.init_auto_alias()
2403 db = ip.db
2403 db = ip.db
2404 db['syscmdlist'] = syscmdlist
2404 db['syscmdlist'] = syscmdlist
2405 finally:
2405 finally:
2406 os.chdir(savedir)
2406 os.chdir(savedir)
2407
2407
2408 def magic_pwd(self, parameter_s = ''):
2408 def magic_pwd(self, parameter_s = ''):
2409 """Return the current working directory path."""
2409 """Return the current working directory path."""
2410 return os.getcwd()
2410 return os.getcwd()
2411
2411
2412 def magic_cd(self, parameter_s=''):
2412 def magic_cd(self, parameter_s=''):
2413 """Change the current working directory.
2413 """Change the current working directory.
2414
2414
2415 This command automatically maintains an internal list of directories
2415 This command automatically maintains an internal list of directories
2416 you visit during your IPython session, in the variable _dh. The
2416 you visit during your IPython session, in the variable _dh. The
2417 command %dhist shows this history nicely formatted. You can also
2417 command %dhist shows this history nicely formatted. You can also
2418 do 'cd -<tab>' to see directory history conveniently.
2418 do 'cd -<tab>' to see directory history conveniently.
2419
2419
2420 Usage:
2420 Usage:
2421
2421
2422 cd 'dir': changes to directory 'dir'.
2422 cd 'dir': changes to directory 'dir'.
2423
2423
2424 cd -: changes to the last visited directory.
2424 cd -: changes to the last visited directory.
2425
2425
2426 cd -<n>: changes to the n-th directory in the directory history.
2426 cd -<n>: changes to the n-th directory in the directory history.
2427
2427
2428 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2428 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2429 (note: cd <bookmark_name> is enough if there is no
2429 (note: cd <bookmark_name> is enough if there is no
2430 directory <bookmark_name>, but a bookmark with the name exists.)
2430 directory <bookmark_name>, but a bookmark with the name exists.)
2431 'cd -b <tab>' allows you to tab-complete bookmark names.
2431 'cd -b <tab>' allows you to tab-complete bookmark names.
2432
2432
2433 Options:
2433 Options:
2434
2434
2435 -q: quiet. Do not print the working directory after the cd command is
2435 -q: quiet. Do not print the working directory after the cd command is
2436 executed. By default IPython's cd command does print this directory,
2436 executed. By default IPython's cd command does print this directory,
2437 since the default prompts do not display path information.
2437 since the default prompts do not display path information.
2438
2438
2439 Note that !cd doesn't work for this purpose because the shell where
2439 Note that !cd doesn't work for this purpose because the shell where
2440 !command runs is immediately discarded after executing 'command'."""
2440 !command runs is immediately discarded after executing 'command'."""
2441
2441
2442 parameter_s = parameter_s.strip()
2442 parameter_s = parameter_s.strip()
2443 #bkms = self.shell.persist.get("bookmarks",{})
2443 #bkms = self.shell.persist.get("bookmarks",{})
2444
2444
2445 numcd = re.match(r'(-)(\d+)$',parameter_s)
2445 numcd = re.match(r'(-)(\d+)$',parameter_s)
2446 # jump in directory history by number
2446 # jump in directory history by number
2447 if numcd:
2447 if numcd:
2448 nn = int(numcd.group(2))
2448 nn = int(numcd.group(2))
2449 try:
2449 try:
2450 ps = self.shell.user_ns['_dh'][nn]
2450 ps = self.shell.user_ns['_dh'][nn]
2451 except IndexError:
2451 except IndexError:
2452 print 'The requested directory does not exist in history.'
2452 print 'The requested directory does not exist in history.'
2453 return
2453 return
2454 else:
2454 else:
2455 opts = {}
2455 opts = {}
2456 else:
2456 else:
2457 #turn all non-space-escaping backslashes to slashes,
2457 #turn all non-space-escaping backslashes to slashes,
2458 # for c:\windows\directory\names\
2458 # for c:\windows\directory\names\
2459 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2459 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2460 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2460 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2461 # jump to previous
2461 # jump to previous
2462 if ps == '-':
2462 if ps == '-':
2463 try:
2463 try:
2464 ps = self.shell.user_ns['_dh'][-2]
2464 ps = self.shell.user_ns['_dh'][-2]
2465 except IndexError:
2465 except IndexError:
2466 print 'No previous directory to change to.'
2466 print 'No previous directory to change to.'
2467 return
2467 return
2468 # jump to bookmark if needed
2468 # jump to bookmark if needed
2469 else:
2469 else:
2470 if not os.path.isdir(ps) or opts.has_key('b'):
2470 if not os.path.isdir(ps) or opts.has_key('b'):
2471 bkms = self.db.get('bookmarks', {})
2471 bkms = self.db.get('bookmarks', {})
2472
2472
2473 if bkms.has_key(ps):
2473 if bkms.has_key(ps):
2474 target = bkms[ps]
2474 target = bkms[ps]
2475 print '(bookmark:%s) -> %s' % (ps,target)
2475 print '(bookmark:%s) -> %s' % (ps,target)
2476 ps = target
2476 ps = target
2477 else:
2477 else:
2478 if opts.has_key('b'):
2478 if opts.has_key('b'):
2479 error("Bookmark '%s' not found. "
2479 error("Bookmark '%s' not found. "
2480 "Use '%%bookmark -l' to see your bookmarks." % ps)
2480 "Use '%%bookmark -l' to see your bookmarks." % ps)
2481 return
2481 return
2482
2482
2483 # at this point ps should point to the target dir
2483 # at this point ps should point to the target dir
2484 if ps:
2484 if ps:
2485 try:
2485 try:
2486 os.chdir(os.path.expanduser(ps))
2486 os.chdir(os.path.expanduser(ps))
2487 if self.shell.rc.term_title:
2487 if self.shell.rc.term_title:
2488 #print 'set term title:',self.shell.rc.term_title # dbg
2488 #print 'set term title:',self.shell.rc.term_title # dbg
2489 ttitle = ("IPy:" + (
2489 ttitle = ("IPy:" + (
2490 os.getcwd() == '/' and '/' or \
2490 os.getcwd() == '/' and '/' or \
2491 os.path.basename(os.getcwd())))
2491 os.path.basename(os.getcwd())))
2492 platutils.set_term_title(ttitle)
2492 platutils.set_term_title(ttitle)
2493 except OSError:
2493 except OSError:
2494 print sys.exc_info()[1]
2494 print sys.exc_info()[1]
2495 else:
2495 else:
2496 cwd = os.getcwd()
2496 cwd = os.getcwd()
2497 dhist = self.shell.user_ns['_dh']
2497 dhist = self.shell.user_ns['_dh']
2498 dhist.append(cwd)
2498 dhist.append(cwd)
2499 self.db['dhist'] = compress_dhist(dhist)[-100:]
2499 self.db['dhist'] = compress_dhist(dhist)[-100:]
2500
2500
2501 else:
2501 else:
2502 os.chdir(self.shell.home_dir)
2502 os.chdir(self.shell.home_dir)
2503 if self.shell.rc.term_title:
2503 if self.shell.rc.term_title:
2504 platutils.set_term_title("IPy:~")
2504 platutils.set_term_title("IPy:~")
2505 cwd = os.getcwd()
2505 cwd = os.getcwd()
2506 dhist = self.shell.user_ns['_dh']
2506 dhist = self.shell.user_ns['_dh']
2507 dhist.append(cwd)
2507 dhist.append(cwd)
2508 self.db['dhist'] = compress_dhist(dhist)[-100:]
2508 self.db['dhist'] = compress_dhist(dhist)[-100:]
2509 if not 'q' in opts:
2509 if not 'q' in opts:
2510 print self.shell.user_ns['_dh'][-1]
2510 print self.shell.user_ns['_dh'][-1]
2511
2511
2512
2512
2513 def magic_env(self, parameter_s=''):
2513 def magic_env(self, parameter_s=''):
2514 """List environment variables."""
2514 """List environment variables."""
2515
2515
2516 return os.environ.data
2516 return os.environ.data
2517
2517
2518 def magic_pushd(self, parameter_s=''):
2518 def magic_pushd(self, parameter_s=''):
2519 """Place the current dir on stack and change directory.
2519 """Place the current dir on stack and change directory.
2520
2520
2521 Usage:\\
2521 Usage:\\
2522 %pushd ['dirname']
2522 %pushd ['dirname']
2523
2523
2524 %pushd with no arguments does a %pushd to your home directory.
2524 %pushd with no arguments does a %pushd to your home directory.
2525 """
2525 """
2526 if parameter_s == '': parameter_s = '~'
2526 if parameter_s == '': parameter_s = '~'
2527 dir_s = self.shell.dir_stack
2527 dir_s = self.shell.dir_stack
2528 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2528 if len(dir_s)>0 and os.path.expanduser(parameter_s) != \
2529 os.path.expanduser(self.shell.dir_stack[0]):
2529 os.path.expanduser(self.shell.dir_stack[0]):
2530 try:
2530 try:
2531 self.magic_cd(parameter_s)
2531 self.magic_cd(parameter_s)
2532 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2532 dir_s.insert(0,os.getcwd().replace(self.home_dir,'~'))
2533 self.magic_dirs()
2533 self.magic_dirs()
2534 except:
2534 except:
2535 print 'Invalid directory'
2535 print 'Invalid directory'
2536 else:
2536 else:
2537 print 'You are already there!'
2537 print 'You are already there!'
2538
2538
2539 def magic_popd(self, parameter_s=''):
2539 def magic_popd(self, parameter_s=''):
2540 """Change to directory popped off the top of the stack.
2540 """Change to directory popped off the top of the stack.
2541 """
2541 """
2542 if len (self.shell.dir_stack) > 1:
2542 if len (self.shell.dir_stack) > 1:
2543 self.shell.dir_stack.pop(0)
2543 self.shell.dir_stack.pop(0)
2544 self.magic_cd(self.shell.dir_stack[0])
2544 self.magic_cd(self.shell.dir_stack[0])
2545 print self.shell.dir_stack[0]
2545 print self.shell.dir_stack[0]
2546 else:
2546 else:
2547 print "You can't remove the starting directory from the stack:",\
2547 print "You can't remove the starting directory from the stack:",\
2548 self.shell.dir_stack
2548 self.shell.dir_stack
2549
2549
2550 def magic_dirs(self, parameter_s=''):
2550 def magic_dirs(self, parameter_s=''):
2551 """Return the current directory stack."""
2551 """Return the current directory stack."""
2552
2552
2553 return self.shell.dir_stack[:]
2553 return self.shell.dir_stack[:]
2554
2554
2555 def magic_sc(self, parameter_s=''):
2555 def magic_sc(self, parameter_s=''):
2556 """Shell capture - execute a shell command and capture its output.
2556 """Shell capture - execute a shell command and capture its output.
2557
2557
2558 DEPRECATED. Suboptimal, retained for backwards compatibility.
2558 DEPRECATED. Suboptimal, retained for backwards compatibility.
2559
2559
2560 You should use the form 'var = !command' instead. Example:
2560 You should use the form 'var = !command' instead. Example:
2561
2561
2562 "%sc -l myfiles = ls ~" should now be written as
2562 "%sc -l myfiles = ls ~" should now be written as
2563
2563
2564 "myfiles = !ls ~"
2564 "myfiles = !ls ~"
2565
2565
2566 myfiles.s, myfiles.l and myfiles.n still apply as documented
2566 myfiles.s, myfiles.l and myfiles.n still apply as documented
2567 below.
2567 below.
2568
2568
2569 --
2569 --
2570 %sc [options] varname=command
2570 %sc [options] varname=command
2571
2571
2572 IPython will run the given command using commands.getoutput(), and
2572 IPython will run the given command using commands.getoutput(), and
2573 will then update the user's interactive namespace with a variable
2573 will then update the user's interactive namespace with a variable
2574 called varname, containing the value of the call. Your command can
2574 called varname, containing the value of the call. Your command can
2575 contain shell wildcards, pipes, etc.
2575 contain shell wildcards, pipes, etc.
2576
2576
2577 The '=' sign in the syntax is mandatory, and the variable name you
2577 The '=' sign in the syntax is mandatory, and the variable name you
2578 supply must follow Python's standard conventions for valid names.
2578 supply must follow Python's standard conventions for valid names.
2579
2579
2580 (A special format without variable name exists for internal use)
2580 (A special format without variable name exists for internal use)
2581
2581
2582 Options:
2582 Options:
2583
2583
2584 -l: list output. Split the output on newlines into a list before
2584 -l: list output. Split the output on newlines into a list before
2585 assigning it to the given variable. By default the output is stored
2585 assigning it to the given variable. By default the output is stored
2586 as a single string.
2586 as a single string.
2587
2587
2588 -v: verbose. Print the contents of the variable.
2588 -v: verbose. Print the contents of the variable.
2589
2589
2590 In most cases you should not need to split as a list, because the
2590 In most cases you should not need to split as a list, because the
2591 returned value is a special type of string which can automatically
2591 returned value is a special type of string which can automatically
2592 provide its contents either as a list (split on newlines) or as a
2592 provide its contents either as a list (split on newlines) or as a
2593 space-separated string. These are convenient, respectively, either
2593 space-separated string. These are convenient, respectively, either
2594 for sequential processing or to be passed to a shell command.
2594 for sequential processing or to be passed to a shell command.
2595
2595
2596 For example:
2596 For example:
2597
2597
2598 # Capture into variable a
2598 # Capture into variable a
2599 In [9]: sc a=ls *py
2599 In [9]: sc a=ls *py
2600
2600
2601 # a is a string with embedded newlines
2601 # a is a string with embedded newlines
2602 In [10]: a
2602 In [10]: a
2603 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2603 Out[10]: 'setup.py\nwin32_manual_post_install.py'
2604
2604
2605 # which can be seen as a list:
2605 # which can be seen as a list:
2606 In [11]: a.l
2606 In [11]: a.l
2607 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2607 Out[11]: ['setup.py', 'win32_manual_post_install.py']
2608
2608
2609 # or as a whitespace-separated string:
2609 # or as a whitespace-separated string:
2610 In [12]: a.s
2610 In [12]: a.s
2611 Out[12]: 'setup.py win32_manual_post_install.py'
2611 Out[12]: 'setup.py win32_manual_post_install.py'
2612
2612
2613 # a.s is useful to pass as a single command line:
2613 # a.s is useful to pass as a single command line:
2614 In [13]: !wc -l $a.s
2614 In [13]: !wc -l $a.s
2615 146 setup.py
2615 146 setup.py
2616 130 win32_manual_post_install.py
2616 130 win32_manual_post_install.py
2617 276 total
2617 276 total
2618
2618
2619 # while the list form is useful to loop over:
2619 # while the list form is useful to loop over:
2620 In [14]: for f in a.l:
2620 In [14]: for f in a.l:
2621 ....: !wc -l $f
2621 ....: !wc -l $f
2622 ....:
2622 ....:
2623 146 setup.py
2623 146 setup.py
2624 130 win32_manual_post_install.py
2624 130 win32_manual_post_install.py
2625
2625
2626 Similiarly, the lists returned by the -l option are also special, in
2626 Similiarly, the lists returned by the -l option are also special, in
2627 the sense that you can equally invoke the .s attribute on them to
2627 the sense that you can equally invoke the .s attribute on them to
2628 automatically get a whitespace-separated string from their contents:
2628 automatically get a whitespace-separated string from their contents:
2629
2629
2630 In [1]: sc -l b=ls *py
2630 In [1]: sc -l b=ls *py
2631
2631
2632 In [2]: b
2632 In [2]: b
2633 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2633 Out[2]: ['setup.py', 'win32_manual_post_install.py']
2634
2634
2635 In [3]: b.s
2635 In [3]: b.s
2636 Out[3]: 'setup.py win32_manual_post_install.py'
2636 Out[3]: 'setup.py win32_manual_post_install.py'
2637
2637
2638 In summary, both the lists and strings used for ouptut capture have
2638 In summary, both the lists and strings used for ouptut capture have
2639 the following special attributes:
2639 the following special attributes:
2640
2640
2641 .l (or .list) : value as list.
2641 .l (or .list) : value as list.
2642 .n (or .nlstr): value as newline-separated string.
2642 .n (or .nlstr): value as newline-separated string.
2643 .s (or .spstr): value as space-separated string.
2643 .s (or .spstr): value as space-separated string.
2644 """
2644 """
2645
2645
2646 opts,args = self.parse_options(parameter_s,'lv')
2646 opts,args = self.parse_options(parameter_s,'lv')
2647 # Try to get a variable name and command to run
2647 # Try to get a variable name and command to run
2648 try:
2648 try:
2649 # the variable name must be obtained from the parse_options
2649 # the variable name must be obtained from the parse_options
2650 # output, which uses shlex.split to strip options out.
2650 # output, which uses shlex.split to strip options out.
2651 var,_ = args.split('=',1)
2651 var,_ = args.split('=',1)
2652 var = var.strip()
2652 var = var.strip()
2653 # But the the command has to be extracted from the original input
2653 # But the the command has to be extracted from the original input
2654 # parameter_s, not on what parse_options returns, to avoid the
2654 # parameter_s, not on what parse_options returns, to avoid the
2655 # quote stripping which shlex.split performs on it.
2655 # quote stripping which shlex.split performs on it.
2656 _,cmd = parameter_s.split('=',1)
2656 _,cmd = parameter_s.split('=',1)
2657 except ValueError:
2657 except ValueError:
2658 var,cmd = '',''
2658 var,cmd = '',''
2659 # If all looks ok, proceed
2659 # If all looks ok, proceed
2660 out,err = self.shell.getoutputerror(cmd)
2660 out,err = self.shell.getoutputerror(cmd)
2661 if err:
2661 if err:
2662 print >> Term.cerr,err
2662 print >> Term.cerr,err
2663 if opts.has_key('l'):
2663 if opts.has_key('l'):
2664 out = SList(out.split('\n'))
2664 out = SList(out.split('\n'))
2665 else:
2665 else:
2666 out = LSString(out)
2666 out = LSString(out)
2667 if opts.has_key('v'):
2667 if opts.has_key('v'):
2668 print '%s ==\n%s' % (var,pformat(out))
2668 print '%s ==\n%s' % (var,pformat(out))
2669 if var:
2669 if var:
2670 self.shell.user_ns.update({var:out})
2670 self.shell.user_ns.update({var:out})
2671 else:
2671 else:
2672 return out
2672 return out
2673
2673
2674 def magic_sx(self, parameter_s=''):
2674 def magic_sx(self, parameter_s=''):
2675 """Shell execute - run a shell command and capture its output.
2675 """Shell execute - run a shell command and capture its output.
2676
2676
2677 %sx command
2677 %sx command
2678
2678
2679 IPython will run the given command using commands.getoutput(), and
2679 IPython will run the given command using commands.getoutput(), and
2680 return the result formatted as a list (split on '\\n'). Since the
2680 return the result formatted as a list (split on '\\n'). Since the
2681 output is _returned_, it will be stored in ipython's regular output
2681 output is _returned_, it will be stored in ipython's regular output
2682 cache Out[N] and in the '_N' automatic variables.
2682 cache Out[N] and in the '_N' automatic variables.
2683
2683
2684 Notes:
2684 Notes:
2685
2685
2686 1) If an input line begins with '!!', then %sx is automatically
2686 1) If an input line begins with '!!', then %sx is automatically
2687 invoked. That is, while:
2687 invoked. That is, while:
2688 !ls
2688 !ls
2689 causes ipython to simply issue system('ls'), typing
2689 causes ipython to simply issue system('ls'), typing
2690 !!ls
2690 !!ls
2691 is a shorthand equivalent to:
2691 is a shorthand equivalent to:
2692 %sx ls
2692 %sx ls
2693
2693
2694 2) %sx differs from %sc in that %sx automatically splits into a list,
2694 2) %sx differs from %sc in that %sx automatically splits into a list,
2695 like '%sc -l'. The reason for this is to make it as easy as possible
2695 like '%sc -l'. The reason for this is to make it as easy as possible
2696 to process line-oriented shell output via further python commands.
2696 to process line-oriented shell output via further python commands.
2697 %sc is meant to provide much finer control, but requires more
2697 %sc is meant to provide much finer control, but requires more
2698 typing.
2698 typing.
2699
2699
2700 3) Just like %sc -l, this is a list with special attributes:
2700 3) Just like %sc -l, this is a list with special attributes:
2701
2701
2702 .l (or .list) : value as list.
2702 .l (or .list) : value as list.
2703 .n (or .nlstr): value as newline-separated string.
2703 .n (or .nlstr): value as newline-separated string.
2704 .s (or .spstr): value as whitespace-separated string.
2704 .s (or .spstr): value as whitespace-separated string.
2705
2705
2706 This is very useful when trying to use such lists as arguments to
2706 This is very useful when trying to use such lists as arguments to
2707 system commands."""
2707 system commands."""
2708
2708
2709 if parameter_s:
2709 if parameter_s:
2710 out,err = self.shell.getoutputerror(parameter_s)
2710 out,err = self.shell.getoutputerror(parameter_s)
2711 if err:
2711 if err:
2712 print >> Term.cerr,err
2712 print >> Term.cerr,err
2713 return SList(out.split('\n'))
2713 return SList(out.split('\n'))
2714
2714
2715 def magic_bg(self, parameter_s=''):
2715 def magic_bg(self, parameter_s=''):
2716 """Run a job in the background, in a separate thread.
2716 """Run a job in the background, in a separate thread.
2717
2717
2718 For example,
2718 For example,
2719
2719
2720 %bg myfunc(x,y,z=1)
2720 %bg myfunc(x,y,z=1)
2721
2721
2722 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2722 will execute 'myfunc(x,y,z=1)' in a background thread. As soon as the
2723 execution starts, a message will be printed indicating the job
2723 execution starts, a message will be printed indicating the job
2724 number. If your job number is 5, you can use
2724 number. If your job number is 5, you can use
2725
2725
2726 myvar = jobs.result(5) or myvar = jobs[5].result
2726 myvar = jobs.result(5) or myvar = jobs[5].result
2727
2727
2728 to assign this result to variable 'myvar'.
2728 to assign this result to variable 'myvar'.
2729
2729
2730 IPython has a job manager, accessible via the 'jobs' object. You can
2730 IPython has a job manager, accessible via the 'jobs' object. You can
2731 type jobs? to get more information about it, and use jobs.<TAB> to see
2731 type jobs? to get more information about it, and use jobs.<TAB> to see
2732 its attributes. All attributes not starting with an underscore are
2732 its attributes. All attributes not starting with an underscore are
2733 meant for public use.
2733 meant for public use.
2734
2734
2735 In particular, look at the jobs.new() method, which is used to create
2735 In particular, look at the jobs.new() method, which is used to create
2736 new jobs. This magic %bg function is just a convenience wrapper
2736 new jobs. This magic %bg function is just a convenience wrapper
2737 around jobs.new(), for expression-based jobs. If you want to create a
2737 around jobs.new(), for expression-based jobs. If you want to create a
2738 new job with an explicit function object and arguments, you must call
2738 new job with an explicit function object and arguments, you must call
2739 jobs.new() directly.
2739 jobs.new() directly.
2740
2740
2741 The jobs.new docstring also describes in detail several important
2741 The jobs.new docstring also describes in detail several important
2742 caveats associated with a thread-based model for background job
2742 caveats associated with a thread-based model for background job
2743 execution. Type jobs.new? for details.
2743 execution. Type jobs.new? for details.
2744
2744
2745 You can check the status of all jobs with jobs.status().
2745 You can check the status of all jobs with jobs.status().
2746
2746
2747 The jobs variable is set by IPython into the Python builtin namespace.
2747 The jobs variable is set by IPython into the Python builtin namespace.
2748 If you ever declare a variable named 'jobs', you will shadow this
2748 If you ever declare a variable named 'jobs', you will shadow this
2749 name. You can either delete your global jobs variable to regain
2749 name. You can either delete your global jobs variable to regain
2750 access to the job manager, or make a new name and assign it manually
2750 access to the job manager, or make a new name and assign it manually
2751 to the manager (stored in IPython's namespace). For example, to
2751 to the manager (stored in IPython's namespace). For example, to
2752 assign the job manager to the Jobs name, use:
2752 assign the job manager to the Jobs name, use:
2753
2753
2754 Jobs = __builtins__.jobs"""
2754 Jobs = __builtins__.jobs"""
2755
2755
2756 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2756 self.shell.jobs.new(parameter_s,self.shell.user_ns)
2757
2757
2758
2758
2759 def magic_bookmark(self, parameter_s=''):
2759 def magic_bookmark(self, parameter_s=''):
2760 """Manage IPython's bookmark system.
2760 """Manage IPython's bookmark system.
2761
2761
2762 %bookmark <name> - set bookmark to current dir
2762 %bookmark <name> - set bookmark to current dir
2763 %bookmark <name> <dir> - set bookmark to <dir>
2763 %bookmark <name> <dir> - set bookmark to <dir>
2764 %bookmark -l - list all bookmarks
2764 %bookmark -l - list all bookmarks
2765 %bookmark -d <name> - remove bookmark
2765 %bookmark -d <name> - remove bookmark
2766 %bookmark -r - remove all bookmarks
2766 %bookmark -r - remove all bookmarks
2767
2767
2768 You can later on access a bookmarked folder with:
2768 You can later on access a bookmarked folder with:
2769 %cd -b <name>
2769 %cd -b <name>
2770 or simply '%cd <name>' if there is no directory called <name> AND
2770 or simply '%cd <name>' if there is no directory called <name> AND
2771 there is such a bookmark defined.
2771 there is such a bookmark defined.
2772
2772
2773 Your bookmarks persist through IPython sessions, but they are
2773 Your bookmarks persist through IPython sessions, but they are
2774 associated with each profile."""
2774 associated with each profile."""
2775
2775
2776 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2776 opts,args = self.parse_options(parameter_s,'drl',mode='list')
2777 if len(args) > 2:
2777 if len(args) > 2:
2778 error('You can only give at most two arguments')
2778 error('You can only give at most two arguments')
2779 return
2779 return
2780
2780
2781 bkms = self.db.get('bookmarks',{})
2781 bkms = self.db.get('bookmarks',{})
2782
2782
2783 if opts.has_key('d'):
2783 if opts.has_key('d'):
2784 try:
2784 try:
2785 todel = args[0]
2785 todel = args[0]
2786 except IndexError:
2786 except IndexError:
2787 error('You must provide a bookmark to delete')
2787 error('You must provide a bookmark to delete')
2788 else:
2788 else:
2789 try:
2789 try:
2790 del bkms[todel]
2790 del bkms[todel]
2791 except:
2791 except:
2792 error("Can't delete bookmark '%s'" % todel)
2792 error("Can't delete bookmark '%s'" % todel)
2793 elif opts.has_key('r'):
2793 elif opts.has_key('r'):
2794 bkms = {}
2794 bkms = {}
2795 elif opts.has_key('l'):
2795 elif opts.has_key('l'):
2796 bks = bkms.keys()
2796 bks = bkms.keys()
2797 bks.sort()
2797 bks.sort()
2798 if bks:
2798 if bks:
2799 size = max(map(len,bks))
2799 size = max(map(len,bks))
2800 else:
2800 else:
2801 size = 0
2801 size = 0
2802 fmt = '%-'+str(size)+'s -> %s'
2802 fmt = '%-'+str(size)+'s -> %s'
2803 print 'Current bookmarks:'
2803 print 'Current bookmarks:'
2804 for bk in bks:
2804 for bk in bks:
2805 print fmt % (bk,bkms[bk])
2805 print fmt % (bk,bkms[bk])
2806 else:
2806 else:
2807 if not args:
2807 if not args:
2808 error("You must specify the bookmark name")
2808 error("You must specify the bookmark name")
2809 elif len(args)==1:
2809 elif len(args)==1:
2810 bkms[args[0]] = os.getcwd()
2810 bkms[args[0]] = os.getcwd()
2811 elif len(args)==2:
2811 elif len(args)==2:
2812 bkms[args[0]] = args[1]
2812 bkms[args[0]] = args[1]
2813 self.db['bookmarks'] = bkms
2813 self.db['bookmarks'] = bkms
2814
2814
2815 def magic_pycat(self, parameter_s=''):
2815 def magic_pycat(self, parameter_s=''):
2816 """Show a syntax-highlighted file through a pager.
2816 """Show a syntax-highlighted file through a pager.
2817
2817
2818 This magic is similar to the cat utility, but it will assume the file
2818 This magic is similar to the cat utility, but it will assume the file
2819 to be Python source and will show it with syntax highlighting. """
2819 to be Python source and will show it with syntax highlighting. """
2820
2820
2821 try:
2821 try:
2822 filename = get_py_filename(parameter_s)
2822 filename = get_py_filename(parameter_s)
2823 cont = file_read(filename)
2823 cont = file_read(filename)
2824 except IOError:
2824 except IOError:
2825 try:
2825 try:
2826 cont = eval(parameter_s,self.user_ns)
2826 cont = eval(parameter_s,self.user_ns)
2827 except NameError:
2827 except NameError:
2828 cont = None
2828 cont = None
2829 if cont is None:
2829 if cont is None:
2830 print "Error: no such file or variable"
2830 print "Error: no such file or variable"
2831 return
2831 return
2832
2832
2833 page(self.shell.pycolorize(cont),
2833 page(self.shell.pycolorize(cont),
2834 screen_lines=self.shell.rc.screen_length)
2834 screen_lines=self.shell.rc.screen_length)
2835
2835
2836 def magic_cpaste(self, parameter_s=''):
2836 def magic_cpaste(self, parameter_s=''):
2837 """Allows you to paste & execute a pre-formatted code block from clipboard
2837 """Allows you to paste & execute a pre-formatted code block from clipboard
2838
2838
2839 You must terminate the block with '--' (two minus-signs) alone on the
2839 You must terminate the block with '--' (two minus-signs) alone on the
2840 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2840 line. You can also provide your own sentinel with '%paste -s %%' ('%%'
2841 is the new sentinel for this operation)
2841 is the new sentinel for this operation)
2842
2842
2843 The block is dedented prior to execution to enable execution of method
2843 The block is dedented prior to execution to enable execution of method
2844 definitions. '>' and '+' characters at the beginning of a line are
2844 definitions. '>' and '+' characters at the beginning of a line are
2845 ignored, to allow pasting directly from e-mails or diff files. The
2845 ignored, to allow pasting directly from e-mails or diff files. The
2846 executed block is also assigned to variable named 'pasted_block' for
2846 executed block is also assigned to variable named 'pasted_block' for
2847 later editing with '%edit pasted_block'.
2847 later editing with '%edit pasted_block'.
2848
2848
2849 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2849 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
2850 This assigns the pasted block to variable 'foo' as string, without
2850 This assigns the pasted block to variable 'foo' as string, without
2851 dedenting or executing it.
2851 dedenting or executing it.
2852
2852
2853 Do not be alarmed by garbled output on Windows (it's a readline bug).
2853 Do not be alarmed by garbled output on Windows (it's a readline bug).
2854 Just press enter and type -- (and press enter again) and the block
2854 Just press enter and type -- (and press enter again) and the block
2855 will be what was just pasted.
2855 will be what was just pasted.
2856
2856
2857 IPython statements (magics, shell escapes) are not supported (yet).
2857 IPython statements (magics, shell escapes) are not supported (yet).
2858 """
2858 """
2859 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2859 opts,args = self.parse_options(parameter_s,'s:',mode='string')
2860 par = args.strip()
2860 par = args.strip()
2861 sentinel = opts.get('s','--')
2861 sentinel = opts.get('s','--')
2862
2862
2863 from IPython import iplib
2863 from IPython import iplib
2864 lines = []
2864 lines = []
2865 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2865 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
2866 while 1:
2866 while 1:
2867 l = iplib.raw_input_original(':')
2867 l = iplib.raw_input_original(':')
2868 if l ==sentinel:
2868 if l ==sentinel:
2869 break
2869 break
2870 lines.append(l.lstrip('>').lstrip('+'))
2870 lines.append(l.lstrip('>').lstrip('+'))
2871 block = "\n".join(lines) + '\n'
2871 block = "\n".join(lines) + '\n'
2872 #print "block:\n",block
2872 #print "block:\n",block
2873 if not par:
2873 if not par:
2874 b = textwrap.dedent(block)
2874 b = textwrap.dedent(block)
2875 exec b in self.user_ns
2875 exec b in self.user_ns
2876 self.user_ns['pasted_block'] = b
2876 self.user_ns['pasted_block'] = b
2877 else:
2877 else:
2878 self.user_ns[par] = block
2878 self.user_ns[par] = block
2879 print "Block assigned to '%s'" % par
2879 print "Block assigned to '%s'" % par
2880
2880
2881 def magic_quickref(self,arg):
2881 def magic_quickref(self,arg):
2882 """ Show a quick reference sheet """
2882 """ Show a quick reference sheet """
2883 import IPython.usage
2883 import IPython.usage
2884 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
2884 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
2885
2885
2886 page(qr)
2886 page(qr)
2887
2887
2888 def magic_upgrade(self,arg):
2888 def magic_upgrade(self,arg):
2889 """ Upgrade your IPython installation
2889 """ Upgrade your IPython installation
2890
2890
2891 This will copy the config files that don't yet exist in your
2891 This will copy the config files that don't yet exist in your
2892 ipython dir from the system config dir. Use this after upgrading
2892 ipython dir from the system config dir. Use this after upgrading
2893 IPython if you don't wish to delete your .ipython dir.
2893 IPython if you don't wish to delete your .ipython dir.
2894
2894
2895 Call with -nolegacy to get rid of ipythonrc* files (recommended for
2895 Call with -nolegacy to get rid of ipythonrc* files (recommended for
2896 new users)
2896 new users)
2897
2897
2898 """
2898 """
2899 ip = self.getapi()
2899 ip = self.getapi()
2900 ipinstallation = path(IPython.__file__).dirname()
2900 ipinstallation = path(IPython.__file__).dirname()
2901 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py')
2901 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py')
2902 src_config = ipinstallation / 'UserConfig'
2902 src_config = ipinstallation / 'UserConfig'
2903 userdir = path(ip.options.ipythondir)
2903 userdir = path(ip.options.ipythondir)
2904 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
2904 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
2905 print ">",cmd
2905 print ">",cmd
2906 shell(cmd)
2906 shell(cmd)
2907 if arg == '-nolegacy':
2907 if arg == '-nolegacy':
2908 legacy = userdir.files('ipythonrc*')
2908 legacy = userdir.files('ipythonrc*')
2909 print "Nuking legacy files:",legacy
2909 print "Nuking legacy files:",legacy
2910
2910
2911 [p.remove() for p in legacy]
2911 [p.remove() for p in legacy]
2912 suffix = (sys.platform == 'win32' and '.ini' or '')
2912 suffix = (sys.platform == 'win32' and '.ini' or '')
2913 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
2913 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
2914
2914
2915
2916 def magic_doctest_mode(self,parameter_s=''):
2917 """Toggle doctest mode on and off.
2918
2919 This mode allows you to toggle the prompt behavior between normal
2920 IPython prompts and ones that are as similar to the default IPython
2921 interpreter as possible.
2922
2923 It also supports the pasting of code snippets that have leading '>>>'
2924 and '...' prompts in them. This means that you can paste doctests from
2925 files or docstrings (even if they have leading whitespace), and the
2926 code will execute correctly. You can then use '%history -tn' to see
2927 the translated history without line numbers; this will give you the
2928 input after removal of all the leading prompts and whitespace, which
2929 can be pasted back into an editor.
2930
2931 With these features, you can switch into this mode easily whenever you
2932 need to do testing and changes to doctests, without having to leave
2933 your existing IPython session.
2934 """
2935
2936 # XXX - Fix this to have cleaner activate/deactivate calls.
2937 from IPython.Extensions import InterpreterPasteInput as ipaste
2938 from IPython.ipstruct import Struct
2939
2940 # Shorthands
2941 shell = self.shell
2942 oc = shell.outputcache
2943 rc = shell.rc
2944 meta = shell.meta
2945 # dstore is a data store kept in the instance metadata bag to track any
2946 # changes we make, so we can undo them later.
2947 dstore = meta.setdefault('doctest_mode',Struct())
2948 save_dstore = dstore.setdefault
2949
2950 # save a few values we'll need to recover later
2951 mode = save_dstore('mode',False)
2952 save_dstore('rc_pprint',rc.pprint)
2953 save_dstore('xmode',shell.InteractiveTB.mode)
2954 save_dstore('rc_separate_in',rc.separate_in)
2955 save_dstore('rc_separate_out',rc.separate_out)
2956 save_dstore('rc_separate_out2',rc.separate_out2)
2957 save_dstore('rc_prompts_pad_left',rc.prompts_pad_left)
2958
2959 if mode == False:
2960 # turn on
2961 ipaste.activate_prefilter()
2962
2963 oc.prompt1.p_template = '>>> '
2964 oc.prompt2.p_template = '... '
2965 oc.prompt_out.p_template = ''
2966
2967 oc.prompt1.sep = ''
2968 oc.prompt_out.output_sep = ''
2969 oc.prompt_out.output_sep2 = '\n'
2970
2971 oc.prompt1.pad_left = oc.prompt2.pad_left = \
2972 oc.prompt_out.pad_left = False
2973
2974 shell.magic_xmode('Plain')
2975
2976 rc.pprint = False
2977
2978 else:
2979 # turn off
2980 ipaste.deactivate_prefilter()
2981
2982 oc.prompt1.p_template = rc.prompt_in1
2983 oc.prompt2.p_template = rc.prompt_in2
2984 oc.prompt_out.p_template = rc.prompt_out
2985
2986 oc.prompt1.sep = dstore.rc_separate_in
2987 oc.prompt_out.output_sep = dstore.rc_separate_out
2988 oc.prompt_out.output_sep2 = dstore.rc_separate_out2
2989
2990 oc.prompt1.pad_left = oc.prompt2.pad_left = \
2991 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
2992 shell.magic_xmode(dstore.xmode)
2993
2994 rc.pprint = dstore.rc_pprint
2995
2996 # Store new mode and inform
2997 dstore.mode = bool(1-int(mode))
2998 print 'Doctest mode is:',
2999 print ['OFF','ON'][dstore.mode]
3000
2915 # end Magic
3001 # end Magic
@@ -1,591 +1,604 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4
4
5 $Id: Prompts.py 2397 2007-05-26 10:06:26Z vivainio $"""
5 $Id: Prompts.py 2601 2007-08-10 07:01:29Z fperez $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
9 #
9 #
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #*****************************************************************************
12 #*****************************************************************************
13
13
14 from IPython import Release
14 from IPython import Release
15 __author__ = '%s <%s>' % Release.authors['Fernando']
15 __author__ = '%s <%s>' % Release.authors['Fernando']
16 __license__ = Release.license
16 __license__ = Release.license
17 __version__ = Release.version
17 __version__ = Release.version
18
18
19 #****************************************************************************
19 #****************************************************************************
20 # Required modules
20 # Required modules
21 import __builtin__
21 import __builtin__
22 import os
22 import os
23 import socket
23 import socket
24 import sys
24 import sys
25 import time
25 import time
26
26
27 # IPython's own
27 # IPython's own
28 from IPython import ColorANSI
28 from IPython import ColorANSI
29 from IPython.Itpl import ItplNS
29 from IPython.Itpl import ItplNS
30 from IPython.ipstruct import Struct
30 from IPython.ipstruct import Struct
31 from IPython.macro import Macro
31 from IPython.macro import Macro
32 from IPython.genutils import *
32 from IPython.genutils import *
33 from IPython.ipapi import TryNext
33 from IPython.ipapi import TryNext
34
34
35 #****************************************************************************
35 #****************************************************************************
36 #Color schemes for Prompts.
36 #Color schemes for Prompts.
37
37
38 PromptColors = ColorANSI.ColorSchemeTable()
38 PromptColors = ColorANSI.ColorSchemeTable()
39 InputColors = ColorANSI.InputTermColors # just a shorthand
39 InputColors = ColorANSI.InputTermColors # just a shorthand
40 Colors = ColorANSI.TermColors # just a shorthand
40 Colors = ColorANSI.TermColors # just a shorthand
41
41
42 PromptColors.add_scheme(ColorANSI.ColorScheme(
42 PromptColors.add_scheme(ColorANSI.ColorScheme(
43 'NoColor',
43 'NoColor',
44 in_prompt = InputColors.NoColor, # Input prompt
44 in_prompt = InputColors.NoColor, # Input prompt
45 in_number = InputColors.NoColor, # Input prompt number
45 in_number = InputColors.NoColor, # Input prompt number
46 in_prompt2 = InputColors.NoColor, # Continuation prompt
46 in_prompt2 = InputColors.NoColor, # Continuation prompt
47 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
47 in_normal = InputColors.NoColor, # color off (usu. Colors.Normal)
48
48
49 out_prompt = Colors.NoColor, # Output prompt
49 out_prompt = Colors.NoColor, # Output prompt
50 out_number = Colors.NoColor, # Output prompt number
50 out_number = Colors.NoColor, # Output prompt number
51
51
52 normal = Colors.NoColor # color off (usu. Colors.Normal)
52 normal = Colors.NoColor # color off (usu. Colors.Normal)
53 ))
53 ))
54
54
55 # make some schemes as instances so we can copy them for modification easily:
55 # make some schemes as instances so we can copy them for modification easily:
56 __PColLinux = ColorANSI.ColorScheme(
56 __PColLinux = ColorANSI.ColorScheme(
57 'Linux',
57 'Linux',
58 in_prompt = InputColors.Green,
58 in_prompt = InputColors.Green,
59 in_number = InputColors.LightGreen,
59 in_number = InputColors.LightGreen,
60 in_prompt2 = InputColors.Green,
60 in_prompt2 = InputColors.Green,
61 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
61 in_normal = InputColors.Normal, # color off (usu. Colors.Normal)
62
62
63 out_prompt = Colors.Red,
63 out_prompt = Colors.Red,
64 out_number = Colors.LightRed,
64 out_number = Colors.LightRed,
65
65
66 normal = Colors.Normal
66 normal = Colors.Normal
67 )
67 )
68 # Don't forget to enter it into the table!
68 # Don't forget to enter it into the table!
69 PromptColors.add_scheme(__PColLinux)
69 PromptColors.add_scheme(__PColLinux)
70
70
71 # Slightly modified Linux for light backgrounds
71 # Slightly modified Linux for light backgrounds
72 __PColLightBG = __PColLinux.copy('LightBG')
72 __PColLightBG = __PColLinux.copy('LightBG')
73
73
74 __PColLightBG.colors.update(
74 __PColLightBG.colors.update(
75 in_prompt = InputColors.Blue,
75 in_prompt = InputColors.Blue,
76 in_number = InputColors.LightBlue,
76 in_number = InputColors.LightBlue,
77 in_prompt2 = InputColors.Blue
77 in_prompt2 = InputColors.Blue
78 )
78 )
79 PromptColors.add_scheme(__PColLightBG)
79 PromptColors.add_scheme(__PColLightBG)
80
80
81 del Colors,InputColors
81 del Colors,InputColors
82
82
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84 def multiple_replace(dict, text):
84 def multiple_replace(dict, text):
85 """ Replace in 'text' all occurences of any key in the given
85 """ Replace in 'text' all occurences of any key in the given
86 dictionary by its corresponding value. Returns the new string."""
86 dictionary by its corresponding value. Returns the new string."""
87
87
88 # Function by Xavier Defrang, originally found at:
88 # Function by Xavier Defrang, originally found at:
89 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
89 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
90
90
91 # Create a regular expression from the dictionary keys
91 # Create a regular expression from the dictionary keys
92 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
92 regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
93 # For each match, look-up corresponding value in dictionary
93 # For each match, look-up corresponding value in dictionary
94 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
94 return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
95
95
96 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
97 # Special characters that can be used in prompt templates, mainly bash-like
97 # Special characters that can be used in prompt templates, mainly bash-like
98
98
99 # If $HOME isn't defined (Windows), make it an absurd string so that it can
99 # If $HOME isn't defined (Windows), make it an absurd string so that it can
100 # never be expanded out into '~'. Basically anything which can never be a
100 # never be expanded out into '~'. Basically anything which can never be a
101 # reasonable directory name will do, we just want the $HOME -> '~' operation
101 # reasonable directory name will do, we just want the $HOME -> '~' operation
102 # to become a no-op. We pre-compute $HOME here so it's not done on every
102 # to become a no-op. We pre-compute $HOME here so it's not done on every
103 # prompt call.
103 # prompt call.
104
104
105 # FIXME:
105 # FIXME:
106
106
107 # - This should be turned into a class which does proper namespace management,
107 # - This should be turned into a class which does proper namespace management,
108 # since the prompt specials need to be evaluated in a certain namespace.
108 # since the prompt specials need to be evaluated in a certain namespace.
109 # Currently it's just globals, which need to be managed manually by code
109 # Currently it's just globals, which need to be managed manually by code
110 # below.
110 # below.
111
111
112 # - I also need to split up the color schemes from the prompt specials
112 # - I also need to split up the color schemes from the prompt specials
113 # somehow. I don't have a clean design for that quite yet.
113 # somehow. I don't have a clean design for that quite yet.
114
114
115 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
115 HOME = os.environ.get("HOME","//////:::::ZZZZZ,,,~~~")
116
116
117 # We precompute a few more strings here for the prompt_specials, which are
117 # We precompute a few more strings here for the prompt_specials, which are
118 # fixed once ipython starts. This reduces the runtime overhead of computing
118 # fixed once ipython starts. This reduces the runtime overhead of computing
119 # prompt strings.
119 # prompt strings.
120 USER = os.environ.get("USER")
120 USER = os.environ.get("USER")
121 HOSTNAME = socket.gethostname()
121 HOSTNAME = socket.gethostname()
122 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
122 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
123 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
123 ROOT_SYMBOL = "$#"[os.name=='nt' or os.getuid()==0]
124
124
125 prompt_specials_color = {
125 prompt_specials_color = {
126 # Prompt/history count
126 # Prompt/history count
127 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
127 '%n' : '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
128 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
128 r'\#': '${self.col_num}' '${self.cache.prompt_count}' '${self.col_p}',
129 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
129 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
130 # can get numbers displayed in whatever color they want.
130 # can get numbers displayed in whatever color they want.
131 r'\N': '${self.cache.prompt_count}',
131 r'\N': '${self.cache.prompt_count}',
132 # Prompt/history count, with the actual digits replaced by dots. Used
132 # Prompt/history count, with the actual digits replaced by dots. Used
133 # mainly in continuation prompts (prompt_in2)
133 # mainly in continuation prompts (prompt_in2)
134 r'\D': '${"."*len(str(self.cache.prompt_count))}',
134 r'\D': '${"."*len(str(self.cache.prompt_count))}',
135 # Current working directory
135 # Current working directory
136 r'\w': '${os.getcwd()}',
136 r'\w': '${os.getcwd()}',
137 # Current time
137 # Current time
138 r'\t' : '${time.strftime("%H:%M:%S")}',
138 r'\t' : '${time.strftime("%H:%M:%S")}',
139 # Basename of current working directory.
139 # Basename of current working directory.
140 # (use os.sep to make this portable across OSes)
140 # (use os.sep to make this portable across OSes)
141 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
141 r'\W' : '${os.getcwd().split("%s")[-1]}' % os.sep,
142 # These X<N> are an extension to the normal bash prompts. They return
142 # These X<N> are an extension to the normal bash prompts. They return
143 # N terms of the path, after replacing $HOME with '~'
143 # N terms of the path, after replacing $HOME with '~'
144 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
144 r'\X0': '${os.getcwd().replace("%s","~")}' % HOME,
145 r'\X1': '${self.cwd_filt(1)}',
145 r'\X1': '${self.cwd_filt(1)}',
146 r'\X2': '${self.cwd_filt(2)}',
146 r'\X2': '${self.cwd_filt(2)}',
147 r'\X3': '${self.cwd_filt(3)}',
147 r'\X3': '${self.cwd_filt(3)}',
148 r'\X4': '${self.cwd_filt(4)}',
148 r'\X4': '${self.cwd_filt(4)}',
149 r'\X5': '${self.cwd_filt(5)}',
149 r'\X5': '${self.cwd_filt(5)}',
150 # Y<N> are similar to X<N>, but they show '~' if it's the directory
150 # Y<N> are similar to X<N>, but they show '~' if it's the directory
151 # N+1 in the list. Somewhat like %cN in tcsh.
151 # N+1 in the list. Somewhat like %cN in tcsh.
152 r'\Y0': '${self.cwd_filt2(0)}',
152 r'\Y0': '${self.cwd_filt2(0)}',
153 r'\Y1': '${self.cwd_filt2(1)}',
153 r'\Y1': '${self.cwd_filt2(1)}',
154 r'\Y2': '${self.cwd_filt2(2)}',
154 r'\Y2': '${self.cwd_filt2(2)}',
155 r'\Y3': '${self.cwd_filt2(3)}',
155 r'\Y3': '${self.cwd_filt2(3)}',
156 r'\Y4': '${self.cwd_filt2(4)}',
156 r'\Y4': '${self.cwd_filt2(4)}',
157 r'\Y5': '${self.cwd_filt2(5)}',
157 r'\Y5': '${self.cwd_filt2(5)}',
158 # Hostname up to first .
158 # Hostname up to first .
159 r'\h': HOSTNAME_SHORT,
159 r'\h': HOSTNAME_SHORT,
160 # Full hostname
160 # Full hostname
161 r'\H': HOSTNAME,
161 r'\H': HOSTNAME,
162 # Username of current user
162 # Username of current user
163 r'\u': USER,
163 r'\u': USER,
164 # Escaped '\'
164 # Escaped '\'
165 '\\\\': '\\',
165 '\\\\': '\\',
166 # Newline
166 # Newline
167 r'\n': '\n',
167 r'\n': '\n',
168 # Carriage return
168 # Carriage return
169 r'\r': '\r',
169 r'\r': '\r',
170 # Release version
170 # Release version
171 r'\v': __version__,
171 r'\v': __version__,
172 # Root symbol ($ or #)
172 # Root symbol ($ or #)
173 r'\$': ROOT_SYMBOL,
173 r'\$': ROOT_SYMBOL,
174 }
174 }
175
175
176 # A copy of the prompt_specials dictionary but with all color escapes removed,
176 # A copy of the prompt_specials dictionary but with all color escapes removed,
177 # so we can correctly compute the prompt length for the auto_rewrite method.
177 # so we can correctly compute the prompt length for the auto_rewrite method.
178 prompt_specials_nocolor = prompt_specials_color.copy()
178 prompt_specials_nocolor = prompt_specials_color.copy()
179 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
179 prompt_specials_nocolor['%n'] = '${self.cache.prompt_count}'
180 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
180 prompt_specials_nocolor[r'\#'] = '${self.cache.prompt_count}'
181
181
182 # Add in all the InputTermColors color escapes as valid prompt characters.
182 # Add in all the InputTermColors color escapes as valid prompt characters.
183 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
183 # They all get added as \\C_COLORNAME, so that we don't have any conflicts
184 # with a color name which may begin with a letter used by any other of the
184 # with a color name which may begin with a letter used by any other of the
185 # allowed specials. This of course means that \\C will never be allowed for
185 # allowed specials. This of course means that \\C will never be allowed for
186 # anything else.
186 # anything else.
187 input_colors = ColorANSI.InputTermColors
187 input_colors = ColorANSI.InputTermColors
188 for _color in dir(input_colors):
188 for _color in dir(input_colors):
189 if _color[0] != '_':
189 if _color[0] != '_':
190 c_name = r'\C_'+_color
190 c_name = r'\C_'+_color
191 prompt_specials_color[c_name] = getattr(input_colors,_color)
191 prompt_specials_color[c_name] = getattr(input_colors,_color)
192 prompt_specials_nocolor[c_name] = ''
192 prompt_specials_nocolor[c_name] = ''
193
193
194 # we default to no color for safety. Note that prompt_specials is a global
194 # we default to no color for safety. Note that prompt_specials is a global
195 # variable used by all prompt objects.
195 # variable used by all prompt objects.
196 prompt_specials = prompt_specials_nocolor
196 prompt_specials = prompt_specials_nocolor
197
197
198 #-----------------------------------------------------------------------------
198 #-----------------------------------------------------------------------------
199 def str_safe(arg):
199 def str_safe(arg):
200 """Convert to a string, without ever raising an exception.
200 """Convert to a string, without ever raising an exception.
201
201
202 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
202 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
203 error message."""
203 error message."""
204
204
205 try:
205 try:
206 out = str(arg)
206 out = str(arg)
207 except UnicodeError:
207 except UnicodeError:
208 try:
208 try:
209 out = arg.encode('utf_8','replace')
209 out = arg.encode('utf_8','replace')
210 except Exception,msg:
210 except Exception,msg:
211 # let's keep this little duplication here, so that the most common
211 # let's keep this little duplication here, so that the most common
212 # case doesn't suffer from a double try wrapping.
212 # case doesn't suffer from a double try wrapping.
213 out = '<ERROR: %s>' % msg
213 out = '<ERROR: %s>' % msg
214 except Exception,msg:
214 except Exception,msg:
215 out = '<ERROR: %s>' % msg
215 out = '<ERROR: %s>' % msg
216 return out
216 return out
217
217
218 class BasePrompt:
218 class BasePrompt(object):
219 """Interactive prompt similar to Mathematica's."""
219 """Interactive prompt similar to Mathematica's."""
220
221 def _get_p_template(self):
222 return self._p_template
223
224 def _set_p_template(self,val):
225 self._p_template = val
226 self.set_p_str()
227
228 p_template = property(_get_p_template,_set_p_template,
229 doc='Template for prompt string creation')
230
220 def __init__(self,cache,sep,prompt,pad_left=False):
231 def __init__(self,cache,sep,prompt,pad_left=False):
221
232
222 # Hack: we access information about the primary prompt through the
233 # Hack: we access information about the primary prompt through the
223 # cache argument. We need this, because we want the secondary prompt
234 # cache argument. We need this, because we want the secondary prompt
224 # to be aligned with the primary one. Color table info is also shared
235 # to be aligned with the primary one. Color table info is also shared
225 # by all prompt classes through the cache. Nice OO spaghetti code!
236 # by all prompt classes through the cache. Nice OO spaghetti code!
226 self.cache = cache
237 self.cache = cache
227 self.sep = sep
238 self.sep = sep
228
239
229 # regexp to count the number of spaces at the end of a prompt
240 # regexp to count the number of spaces at the end of a prompt
230 # expression, useful for prompt auto-rewriting
241 # expression, useful for prompt auto-rewriting
231 self.rspace = re.compile(r'(\s*)$')
242 self.rspace = re.compile(r'(\s*)$')
232 # Flag to left-pad prompt strings to match the length of the primary
243 # Flag to left-pad prompt strings to match the length of the primary
233 # prompt
244 # prompt
234 self.pad_left = pad_left
245 self.pad_left = pad_left
235 # Set template to create each actual prompt (where numbers change)
246
247 # Set template to create each actual prompt (where numbers change).
248 # Use a property
236 self.p_template = prompt
249 self.p_template = prompt
237 self.set_p_str()
250 self.set_p_str()
238
251
239 def set_p_str(self):
252 def set_p_str(self):
240 """ Set the interpolating prompt strings.
253 """ Set the interpolating prompt strings.
241
254
242 This must be called every time the color settings change, because the
255 This must be called every time the color settings change, because the
243 prompt_specials global may have changed."""
256 prompt_specials global may have changed."""
244
257
245 import os,time # needed in locals for prompt string handling
258 import os,time # needed in locals for prompt string handling
246 loc = locals()
259 loc = locals()
247 self.p_str = ItplNS('%s%s%s' %
260 self.p_str = ItplNS('%s%s%s' %
248 ('${self.sep}${self.col_p}',
261 ('${self.sep}${self.col_p}',
249 multiple_replace(prompt_specials, self.p_template),
262 multiple_replace(prompt_specials, self.p_template),
250 '${self.col_norm}'),self.cache.user_ns,loc)
263 '${self.col_norm}'),self.cache.user_ns,loc)
251
264
252 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
265 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
253 self.p_template),
266 self.p_template),
254 self.cache.user_ns,loc)
267 self.cache.user_ns,loc)
255
268
256 def write(self,msg): # dbg
269 def write(self,msg): # dbg
257 sys.stdout.write(msg)
270 sys.stdout.write(msg)
258 return ''
271 return ''
259
272
260 def __str__(self):
273 def __str__(self):
261 """Return a string form of the prompt.
274 """Return a string form of the prompt.
262
275
263 This for is useful for continuation and output prompts, since it is
276 This for is useful for continuation and output prompts, since it is
264 left-padded to match lengths with the primary one (if the
277 left-padded to match lengths with the primary one (if the
265 self.pad_left attribute is set)."""
278 self.pad_left attribute is set)."""
266
279
267 out_str = str_safe(self.p_str)
280 out_str = str_safe(self.p_str)
268 if self.pad_left:
281 if self.pad_left:
269 # We must find the amount of padding required to match lengths,
282 # We must find the amount of padding required to match lengths,
270 # taking the color escapes (which are invisible on-screen) into
283 # taking the color escapes (which are invisible on-screen) into
271 # account.
284 # account.
272 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
285 esc_pad = len(out_str) - len(str_safe(self.p_str_nocolor))
273 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
286 format = '%%%ss' % (len(str(self.cache.last_prompt))+esc_pad)
274 return format % out_str
287 return format % out_str
275 else:
288 else:
276 return out_str
289 return out_str
277
290
278 # these path filters are put in as methods so that we can control the
291 # these path filters are put in as methods so that we can control the
279 # namespace where the prompt strings get evaluated
292 # namespace where the prompt strings get evaluated
280 def cwd_filt(self,depth):
293 def cwd_filt(self,depth):
281 """Return the last depth elements of the current working directory.
294 """Return the last depth elements of the current working directory.
282
295
283 $HOME is always replaced with '~'.
296 $HOME is always replaced with '~'.
284 If depth==0, the full path is returned."""
297 If depth==0, the full path is returned."""
285
298
286 cwd = os.getcwd().replace(HOME,"~")
299 cwd = os.getcwd().replace(HOME,"~")
287 out = os.sep.join(cwd.split(os.sep)[-depth:])
300 out = os.sep.join(cwd.split(os.sep)[-depth:])
288 if out:
301 if out:
289 return out
302 return out
290 else:
303 else:
291 return os.sep
304 return os.sep
292
305
293 def cwd_filt2(self,depth):
306 def cwd_filt2(self,depth):
294 """Return the last depth elements of the current working directory.
307 """Return the last depth elements of the current working directory.
295
308
296 $HOME is always replaced with '~'.
309 $HOME is always replaced with '~'.
297 If depth==0, the full path is returned."""
310 If depth==0, the full path is returned."""
298
311
299 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
312 cwd = os.getcwd().replace(HOME,"~").split(os.sep)
300 if '~' in cwd and len(cwd) == depth+1:
313 if '~' in cwd and len(cwd) == depth+1:
301 depth += 1
314 depth += 1
302 out = os.sep.join(cwd[-depth:])
315 out = os.sep.join(cwd[-depth:])
303 if out:
316 if out:
304 return out
317 return out
305 else:
318 else:
306 return os.sep
319 return os.sep
307
320
308 class Prompt1(BasePrompt):
321 class Prompt1(BasePrompt):
309 """Input interactive prompt similar to Mathematica's."""
322 """Input interactive prompt similar to Mathematica's."""
310
323
311 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
324 def __init__(self,cache,sep='\n',prompt='In [\\#]: ',pad_left=True):
312 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
325 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
313
326
314 def set_colors(self):
327 def set_colors(self):
315 self.set_p_str()
328 self.set_p_str()
316 Colors = self.cache.color_table.active_colors # shorthand
329 Colors = self.cache.color_table.active_colors # shorthand
317 self.col_p = Colors.in_prompt
330 self.col_p = Colors.in_prompt
318 self.col_num = Colors.in_number
331 self.col_num = Colors.in_number
319 self.col_norm = Colors.in_normal
332 self.col_norm = Colors.in_normal
320 # We need a non-input version of these escapes for the '--->'
333 # We need a non-input version of these escapes for the '--->'
321 # auto-call prompts used in the auto_rewrite() method.
334 # auto-call prompts used in the auto_rewrite() method.
322 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
335 self.col_p_ni = self.col_p.replace('\001','').replace('\002','')
323 self.col_norm_ni = Colors.normal
336 self.col_norm_ni = Colors.normal
324
337
325 def __str__(self):
338 def __str__(self):
326 self.cache.prompt_count += 1
339 self.cache.prompt_count += 1
327 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
340 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1]
328 return str_safe(self.p_str)
341 return str_safe(self.p_str)
329
342
330 def auto_rewrite(self):
343 def auto_rewrite(self):
331 """Print a string of the form '--->' which lines up with the previous
344 """Print a string of the form '--->' which lines up with the previous
332 input string. Useful for systems which re-write the user input when
345 input string. Useful for systems which re-write the user input when
333 handling automatically special syntaxes."""
346 handling automatically special syntaxes."""
334
347
335 curr = str(self.cache.last_prompt)
348 curr = str(self.cache.last_prompt)
336 nrspaces = len(self.rspace.search(curr).group())
349 nrspaces = len(self.rspace.search(curr).group())
337 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
350 return '%s%s>%s%s' % (self.col_p_ni,'-'*(len(curr)-nrspaces-1),
338 ' '*nrspaces,self.col_norm_ni)
351 ' '*nrspaces,self.col_norm_ni)
339
352
340 class PromptOut(BasePrompt):
353 class PromptOut(BasePrompt):
341 """Output interactive prompt similar to Mathematica's."""
354 """Output interactive prompt similar to Mathematica's."""
342
355
343 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
356 def __init__(self,cache,sep='',prompt='Out[\\#]: ',pad_left=True):
344 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
357 BasePrompt.__init__(self,cache,sep,prompt,pad_left)
345 if not self.p_template:
358 if not self.p_template:
346 self.__str__ = lambda: ''
359 self.__str__ = lambda: ''
347
360
348 def set_colors(self):
361 def set_colors(self):
349 self.set_p_str()
362 self.set_p_str()
350 Colors = self.cache.color_table.active_colors # shorthand
363 Colors = self.cache.color_table.active_colors # shorthand
351 self.col_p = Colors.out_prompt
364 self.col_p = Colors.out_prompt
352 self.col_num = Colors.out_number
365 self.col_num = Colors.out_number
353 self.col_norm = Colors.normal
366 self.col_norm = Colors.normal
354
367
355 class Prompt2(BasePrompt):
368 class Prompt2(BasePrompt):
356 """Interactive continuation prompt."""
369 """Interactive continuation prompt."""
357
370
358 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
371 def __init__(self,cache,prompt=' .\\D.: ',pad_left=True):
359 self.cache = cache
372 self.cache = cache
360 self.p_template = prompt
373 self.p_template = prompt
361 self.pad_left = pad_left
374 self.pad_left = pad_left
362 self.set_p_str()
375 self.set_p_str()
363
376
364 def set_p_str(self):
377 def set_p_str(self):
365 import os,time # needed in locals for prompt string handling
378 import os,time # needed in locals for prompt string handling
366 loc = locals()
379 loc = locals()
367 self.p_str = ItplNS('%s%s%s' %
380 self.p_str = ItplNS('%s%s%s' %
368 ('${self.col_p2}',
381 ('${self.col_p2}',
369 multiple_replace(prompt_specials, self.p_template),
382 multiple_replace(prompt_specials, self.p_template),
370 '$self.col_norm'),
383 '$self.col_norm'),
371 self.cache.user_ns,loc)
384 self.cache.user_ns,loc)
372 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
385 self.p_str_nocolor = ItplNS(multiple_replace(prompt_specials_nocolor,
373 self.p_template),
386 self.p_template),
374 self.cache.user_ns,loc)
387 self.cache.user_ns,loc)
375
388
376 def set_colors(self):
389 def set_colors(self):
377 self.set_p_str()
390 self.set_p_str()
378 Colors = self.cache.color_table.active_colors
391 Colors = self.cache.color_table.active_colors
379 self.col_p2 = Colors.in_prompt2
392 self.col_p2 = Colors.in_prompt2
380 self.col_norm = Colors.in_normal
393 self.col_norm = Colors.in_normal
381 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
394 # FIXME (2004-06-16) HACK: prevent crashes for users who haven't
382 # updated their prompt_in2 definitions. Remove eventually.
395 # updated their prompt_in2 definitions. Remove eventually.
383 self.col_p = Colors.out_prompt
396 self.col_p = Colors.out_prompt
384 self.col_num = Colors.out_number
397 self.col_num = Colors.out_number
385
398
386
399
387 #-----------------------------------------------------------------------------
400 #-----------------------------------------------------------------------------
388 class CachedOutput:
401 class CachedOutput:
389 """Class for printing output from calculations while keeping a cache of
402 """Class for printing output from calculations while keeping a cache of
390 reults. It dynamically creates global variables prefixed with _ which
403 reults. It dynamically creates global variables prefixed with _ which
391 contain these results.
404 contain these results.
392
405
393 Meant to be used as a sys.displayhook replacement, providing numbered
406 Meant to be used as a sys.displayhook replacement, providing numbered
394 prompts and cache services.
407 prompts and cache services.
395
408
396 Initialize with initial and final values for cache counter (this defines
409 Initialize with initial and final values for cache counter (this defines
397 the maximum size of the cache."""
410 the maximum size of the cache."""
398
411
399 def __init__(self,shell,cache_size,Pprint,
412 def __init__(self,shell,cache_size,Pprint,
400 colors='NoColor',input_sep='\n',
413 colors='NoColor',input_sep='\n',
401 output_sep='\n',output_sep2='',
414 output_sep='\n',output_sep2='',
402 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
415 ps1 = None, ps2 = None,ps_out = None,pad_left=True):
403
416
404 cache_size_min = 3
417 cache_size_min = 3
405 if cache_size <= 0:
418 if cache_size <= 0:
406 self.do_full_cache = 0
419 self.do_full_cache = 0
407 cache_size = 0
420 cache_size = 0
408 elif cache_size < cache_size_min:
421 elif cache_size < cache_size_min:
409 self.do_full_cache = 0
422 self.do_full_cache = 0
410 cache_size = 0
423 cache_size = 0
411 warn('caching was disabled (min value for cache size is %s).' %
424 warn('caching was disabled (min value for cache size is %s).' %
412 cache_size_min,level=3)
425 cache_size_min,level=3)
413 else:
426 else:
414 self.do_full_cache = 1
427 self.do_full_cache = 1
415
428
416 self.cache_size = cache_size
429 self.cache_size = cache_size
417 self.input_sep = input_sep
430 self.input_sep = input_sep
418
431
419 # we need a reference to the user-level namespace
432 # we need a reference to the user-level namespace
420 self.shell = shell
433 self.shell = shell
421 self.user_ns = shell.user_ns
434 self.user_ns = shell.user_ns
422 # and to the user's input
435 # and to the user's input
423 self.input_hist = shell.input_hist
436 self.input_hist = shell.input_hist
424 # and to the user's logger, for logging output
437 # and to the user's logger, for logging output
425 self.logger = shell.logger
438 self.logger = shell.logger
426
439
427 # Set input prompt strings and colors
440 # Set input prompt strings and colors
428 if cache_size == 0:
441 if cache_size == 0:
429 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
442 if ps1.find('%n') > -1 or ps1.find(r'\#') > -1 \
430 or ps1.find(r'\N') > -1:
443 or ps1.find(r'\N') > -1:
431 ps1 = '>>> '
444 ps1 = '>>> '
432 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
445 if ps2.find('%n') > -1 or ps2.find(r'\#') > -1 \
433 or ps2.find(r'\N') > -1:
446 or ps2.find(r'\N') > -1:
434 ps2 = '... '
447 ps2 = '... '
435 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
448 self.ps1_str = self._set_prompt_str(ps1,'In [\\#]: ','>>> ')
436 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
449 self.ps2_str = self._set_prompt_str(ps2,' .\\D.: ','... ')
437 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
450 self.ps_out_str = self._set_prompt_str(ps_out,'Out[\\#]: ','')
438
451
439 self.color_table = PromptColors
452 self.color_table = PromptColors
440 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
453 self.prompt1 = Prompt1(self,sep=input_sep,prompt=self.ps1_str,
441 pad_left=pad_left)
454 pad_left=pad_left)
442 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
455 self.prompt2 = Prompt2(self,prompt=self.ps2_str,pad_left=pad_left)
443 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
456 self.prompt_out = PromptOut(self,sep='',prompt=self.ps_out_str,
444 pad_left=pad_left)
457 pad_left=pad_left)
445 self.set_colors(colors)
458 self.set_colors(colors)
446
459
447 # other more normal stuff
460 # other more normal stuff
448 # b/c each call to the In[] prompt raises it by 1, even the first.
461 # b/c each call to the In[] prompt raises it by 1, even the first.
449 self.prompt_count = 0
462 self.prompt_count = 0
450 # Store the last prompt string each time, we need it for aligning
463 # Store the last prompt string each time, we need it for aligning
451 # continuation and auto-rewrite prompts
464 # continuation and auto-rewrite prompts
452 self.last_prompt = ''
465 self.last_prompt = ''
453 self.Pprint = Pprint
466 self.Pprint = Pprint
454 self.output_sep = output_sep
467 self.output_sep = output_sep
455 self.output_sep2 = output_sep2
468 self.output_sep2 = output_sep2
456 self._,self.__,self.___ = '','',''
469 self._,self.__,self.___ = '','',''
457 self.pprint_types = map(type,[(),[],{}])
470 self.pprint_types = map(type,[(),[],{}])
458
471
459 # these are deliberately global:
472 # these are deliberately global:
460 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
473 to_user_ns = {'_':self._,'__':self.__,'___':self.___}
461 self.user_ns.update(to_user_ns)
474 self.user_ns.update(to_user_ns)
462
475
463 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
476 def _set_prompt_str(self,p_str,cache_def,no_cache_def):
464 if p_str is None:
477 if p_str is None:
465 if self.do_full_cache:
478 if self.do_full_cache:
466 return cache_def
479 return cache_def
467 else:
480 else:
468 return no_cache_def
481 return no_cache_def
469 else:
482 else:
470 return p_str
483 return p_str
471
484
472 def set_colors(self,colors):
485 def set_colors(self,colors):
473 """Set the active color scheme and configure colors for the three
486 """Set the active color scheme and configure colors for the three
474 prompt subsystems."""
487 prompt subsystems."""
475
488
476 # FIXME: the prompt_specials global should be gobbled inside this
489 # FIXME: the prompt_specials global should be gobbled inside this
477 # class instead. Do it when cleaning up the whole 3-prompt system.
490 # class instead. Do it when cleaning up the whole 3-prompt system.
478 global prompt_specials
491 global prompt_specials
479 if colors.lower()=='nocolor':
492 if colors.lower()=='nocolor':
480 prompt_specials = prompt_specials_nocolor
493 prompt_specials = prompt_specials_nocolor
481 else:
494 else:
482 prompt_specials = prompt_specials_color
495 prompt_specials = prompt_specials_color
483
496
484 self.color_table.set_active_scheme(colors)
497 self.color_table.set_active_scheme(colors)
485 self.prompt1.set_colors()
498 self.prompt1.set_colors()
486 self.prompt2.set_colors()
499 self.prompt2.set_colors()
487 self.prompt_out.set_colors()
500 self.prompt_out.set_colors()
488
501
489 def __call__(self,arg=None):
502 def __call__(self,arg=None):
490 """Printing with history cache management.
503 """Printing with history cache management.
491
504
492 This is invoked everytime the interpreter needs to print, and is
505 This is invoked everytime the interpreter needs to print, and is
493 activated by setting the variable sys.displayhook to it."""
506 activated by setting the variable sys.displayhook to it."""
494
507
495 # If something injected a '_' variable in __builtin__, delete
508 # If something injected a '_' variable in __builtin__, delete
496 # ipython's automatic one so we don't clobber that. gettext() in
509 # ipython's automatic one so we don't clobber that. gettext() in
497 # particular uses _, so we need to stay away from it.
510 # particular uses _, so we need to stay away from it.
498 if '_' in __builtin__.__dict__:
511 if '_' in __builtin__.__dict__:
499 try:
512 try:
500 del self.user_ns['_']
513 del self.user_ns['_']
501 except KeyError:
514 except KeyError:
502 pass
515 pass
503 if arg is not None:
516 if arg is not None:
504 cout_write = Term.cout.write # fast lookup
517 cout_write = Term.cout.write # fast lookup
505 # first handle the cache and counters
518 # first handle the cache and counters
506
519
507 # do not print output if input ends in ';'
520 # do not print output if input ends in ';'
508 if self.input_hist[self.prompt_count].endswith(';\n'):
521 if self.input_hist[self.prompt_count].endswith(';\n'):
509 return
522 return
510 # don't use print, puts an extra space
523 # don't use print, puts an extra space
511 cout_write(self.output_sep)
524 cout_write(self.output_sep)
512 outprompt = self.shell.hooks.generate_output_prompt()
525 outprompt = self.shell.hooks.generate_output_prompt()
513 if self.do_full_cache:
526 if self.do_full_cache:
514 cout_write(outprompt)
527 cout_write(outprompt)
515
528
516 # and now call a possibly user-defined print mechanism
529 # and now call a possibly user-defined print mechanism
517 manipulated_val = self.display(arg)
530 manipulated_val = self.display(arg)
518
531
519 # user display hooks can change the variable to be stored in
532 # user display hooks can change the variable to be stored in
520 # output history
533 # output history
521
534
522 if manipulated_val is not None:
535 if manipulated_val is not None:
523 arg = manipulated_val
536 arg = manipulated_val
524
537
525 # avoid recursive reference when displaying _oh/Out
538 # avoid recursive reference when displaying _oh/Out
526 if arg is not self.user_ns['_oh']:
539 if arg is not self.user_ns['_oh']:
527 self.update(arg)
540 self.update(arg)
528
541
529 if self.logger.log_output:
542 if self.logger.log_output:
530 self.logger.log_write(repr(arg),'output')
543 self.logger.log_write(repr(arg),'output')
531 cout_write(self.output_sep2)
544 cout_write(self.output_sep2)
532 Term.cout.flush()
545 Term.cout.flush()
533
546
534 def _display(self,arg):
547 def _display(self,arg):
535 """Default printer method, uses pprint.
548 """Default printer method, uses pprint.
536
549
537 Do ip.set_hook("result_display", my_displayhook) for custom result
550 Do ip.set_hook("result_display", my_displayhook) for custom result
538 display, e.g. when your own objects need special formatting.
551 display, e.g. when your own objects need special formatting.
539 """
552 """
540 try:
553 try:
541 return IPython.generics.result_display(arg)
554 return IPython.generics.result_display(arg)
542 except TryNext:
555 except TryNext:
543 return self.shell.hooks.result_display(arg)
556 return self.shell.hooks.result_display(arg)
544
557
545 # Assign the default display method:
558 # Assign the default display method:
546 display = _display
559 display = _display
547
560
548 def update(self,arg):
561 def update(self,arg):
549 #print '***cache_count', self.cache_count # dbg
562 #print '***cache_count', self.cache_count # dbg
550 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
563 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
551 warn('Output cache limit (currently '+
564 warn('Output cache limit (currently '+
552 `self.cache_size`+' entries) hit.\n'
565 `self.cache_size`+' entries) hit.\n'
553 'Flushing cache and resetting history counter...\n'
566 'Flushing cache and resetting history counter...\n'
554 'The only history variables available will be _,__,___ and _1\n'
567 'The only history variables available will be _,__,___ and _1\n'
555 'with the current result.')
568 'with the current result.')
556
569
557 self.flush()
570 self.flush()
558 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
571 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
559 # we cause buggy behavior for things like gettext).
572 # we cause buggy behavior for things like gettext).
560 if '_' not in __builtin__.__dict__:
573 if '_' not in __builtin__.__dict__:
561 self.___ = self.__
574 self.___ = self.__
562 self.__ = self._
575 self.__ = self._
563 self._ = arg
576 self._ = arg
564 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
577 self.user_ns.update({'_':self._,'__':self.__,'___':self.___})
565
578
566 # hackish access to top-level namespace to create _1,_2... dynamically
579 # hackish access to top-level namespace to create _1,_2... dynamically
567 to_main = {}
580 to_main = {}
568 if self.do_full_cache:
581 if self.do_full_cache:
569 new_result = '_'+`self.prompt_count`
582 new_result = '_'+`self.prompt_count`
570 to_main[new_result] = arg
583 to_main[new_result] = arg
571 self.user_ns.update(to_main)
584 self.user_ns.update(to_main)
572 self.user_ns['_oh'][self.prompt_count] = arg
585 self.user_ns['_oh'][self.prompt_count] = arg
573
586
574 def flush(self):
587 def flush(self):
575 if not self.do_full_cache:
588 if not self.do_full_cache:
576 raise ValueError,"You shouldn't have reached the cache flush "\
589 raise ValueError,"You shouldn't have reached the cache flush "\
577 "if full caching is not enabled!"
590 "if full caching is not enabled!"
578 # delete auto-generated vars from global namespace
591 # delete auto-generated vars from global namespace
579
592
580 for n in range(1,self.prompt_count + 1):
593 for n in range(1,self.prompt_count + 1):
581 key = '_'+`n`
594 key = '_'+`n`
582 try:
595 try:
583 del self.user_ns[key]
596 del self.user_ns[key]
584 except: pass
597 except: pass
585 self.user_ns['_oh'].clear()
598 self.user_ns['_oh'].clear()
586
599
587 if '_' not in __builtin__.__dict__:
600 if '_' not in __builtin__.__dict__:
588 self.user_ns.update({'_':None,'__':None, '___':None})
601 self.user_ns.update({'_':None,'__':None, '___':None})
589 import gc
602 import gc
590 gc.collect() # xxx needed?
603 gc.collect() # xxx needed?
591
604
@@ -1,463 +1,484 b''
1 ''' IPython customization API
1 ''' IPython customization API
2
2
3 Your one-stop module for configuring & extending ipython
3 Your one-stop module for configuring & extending ipython
4
4
5 The API will probably break when ipython 1.0 is released, but so
5 The API will probably break when ipython 1.0 is released, but so
6 will the other configuration method (rc files).
6 will the other configuration method (rc files).
7
7
8 All names prefixed by underscores are for internal use, not part
8 All names prefixed by underscores are for internal use, not part
9 of the public api.
9 of the public api.
10
10
11 Below is an example that you can just put to a module and import from ipython.
11 Below is an example that you can just put to a module and import from ipython.
12
12
13 A good practice is to install the config script below as e.g.
13 A good practice is to install the config script below as e.g.
14
14
15 ~/.ipython/my_private_conf.py
15 ~/.ipython/my_private_conf.py
16
16
17 And do
17 And do
18
18
19 import_mod my_private_conf
19 import_mod my_private_conf
20
20
21 in ~/.ipython/ipythonrc
21 in ~/.ipython/ipythonrc
22
22
23 That way the module is imported at startup and you can have all your
23 That way the module is imported at startup and you can have all your
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
24 personal configuration (as opposed to boilerplate ipythonrc-PROFILENAME
25 stuff) in there.
25 stuff) in there.
26
26
27 -----------------------------------------------
27 -----------------------------------------------
28 import IPython.ipapi
28 import IPython.ipapi
29 ip = IPython.ipapi.get()
29 ip = IPython.ipapi.get()
30
30
31 def ankka_f(self, arg):
31 def ankka_f(self, arg):
32 print "Ankka",self,"says uppercase:",arg.upper()
32 print "Ankka",self,"says uppercase:",arg.upper()
33
33
34 ip.expose_magic("ankka",ankka_f)
34 ip.expose_magic("ankka",ankka_f)
35
35
36 ip.magic('alias sayhi echo "Testing, hi ok"')
36 ip.magic('alias sayhi echo "Testing, hi ok"')
37 ip.magic('alias helloworld echo "Hello world"')
37 ip.magic('alias helloworld echo "Hello world"')
38 ip.system('pwd')
38 ip.system('pwd')
39
39
40 ip.ex('import re')
40 ip.ex('import re')
41 ip.ex("""
41 ip.ex("""
42 def funcci(a,b):
42 def funcci(a,b):
43 print a+b
43 print a+b
44 print funcci(3,4)
44 print funcci(3,4)
45 """)
45 """)
46 ip.ex("funcci(348,9)")
46 ip.ex("funcci(348,9)")
47
47
48 def jed_editor(self,filename, linenum=None):
48 def jed_editor(self,filename, linenum=None):
49 print "Calling my own editor, jed ... via hook!"
49 print "Calling my own editor, jed ... via hook!"
50 import os
50 import os
51 if linenum is None: linenum = 0
51 if linenum is None: linenum = 0
52 os.system('jed +%d %s' % (linenum, filename))
52 os.system('jed +%d %s' % (linenum, filename))
53 print "exiting jed"
53 print "exiting jed"
54
54
55 ip.set_hook('editor',jed_editor)
55 ip.set_hook('editor',jed_editor)
56
56
57 o = ip.options
57 o = ip.options
58 o.autocall = 2 # FULL autocall mode
58 o.autocall = 2 # FULL autocall mode
59
59
60 print "done!"
60 print "done!"
61 '''
61 '''
62
62
63 # stdlib imports
63 # stdlib imports
64 import __builtin__
64 import __builtin__
65 import sys
65 import sys
66
66
67 # our own
67 # our own
68 #from IPython.genutils import warn,error
68 #from IPython.genutils import warn,error
69
69
70 class TryNext(Exception):
70 class TryNext(Exception):
71 """Try next hook exception.
71 """Try next hook exception.
72
72
73 Raise this in your hook function to indicate that the next hook handler
73 Raise this in your hook function to indicate that the next hook handler
74 should be used to handle the operation. If you pass arguments to the
74 should be used to handle the operation. If you pass arguments to the
75 constructor those arguments will be used by the next hook instead of the
75 constructor those arguments will be used by the next hook instead of the
76 original ones.
76 original ones.
77 """
77 """
78
78
79 def __init__(self, *args, **kwargs):
79 def __init__(self, *args, **kwargs):
80 self.args = args
80 self.args = args
81 self.kwargs = kwargs
81 self.kwargs = kwargs
82
82
83 class IPyAutocall:
83 class IPyAutocall:
84 """ Instances of this class are always autocalled
84 """ Instances of this class are always autocalled
85
85
86 This happens regardless of 'autocall' variable state. Use this to
86 This happens regardless of 'autocall' variable state. Use this to
87 develop macro-like mechanisms.
87 develop macro-like mechanisms.
88 """
88 """
89
89
90 def set_ip(self,ip):
90 def set_ip(self,ip):
91 """ Will be used to set _ip point to current ipython instance b/f call
91 """ Will be used to set _ip point to current ipython instance b/f call
92
92
93 Override this method if you don't want this to happen.
93 Override this method if you don't want this to happen.
94
94
95 """
95 """
96 self._ip = ip
96 self._ip = ip
97
97
98
98
99 # contains the most recently instantiated IPApi
99 # contains the most recently instantiated IPApi
100
100
101 class IPythonNotRunning:
101 class IPythonNotRunning:
102 """Dummy do-nothing class.
102 """Dummy do-nothing class.
103
103
104 Instances of this class return a dummy attribute on all accesses, which
104 Instances of this class return a dummy attribute on all accesses, which
105 can be called and warns. This makes it easier to write scripts which use
105 can be called and warns. This makes it easier to write scripts which use
106 the ipapi.get() object for informational purposes to operate both with and
106 the ipapi.get() object for informational purposes to operate both with and
107 without ipython. Obviously code which uses the ipython object for
107 without ipython. Obviously code which uses the ipython object for
108 computations will not work, but this allows a wider range of code to
108 computations will not work, but this allows a wider range of code to
109 transparently work whether ipython is being used or not."""
109 transparently work whether ipython is being used or not."""
110
110
111 def __init__(self,warn=True):
111 def __init__(self,warn=True):
112 if warn:
112 if warn:
113 self.dummy = self._dummy_warn
113 self.dummy = self._dummy_warn
114 else:
114 else:
115 self.dummy = self._dummy_silent
115 self.dummy = self._dummy_silent
116
116
117 def __str__(self):
117 def __str__(self):
118 return "<IPythonNotRunning>"
118 return "<IPythonNotRunning>"
119
119
120 __repr__ = __str__
120 __repr__ = __str__
121
121
122 def __getattr__(self,name):
122 def __getattr__(self,name):
123 return self.dummy
123 return self.dummy
124
124
125 def _dummy_warn(self,*args,**kw):
125 def _dummy_warn(self,*args,**kw):
126 """Dummy function, which doesn't do anything but warn."""
126 """Dummy function, which doesn't do anything but warn."""
127
127
128 print ("IPython is not running, this is a dummy no-op function")
128 print ("IPython is not running, this is a dummy no-op function")
129
129
130 def _dummy_silent(self,*args,**kw):
130 def _dummy_silent(self,*args,**kw):
131 """Dummy function, which doesn't do anything and emits no warnings."""
131 """Dummy function, which doesn't do anything and emits no warnings."""
132 pass
132 pass
133
133
134 _recent = None
134 _recent = None
135
135
136
136
137 def get(allow_dummy=False,dummy_warn=True):
137 def get(allow_dummy=False,dummy_warn=True):
138 """Get an IPApi object.
138 """Get an IPApi object.
139
139
140 If allow_dummy is true, returns an instance of IPythonNotRunning
140 If allow_dummy is true, returns an instance of IPythonNotRunning
141 instead of None if not running under IPython.
141 instead of None if not running under IPython.
142
142
143 If dummy_warn is false, the dummy instance will be completely silent.
143 If dummy_warn is false, the dummy instance will be completely silent.
144
144
145 Running this should be the first thing you do when writing extensions that
145 Running this should be the first thing you do when writing extensions that
146 can be imported as normal modules. You can then direct all the
146 can be imported as normal modules. You can then direct all the
147 configuration operations against the returned object.
147 configuration operations against the returned object.
148 """
148 """
149 global _recent
149 global _recent
150 if allow_dummy and not _recent:
150 if allow_dummy and not _recent:
151 _recent = IPythonNotRunning(dummy_warn)
151 _recent = IPythonNotRunning(dummy_warn)
152 return _recent
152 return _recent
153
153
154 class IPApi:
154 class IPApi:
155 """ The actual API class for configuring IPython
155 """ The actual API class for configuring IPython
156
156
157 You should do all of the IPython configuration by getting an IPApi object
157 You should do all of the IPython configuration by getting an IPApi object
158 with IPython.ipapi.get() and using the attributes and methods of the
158 with IPython.ipapi.get() and using the attributes and methods of the
159 returned object."""
159 returned object."""
160
160
161 def __init__(self,ip):
161 def __init__(self,ip):
162
162
163 # All attributes exposed here are considered to be the public API of
163 # All attributes exposed here are considered to be the public API of
164 # IPython. As needs dictate, some of these may be wrapped as
164 # IPython. As needs dictate, some of these may be wrapped as
165 # properties.
165 # properties.
166
166
167 self.magic = ip.ipmagic
167 self.magic = ip.ipmagic
168
168
169 self.system = ip.system
169 self.system = ip.system
170
170
171 self.set_hook = ip.set_hook
171 self.set_hook = ip.set_hook
172
172
173 self.set_custom_exc = ip.set_custom_exc
173 self.set_custom_exc = ip.set_custom_exc
174
174
175 self.user_ns = ip.user_ns
175 self.user_ns = ip.user_ns
176
176
177 self.set_crash_handler = ip.set_crash_handler
177 self.set_crash_handler = ip.set_crash_handler
178
178
179 # Session-specific data store, which can be used to store
179 # Session-specific data store, which can be used to store
180 # data that should persist through the ipython session.
180 # data that should persist through the ipython session.
181 self.meta = ip.meta
181 self.meta = ip.meta
182
182
183 # The ipython instance provided
183 # The ipython instance provided
184 self.IP = ip
184 self.IP = ip
185
185
186 self.extensions = {}
186 self.extensions = {}
187 global _recent
187 global _recent
188 _recent = self
188 _recent = self
189
189
190 # Use a property for some things which are added to the instance very
190 # Use a property for some things which are added to the instance very
191 # late. I don't have time right now to disentangle the initialization
191 # late. I don't have time right now to disentangle the initialization
192 # order issues, so a property lets us delay item extraction while
192 # order issues, so a property lets us delay item extraction while
193 # providing a normal attribute API.
193 # providing a normal attribute API.
194 def get_db(self):
194 def get_db(self):
195 """A handle to persistent dict-like database (a PickleShareDB object)"""
195 """A handle to persistent dict-like database (a PickleShareDB object)"""
196 return self.IP.db
196 return self.IP.db
197
197
198 db = property(get_db,None,None,get_db.__doc__)
198 db = property(get_db,None,None,get_db.__doc__)
199
199
200 def get_options(self):
200 def get_options(self):
201 """All configurable variables."""
201 """All configurable variables."""
202
202
203 # catch typos by disabling new attribute creation. If new attr creation
203 # catch typos by disabling new attribute creation. If new attr creation
204 # is in fact wanted (e.g. when exposing new options), do allow_new_attr(True)
204 # is in fact wanted (e.g. when exposing new options), do allow_new_attr(True)
205 # for the received rc struct.
205 # for the received rc struct.
206
206
207 self.IP.rc.allow_new_attr(False)
207 self.IP.rc.allow_new_attr(False)
208 return self.IP.rc
208 return self.IP.rc
209
209
210 options = property(get_options,None,None,get_options.__doc__)
210 options = property(get_options,None,None,get_options.__doc__)
211
211
212 def expose_magic(self,magicname, func):
212 def expose_magic(self,magicname, func):
213 ''' Expose own function as magic function for ipython
213 ''' Expose own function as magic function for ipython
214
214
215 def foo_impl(self,parameter_s=''):
215 def foo_impl(self,parameter_s=''):
216 """My very own magic!. (Use docstrings, IPython reads them)."""
216 """My very own magic!. (Use docstrings, IPython reads them)."""
217 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
217 print 'Magic function. Passed parameter is between < >: <'+parameter_s+'>'
218 print 'The self object is:',self
218 print 'The self object is:',self
219
219
220 ipapi.expose_magic("foo",foo_impl)
220 ipapi.expose_magic("foo",foo_impl)
221 '''
221 '''
222
222
223 import new
223 import new
224 im = new.instancemethod(func,self.IP, self.IP.__class__)
224 im = new.instancemethod(func,self.IP, self.IP.__class__)
225 setattr(self.IP, "magic_" + magicname, im)
225 setattr(self.IP, "magic_" + magicname, im)
226
226
227 def ex(self,cmd):
227 def ex(self,cmd):
228 """ Execute a normal python statement in user namespace """
228 """ Execute a normal python statement in user namespace """
229 exec cmd in self.user_ns
229 exec cmd in self.user_ns
230
230
231 def ev(self,expr):
231 def ev(self,expr):
232 """ Evaluate python expression expr in user namespace
232 """ Evaluate python expression expr in user namespace
233
233
234 Returns the result of evaluation"""
234 Returns the result of evaluation"""
235 return eval(expr,self.user_ns)
235 return eval(expr,self.user_ns)
236
236
237 def runlines(self,lines):
237 def runlines(self,lines):
238 """ Run the specified lines in interpreter, honoring ipython directives.
238 """ Run the specified lines in interpreter, honoring ipython directives.
239
239
240 This allows %magic and !shell escape notations.
240 This allows %magic and !shell escape notations.
241
241
242 Takes either all lines in one string or list of lines.
242 Takes either all lines in one string or list of lines.
243 """
243 """
244 if isinstance(lines,basestring):
244 if isinstance(lines,basestring):
245 self.IP.runlines(lines)
245 self.IP.runlines(lines)
246 else:
246 else:
247 self.IP.runlines('\n'.join(lines))
247 self.IP.runlines('\n'.join(lines))
248
248
249 def to_user_ns(self,vars, interactive = True):
249 def to_user_ns(self,vars, interactive = True):
250 """Inject a group of variables into the IPython user namespace.
250 """Inject a group of variables into the IPython user namespace.
251
251
252 Inputs:
252 Inputs:
253
253
254 - vars: string with variable names separated by whitespace
254 - vars: string with variable names separated by whitespace, or a
255 dict with name/value pairs.
255
256
256 - interactive: if True (default), the var will be listed with
257 - interactive: if True (default), the var will be listed with
257 %whos et. al.
258 %whos et. al.
258
259
259 This utility routine is meant to ease interactive debugging work,
260 This utility routine is meant to ease interactive debugging work,
260 where you want to easily propagate some internal variable in your code
261 where you want to easily propagate some internal variable in your code
261 up to the interactive namespace for further exploration.
262 up to the interactive namespace for further exploration.
262
263
263 When you run code via %run, globals in your script become visible at
264 When you run code via %run, globals in your script become visible at
264 the interactive prompt, but this doesn't happen for locals inside your
265 the interactive prompt, but this doesn't happen for locals inside your
265 own functions and methods. Yet when debugging, it is common to want
266 own functions and methods. Yet when debugging, it is common to want
266 to explore some internal variables further at the interactive propmt.
267 to explore some internal variables further at the interactive propmt.
267
268
268 Examples:
269 Examples:
269
270
270 To use this, you first must obtain a handle on the ipython object as
271 To use this, you first must obtain a handle on the ipython object as
271 indicated above, via:
272 indicated above, via:
272
273
273 import IPython.ipapi
274 import IPython.ipapi
274 ip = IPython.ipapi.get()
275 ip = IPython.ipapi.get()
275
276
276 Once this is done, inside a routine foo() where you want to expose
277 Once this is done, inside a routine foo() where you want to expose
277 variables x and y, you do the following:
278 variables x and y, you do the following:
278
279
279 def foo():
280 def foo():
280 ...
281 ...
281 x = your_computation()
282 x = your_computation()
282 y = something_else()
283 y = something_else()
283
284
284 # This pushes x and y to the interactive prompt immediately, even
285 # This pushes x and y to the interactive prompt immediately, even
285 # if this routine crashes on the next line after:
286 # if this routine crashes on the next line after:
286 ip.to_user_ns('x y')
287 ip.to_user_ns('x y')
287 ...
288 ...
289
290 # To expose *ALL* the local variables from the function, use:
291 ip.to_user_ns(locals())
292
293 ...
288 # return
294 # return
289
295
290 If you need to rename variables, just use ip.user_ns with dict
296
291 and update:
297 If you need to rename variables, the dict input makes it easy. For
292
298 example, this call exposes variables 'foo' as 'x' and 'bar' as 'y'
293 # exposes variables 'foo' as 'x' and 'bar' as 'y' in IPython
299 in IPython user namespace:
294 # user namespace
300
295 ip.user_ns.update(dict(x=foo,y=bar))
301 ip.to_user_ns(dict(x=foo,y=bar))
296 """
302 """
297
303
298 # print 'vars given:',vars # dbg
304 # print 'vars given:',vars # dbg
299 # Get the caller's frame to evaluate the given names in
300 cf = sys._getframe(1)
301
305
302 user_ns = self.user_ns
306 # We need a dict of name/value pairs to do namespace updates.
307 if isinstance(vars,dict):
308 # If a dict was given, no need to change anything.
309 vdict = vars
310 elif isinstance(vars,basestring):
311 # If a string with names was given, get the caller's frame to
312 # evaluate the given names in
313 cf = sys._getframe(1)
314 vdict = {}
315 for name in vars.split():
316 try:
317 vdict[name] = eval(name,cf.f_globals,cf.f_locals)
318 except:
319 print ('could not get var. %s from %s' %
320 (name,cf.f_code.co_name))
321 else:
322 raise ValueError('vars must be a string or a dict')
323
324 # Propagate variables to user namespace
325 self.user_ns.update(vdict)
326
327 # And configure interactive visibility
303 config_ns = self.IP.user_config_ns
328 config_ns = self.IP.user_config_ns
304 for name in vars.split():
329 if interactive:
305 try:
330 for name,val in vdict.iteritems():
306 val = eval(name,cf.f_globals,cf.f_locals)
331 config_ns.pop(name,None)
307 user_ns[name] = val
332 else:
308 if not interactive:
333 for name,val in vdict.iteritems():
309 config_ns[name] = val
334 config_ns[name] = val
310 else:
335
311 config_ns.pop(name,None)
312 except:
313 print ('could not get var. %s from %s' %
314 (name,cf.f_code.co_name))
315
336
316 def expand_alias(self,line):
337 def expand_alias(self,line):
317 """ Expand an alias in the command line
338 """ Expand an alias in the command line
318
339
319 Returns the provided command line, possibly with the first word
340 Returns the provided command line, possibly with the first word
320 (command) translated according to alias expansion rules.
341 (command) translated according to alias expansion rules.
321
342
322 [ipython]|16> _ip.expand_aliases("np myfile.txt")
343 [ipython]|16> _ip.expand_aliases("np myfile.txt")
323 <16> 'q:/opt/np/notepad++.exe myfile.txt'
344 <16> 'q:/opt/np/notepad++.exe myfile.txt'
324 """
345 """
325
346
326 pre,fn,rest = self.IP.split_user_input(line)
347 pre,fn,rest = self.IP.split_user_input(line)
327 res = pre + self.IP.expand_aliases(fn,rest)
348 res = pre + self.IP.expand_aliases(fn,rest)
328 return res
349 return res
329
350
330 def defalias(self, name, cmd):
351 def defalias(self, name, cmd):
331 """ Define a new alias
352 """ Define a new alias
332
353
333 _ip.defalias('bb','bldmake bldfiles')
354 _ip.defalias('bb','bldmake bldfiles')
334
355
335 Creates a new alias named 'bb' in ipython user namespace
356 Creates a new alias named 'bb' in ipython user namespace
336 """
357 """
337
358
338 if callable(cmd):
359 if callable(cmd):
339 self.IP.alias_table[name] = cmd
360 self.IP.alias_table[name] = cmd
340 import IPython.shawodns
361 import IPython.shawodns
341 setattr(IPython.shadowns, name,cmd)
362 setattr(IPython.shadowns, name,cmd)
342 return
363 return
343
364
344
365
345 nargs = cmd.count('%s')
366 nargs = cmd.count('%s')
346 if nargs>0 and cmd.find('%l')>=0:
367 if nargs>0 and cmd.find('%l')>=0:
347 raise Exception('The %s and %l specifiers are mutually exclusive '
368 raise Exception('The %s and %l specifiers are mutually exclusive '
348 'in alias definitions.')
369 'in alias definitions.')
349
370
350 else: # all looks OK
371 else: # all looks OK
351 self.IP.alias_table[name] = (nargs,cmd)
372 self.IP.alias_table[name] = (nargs,cmd)
352
373
353 def defmacro(self, *args):
374 def defmacro(self, *args):
354 """ Define a new macro
375 """ Define a new macro
355
376
356 2 forms of calling:
377 2 forms of calling:
357
378
358 mac = _ip.defmacro('print "hello"\nprint "world"')
379 mac = _ip.defmacro('print "hello"\nprint "world"')
359
380
360 (doesn't put the created macro on user namespace)
381 (doesn't put the created macro on user namespace)
361
382
362 _ip.defmacro('build', 'bldmake bldfiles\nabld build winscw udeb')
383 _ip.defmacro('build', 'bldmake bldfiles\nabld build winscw udeb')
363
384
364 (creates a macro named 'build' in user namespace)
385 (creates a macro named 'build' in user namespace)
365 """
386 """
366
387
367 import IPython.macro
388 import IPython.macro
368
389
369 if len(args) == 1:
390 if len(args) == 1:
370 return IPython.macro.Macro(args[0])
391 return IPython.macro.Macro(args[0])
371 elif len(args) == 2:
392 elif len(args) == 2:
372 self.user_ns[args[0]] = IPython.macro.Macro(args[1])
393 self.user_ns[args[0]] = IPython.macro.Macro(args[1])
373 else:
394 else:
374 return Exception("_ip.defmacro must be called with 1 or 2 arguments")
395 return Exception("_ip.defmacro must be called with 1 or 2 arguments")
375
396
376 def set_next_input(self, s):
397 def set_next_input(self, s):
377 """ Sets the 'default' input string for the next command line.
398 """ Sets the 'default' input string for the next command line.
378
399
379 Requires readline.
400 Requires readline.
380
401
381 Example:
402 Example:
382
403
383 [D:\ipython]|1> _ip.set_next_input("Hello Word")
404 [D:\ipython]|1> _ip.set_next_input("Hello Word")
384 [D:\ipython]|2> Hello Word_ # cursor is here
405 [D:\ipython]|2> Hello Word_ # cursor is here
385 """
406 """
386
407
387 self.IP.rl_next_input = s
408 self.IP.rl_next_input = s
388
409
389 def load(self, mod):
410 def load(self, mod):
390 if mod in self.extensions:
411 if mod in self.extensions:
391 # just to make sure we don't init it twice
412 # just to make sure we don't init it twice
392 # note that if you 'load' a module that has already been
413 # note that if you 'load' a module that has already been
393 # imported, init_ipython gets run anyway
414 # imported, init_ipython gets run anyway
394
415
395 return self.extensions[mod]
416 return self.extensions[mod]
396 __import__(mod)
417 __import__(mod)
397 m = sys.modules[mod]
418 m = sys.modules[mod]
398 if hasattr(m,'init_ipython'):
419 if hasattr(m,'init_ipython'):
399 m.init_ipython(self)
420 m.init_ipython(self)
400 self.extensions[mod] = m
421 self.extensions[mod] = m
401 return m
422 return m
402
423
403
424
404 def launch_new_instance(user_ns = None):
425 def launch_new_instance(user_ns = None):
405 """ Make and start a new ipython instance.
426 """ Make and start a new ipython instance.
406
427
407 This can be called even without having an already initialized
428 This can be called even without having an already initialized
408 ipython session running.
429 ipython session running.
409
430
410 This is also used as the egg entry point for the 'ipython' script.
431 This is also used as the egg entry point for the 'ipython' script.
411
432
412 """
433 """
413 ses = make_session(user_ns)
434 ses = make_session(user_ns)
414 ses.mainloop()
435 ses.mainloop()
415
436
416
437
417 def make_user_ns(user_ns = None):
438 def make_user_ns(user_ns = None):
418 """Return a valid user interactive namespace.
439 """Return a valid user interactive namespace.
419
440
420 This builds a dict with the minimal information needed to operate as a
441 This builds a dict with the minimal information needed to operate as a
421 valid IPython user namespace, which you can pass to the various embedding
442 valid IPython user namespace, which you can pass to the various embedding
422 classes in ipython.
443 classes in ipython.
423 """
444 """
424
445
425 if user_ns is None:
446 if user_ns is None:
426 # Set __name__ to __main__ to better match the behavior of the
447 # Set __name__ to __main__ to better match the behavior of the
427 # normal interpreter.
448 # normal interpreter.
428 user_ns = {'__name__' :'__main__',
449 user_ns = {'__name__' :'__main__',
429 '__builtins__' : __builtin__,
450 '__builtins__' : __builtin__,
430 }
451 }
431 else:
452 else:
432 user_ns.setdefault('__name__','__main__')
453 user_ns.setdefault('__name__','__main__')
433 user_ns.setdefault('__builtins__',__builtin__)
454 user_ns.setdefault('__builtins__',__builtin__)
434
455
435 return user_ns
456 return user_ns
436
457
437
458
438 def make_user_global_ns(ns = None):
459 def make_user_global_ns(ns = None):
439 """Return a valid user global namespace.
460 """Return a valid user global namespace.
440
461
441 Similar to make_user_ns(), but global namespaces are really only needed in
462 Similar to make_user_ns(), but global namespaces are really only needed in
442 embedded applications, where there is a distinction between the user's
463 embedded applications, where there is a distinction between the user's
443 interactive namespace and the global one where ipython is running."""
464 interactive namespace and the global one where ipython is running."""
444
465
445 if ns is None: ns = {}
466 if ns is None: ns = {}
446 return ns
467 return ns
447
468
448
469
449 def make_session(user_ns = None):
470 def make_session(user_ns = None):
450 """Makes, but does not launch an IPython session.
471 """Makes, but does not launch an IPython session.
451
472
452 Later on you can call obj.mainloop() on the returned object.
473 Later on you can call obj.mainloop() on the returned object.
453
474
454 Inputs:
475 Inputs:
455
476
456 - user_ns(None): a dict to be used as the user's namespace with initial
477 - user_ns(None): a dict to be used as the user's namespace with initial
457 data.
478 data.
458
479
459 WARNING: This should *not* be run when a session exists already."""
480 WARNING: This should *not* be run when a session exists already."""
460
481
461 import IPython
482 import IPython
462 return IPython.Shell.start(user_ns)
483 return IPython.Shell.start(user_ns)
463
484
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now