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