##// END OF EJS Templates
timeit.template uses new-style string formatting from Python 3.3
Thomas Kluyver -
Show More
@@ -1,985 +1,990 b''
1 """Implementation of execution-related magic functions.
1 """Implementation of execution-related magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import __builtin__ as builtin_mod
16 import __builtin__ as builtin_mod
17 import bdb
17 import bdb
18 import os
18 import os
19 import sys
19 import sys
20 import time
20 import time
21 from StringIO import StringIO
21 from StringIO import StringIO
22
22
23 # cProfile was added in Python2.5
23 # cProfile was added in Python2.5
24 try:
24 try:
25 import cProfile as profile
25 import cProfile as profile
26 import pstats
26 import pstats
27 except ImportError:
27 except ImportError:
28 # profile isn't bundled by default in Debian for license reasons
28 # profile isn't bundled by default in Debian for license reasons
29 try:
29 try:
30 import profile, pstats
30 import profile, pstats
31 except ImportError:
31 except ImportError:
32 profile = pstats = None
32 profile = pstats = None
33
33
34 # Our own packages
34 # Our own packages
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import page
36 from IPython.core import page
37 from IPython.core.error import UsageError
37 from IPython.core.error import UsageError
38 from IPython.core.macro import Macro
38 from IPython.core.macro import Macro
39 from IPython.core.magic import (Magics, magics_class, line_magic,
39 from IPython.core.magic import (Magics, magics_class, line_magic,
40 line_cell_magic, on_off, needs_local_scope)
40 line_cell_magic, on_off, needs_local_scope)
41 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.testing.skipdoctest import skip_doctest
42 from IPython.utils import py3compat
42 from IPython.utils import py3compat
43 from IPython.utils.ipstruct import Struct
43 from IPython.utils.ipstruct import Struct
44 from IPython.utils.module_paths import find_mod
44 from IPython.utils.module_paths import find_mod
45 from IPython.utils.path import get_py_filename, unquote_filename
45 from IPython.utils.path import get_py_filename, unquote_filename
46 from IPython.utils.timing import clock, clock2
46 from IPython.utils.timing import clock, clock2
47 from IPython.utils.warn import warn, error
47 from IPython.utils.warn import warn, error
48
48
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50 # Magic implementation classes
50 # Magic implementation classes
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52
52
53 @magics_class
53 @magics_class
54 class ExecutionMagics(Magics):
54 class ExecutionMagics(Magics):
55 """Magics related to code execution, debugging, profiling, etc.
55 """Magics related to code execution, debugging, profiling, etc.
56
56
57 """
57 """
58
58
59 def __init__(self, shell):
59 def __init__(self, shell):
60 super(ExecutionMagics, self).__init__(shell)
60 super(ExecutionMagics, self).__init__(shell)
61 if profile is None:
61 if profile is None:
62 self.prun = self.profile_missing_notice
62 self.prun = self.profile_missing_notice
63 # Default execution function used to actually run user code.
63 # Default execution function used to actually run user code.
64 self.default_runner = None
64 self.default_runner = None
65
65
66 def profile_missing_notice(self, *args, **kwargs):
66 def profile_missing_notice(self, *args, **kwargs):
67 error("""\
67 error("""\
68 The profile module could not be found. It has been removed from the standard
68 The profile module could not be found. It has been removed from the standard
69 python packages because of its non-free license. To use profiling, install the
69 python packages because of its non-free license. To use profiling, install the
70 python-profiler package from non-free.""")
70 python-profiler package from non-free.""")
71
71
72 @skip_doctest
72 @skip_doctest
73 @line_cell_magic
73 @line_cell_magic
74 def prun(self, parameter_s='', cell=None, user_mode=True,
74 def prun(self, parameter_s='', cell=None, user_mode=True,
75 opts=None,arg_lst=None,prog_ns=None):
75 opts=None,arg_lst=None,prog_ns=None):
76
76
77 """Run a statement through the python code profiler.
77 """Run a statement through the python code profiler.
78
78
79 Usage, in line mode:
79 Usage, in line mode:
80 %prun [options] statement
80 %prun [options] statement
81
81
82 Usage, in cell mode:
82 Usage, in cell mode:
83 %%prun [options] [statement]
83 %%prun [options] [statement]
84 code...
84 code...
85 code...
85 code...
86
86
87 In cell mode, the additional code lines are appended to the (possibly
87 In cell mode, the additional code lines are appended to the (possibly
88 empty) statement in the first line. Cell mode allows you to easily
88 empty) statement in the first line. Cell mode allows you to easily
89 profile multiline blocks without having to put them in a separate
89 profile multiline blocks without having to put them in a separate
90 function.
90 function.
91
91
92 The given statement (which doesn't require quote marks) is run via the
92 The given statement (which doesn't require quote marks) is run via the
93 python profiler in a manner similar to the profile.run() function.
93 python profiler in a manner similar to the profile.run() function.
94 Namespaces are internally managed to work correctly; profile.run
94 Namespaces are internally managed to work correctly; profile.run
95 cannot be used in IPython because it makes certain assumptions about
95 cannot be used in IPython because it makes certain assumptions about
96 namespaces which do not hold under IPython.
96 namespaces which do not hold under IPython.
97
97
98 Options:
98 Options:
99
99
100 -l <limit>: you can place restrictions on what or how much of the
100 -l <limit>: you can place restrictions on what or how much of the
101 profile gets printed. The limit value can be:
101 profile gets printed. The limit value can be:
102
102
103 * A string: only information for function names containing this string
103 * A string: only information for function names containing this string
104 is printed.
104 is printed.
105
105
106 * An integer: only these many lines are printed.
106 * An integer: only these many lines are printed.
107
107
108 * A float (between 0 and 1): this fraction of the report is printed
108 * A float (between 0 and 1): this fraction of the report is printed
109 (for example, use a limit of 0.4 to see the topmost 40% only).
109 (for example, use a limit of 0.4 to see the topmost 40% only).
110
110
111 You can combine several limits with repeated use of the option. For
111 You can combine several limits with repeated use of the option. For
112 example, '-l __init__ -l 5' will print only the topmost 5 lines of
112 example, '-l __init__ -l 5' will print only the topmost 5 lines of
113 information about class constructors.
113 information about class constructors.
114
114
115 -r: return the pstats.Stats object generated by the profiling. This
115 -r: return the pstats.Stats object generated by the profiling. This
116 object has all the information about the profile in it, and you can
116 object has all the information about the profile in it, and you can
117 later use it for further analysis or in other functions.
117 later use it for further analysis or in other functions.
118
118
119 -s <key>: sort profile by given key. You can provide more than one key
119 -s <key>: sort profile by given key. You can provide more than one key
120 by using the option several times: '-s key1 -s key2 -s key3...'. The
120 by using the option several times: '-s key1 -s key2 -s key3...'. The
121 default sorting key is 'time'.
121 default sorting key is 'time'.
122
122
123 The following is copied verbatim from the profile documentation
123 The following is copied verbatim from the profile documentation
124 referenced below:
124 referenced below:
125
125
126 When more than one key is provided, additional keys are used as
126 When more than one key is provided, additional keys are used as
127 secondary criteria when the there is equality in all keys selected
127 secondary criteria when the there is equality in all keys selected
128 before them.
128 before them.
129
129
130 Abbreviations can be used for any key names, as long as the
130 Abbreviations can be used for any key names, as long as the
131 abbreviation is unambiguous. The following are the keys currently
131 abbreviation is unambiguous. The following are the keys currently
132 defined:
132 defined:
133
133
134 Valid Arg Meaning
134 Valid Arg Meaning
135 "calls" call count
135 "calls" call count
136 "cumulative" cumulative time
136 "cumulative" cumulative time
137 "file" file name
137 "file" file name
138 "module" file name
138 "module" file name
139 "pcalls" primitive call count
139 "pcalls" primitive call count
140 "line" line number
140 "line" line number
141 "name" function name
141 "name" function name
142 "nfl" name/file/line
142 "nfl" name/file/line
143 "stdname" standard name
143 "stdname" standard name
144 "time" internal time
144 "time" internal time
145
145
146 Note that all sorts on statistics are in descending order (placing
146 Note that all sorts on statistics are in descending order (placing
147 most time consuming items first), where as name, file, and line number
147 most time consuming items first), where as name, file, and line number
148 searches are in ascending order (i.e., alphabetical). The subtle
148 searches are in ascending order (i.e., alphabetical). The subtle
149 distinction between "nfl" and "stdname" is that the standard name is a
149 distinction between "nfl" and "stdname" is that the standard name is a
150 sort of the name as printed, which means that the embedded line
150 sort of the name as printed, which means that the embedded line
151 numbers get compared in an odd way. For example, lines 3, 20, and 40
151 numbers get compared in an odd way. For example, lines 3, 20, and 40
152 would (if the file names were the same) appear in the string order
152 would (if the file names were the same) appear in the string order
153 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
153 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
154 line numbers. In fact, sort_stats("nfl") is the same as
154 line numbers. In fact, sort_stats("nfl") is the same as
155 sort_stats("name", "file", "line").
155 sort_stats("name", "file", "line").
156
156
157 -T <filename>: save profile results as shown on screen to a text
157 -T <filename>: save profile results as shown on screen to a text
158 file. The profile is still shown on screen.
158 file. The profile is still shown on screen.
159
159
160 -D <filename>: save (via dump_stats) profile statistics to given
160 -D <filename>: save (via dump_stats) profile statistics to given
161 filename. This data is in a format understood by the pstats module, and
161 filename. This data is in a format understood by the pstats module, and
162 is generated by a call to the dump_stats() method of profile
162 is generated by a call to the dump_stats() method of profile
163 objects. The profile is still shown on screen.
163 objects. The profile is still shown on screen.
164
164
165 -q: suppress output to the pager. Best used with -T and/or -D above.
165 -q: suppress output to the pager. Best used with -T and/or -D above.
166
166
167 If you want to run complete programs under the profiler's control, use
167 If you want to run complete programs under the profiler's control, use
168 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
168 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
169 contains profiler specific options as described here.
169 contains profiler specific options as described here.
170
170
171 You can read the complete documentation for the profile module with::
171 You can read the complete documentation for the profile module with::
172
172
173 In [1]: import profile; profile.help()
173 In [1]: import profile; profile.help()
174 """
174 """
175
175
176 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
176 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
177
177
178 if user_mode: # regular user call
178 if user_mode: # regular user call
179 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
179 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
180 list_all=True, posix=False)
180 list_all=True, posix=False)
181 namespace = self.shell.user_ns
181 namespace = self.shell.user_ns
182 if cell is not None:
182 if cell is not None:
183 arg_str += '\n' + cell
183 arg_str += '\n' + cell
184 else: # called to run a program by %run -p
184 else: # called to run a program by %run -p
185 try:
185 try:
186 filename = get_py_filename(arg_lst[0])
186 filename = get_py_filename(arg_lst[0])
187 except IOError as e:
187 except IOError as e:
188 try:
188 try:
189 msg = str(e)
189 msg = str(e)
190 except UnicodeError:
190 except UnicodeError:
191 msg = e.message
191 msg = e.message
192 error(msg)
192 error(msg)
193 return
193 return
194
194
195 arg_str = 'execfile(filename,prog_ns)'
195 arg_str = 'execfile(filename,prog_ns)'
196 namespace = {
196 namespace = {
197 'execfile': self.shell.safe_execfile,
197 'execfile': self.shell.safe_execfile,
198 'prog_ns': prog_ns,
198 'prog_ns': prog_ns,
199 'filename': filename
199 'filename': filename
200 }
200 }
201
201
202 opts.merge(opts_def)
202 opts.merge(opts_def)
203
203
204 prof = profile.Profile()
204 prof = profile.Profile()
205 try:
205 try:
206 prof = prof.runctx(arg_str,namespace,namespace)
206 prof = prof.runctx(arg_str,namespace,namespace)
207 sys_exit = ''
207 sys_exit = ''
208 except SystemExit:
208 except SystemExit:
209 sys_exit = """*** SystemExit exception caught in code being profiled."""
209 sys_exit = """*** SystemExit exception caught in code being profiled."""
210
210
211 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
211 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
212
212
213 lims = opts.l
213 lims = opts.l
214 if lims:
214 if lims:
215 lims = [] # rebuild lims with ints/floats/strings
215 lims = [] # rebuild lims with ints/floats/strings
216 for lim in opts.l:
216 for lim in opts.l:
217 try:
217 try:
218 lims.append(int(lim))
218 lims.append(int(lim))
219 except ValueError:
219 except ValueError:
220 try:
220 try:
221 lims.append(float(lim))
221 lims.append(float(lim))
222 except ValueError:
222 except ValueError:
223 lims.append(lim)
223 lims.append(lim)
224
224
225 # Trap output.
225 # Trap output.
226 stdout_trap = StringIO()
226 stdout_trap = StringIO()
227
227
228 if hasattr(stats,'stream'):
228 if hasattr(stats,'stream'):
229 # In newer versions of python, the stats object has a 'stream'
229 # In newer versions of python, the stats object has a 'stream'
230 # attribute to write into.
230 # attribute to write into.
231 stats.stream = stdout_trap
231 stats.stream = stdout_trap
232 stats.print_stats(*lims)
232 stats.print_stats(*lims)
233 else:
233 else:
234 # For older versions, we manually redirect stdout during printing
234 # For older versions, we manually redirect stdout during printing
235 sys_stdout = sys.stdout
235 sys_stdout = sys.stdout
236 try:
236 try:
237 sys.stdout = stdout_trap
237 sys.stdout = stdout_trap
238 stats.print_stats(*lims)
238 stats.print_stats(*lims)
239 finally:
239 finally:
240 sys.stdout = sys_stdout
240 sys.stdout = sys_stdout
241
241
242 output = stdout_trap.getvalue()
242 output = stdout_trap.getvalue()
243 output = output.rstrip()
243 output = output.rstrip()
244
244
245 if 'q' not in opts:
245 if 'q' not in opts:
246 page.page(output)
246 page.page(output)
247 print sys_exit,
247 print sys_exit,
248
248
249 dump_file = opts.D[0]
249 dump_file = opts.D[0]
250 text_file = opts.T[0]
250 text_file = opts.T[0]
251 if dump_file:
251 if dump_file:
252 dump_file = unquote_filename(dump_file)
252 dump_file = unquote_filename(dump_file)
253 prof.dump_stats(dump_file)
253 prof.dump_stats(dump_file)
254 print '\n*** Profile stats marshalled to file',\
254 print '\n*** Profile stats marshalled to file',\
255 `dump_file`+'.',sys_exit
255 `dump_file`+'.',sys_exit
256 if text_file:
256 if text_file:
257 text_file = unquote_filename(text_file)
257 text_file = unquote_filename(text_file)
258 pfile = open(text_file,'w')
258 pfile = open(text_file,'w')
259 pfile.write(output)
259 pfile.write(output)
260 pfile.close()
260 pfile.close()
261 print '\n*** Profile printout saved to text file',\
261 print '\n*** Profile printout saved to text file',\
262 `text_file`+'.',sys_exit
262 `text_file`+'.',sys_exit
263
263
264 if opts.has_key('r'):
264 if opts.has_key('r'):
265 return stats
265 return stats
266 else:
266 else:
267 return None
267 return None
268
268
269 @line_magic
269 @line_magic
270 def pdb(self, parameter_s=''):
270 def pdb(self, parameter_s=''):
271 """Control the automatic calling of the pdb interactive debugger.
271 """Control the automatic calling of the pdb interactive debugger.
272
272
273 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
273 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
274 argument it works as a toggle.
274 argument it works as a toggle.
275
275
276 When an exception is triggered, IPython can optionally call the
276 When an exception is triggered, IPython can optionally call the
277 interactive pdb debugger after the traceback printout. %pdb toggles
277 interactive pdb debugger after the traceback printout. %pdb toggles
278 this feature on and off.
278 this feature on and off.
279
279
280 The initial state of this feature is set in your configuration
280 The initial state of this feature is set in your configuration
281 file (the option is ``InteractiveShell.pdb``).
281 file (the option is ``InteractiveShell.pdb``).
282
282
283 If you want to just activate the debugger AFTER an exception has fired,
283 If you want to just activate the debugger AFTER an exception has fired,
284 without having to type '%pdb on' and rerunning your code, you can use
284 without having to type '%pdb on' and rerunning your code, you can use
285 the %debug magic."""
285 the %debug magic."""
286
286
287 par = parameter_s.strip().lower()
287 par = parameter_s.strip().lower()
288
288
289 if par:
289 if par:
290 try:
290 try:
291 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
291 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
292 except KeyError:
292 except KeyError:
293 print ('Incorrect argument. Use on/1, off/0, '
293 print ('Incorrect argument. Use on/1, off/0, '
294 'or nothing for a toggle.')
294 'or nothing for a toggle.')
295 return
295 return
296 else:
296 else:
297 # toggle
297 # toggle
298 new_pdb = not self.shell.call_pdb
298 new_pdb = not self.shell.call_pdb
299
299
300 # set on the shell
300 # set on the shell
301 self.shell.call_pdb = new_pdb
301 self.shell.call_pdb = new_pdb
302 print 'Automatic pdb calling has been turned',on_off(new_pdb)
302 print 'Automatic pdb calling has been turned',on_off(new_pdb)
303
303
304 @line_magic
304 @line_magic
305 def debug(self, parameter_s=''):
305 def debug(self, parameter_s=''):
306 """Activate the interactive debugger in post-mortem mode.
306 """Activate the interactive debugger in post-mortem mode.
307
307
308 If an exception has just occurred, this lets you inspect its stack
308 If an exception has just occurred, this lets you inspect its stack
309 frames interactively. Note that this will always work only on the last
309 frames interactively. Note that this will always work only on the last
310 traceback that occurred, so you must call this quickly after an
310 traceback that occurred, so you must call this quickly after an
311 exception that you wish to inspect has fired, because if another one
311 exception that you wish to inspect has fired, because if another one
312 occurs, it clobbers the previous one.
312 occurs, it clobbers the previous one.
313
313
314 If you want IPython to automatically do this on every exception, see
314 If you want IPython to automatically do this on every exception, see
315 the %pdb magic for more details.
315 the %pdb magic for more details.
316 """
316 """
317 self.shell.debugger(force=True)
317 self.shell.debugger(force=True)
318
318
319 @line_magic
319 @line_magic
320 def tb(self, s):
320 def tb(self, s):
321 """Print the last traceback with the currently active exception mode.
321 """Print the last traceback with the currently active exception mode.
322
322
323 See %xmode for changing exception reporting modes."""
323 See %xmode for changing exception reporting modes."""
324 self.shell.showtraceback()
324 self.shell.showtraceback()
325
325
326 @skip_doctest
326 @skip_doctest
327 @line_magic
327 @line_magic
328 def run(self, parameter_s='', runner=None,
328 def run(self, parameter_s='', runner=None,
329 file_finder=get_py_filename):
329 file_finder=get_py_filename):
330 """Run the named file inside IPython as a program.
330 """Run the named file inside IPython as a program.
331
331
332 Usage:\\
332 Usage:\\
333 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
333 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
334
334
335 Parameters after the filename are passed as command-line arguments to
335 Parameters after the filename are passed as command-line arguments to
336 the program (put in sys.argv). Then, control returns to IPython's
336 the program (put in sys.argv). Then, control returns to IPython's
337 prompt.
337 prompt.
338
338
339 This is similar to running at a system prompt:\\
339 This is similar to running at a system prompt:\\
340 $ python file args\\
340 $ python file args\\
341 but with the advantage of giving you IPython's tracebacks, and of
341 but with the advantage of giving you IPython's tracebacks, and of
342 loading all variables into your interactive namespace for further use
342 loading all variables into your interactive namespace for further use
343 (unless -p is used, see below).
343 (unless -p is used, see below).
344
344
345 The file is executed in a namespace initially consisting only of
345 The file is executed in a namespace initially consisting only of
346 __name__=='__main__' and sys.argv constructed as indicated. It thus
346 __name__=='__main__' and sys.argv constructed as indicated. It thus
347 sees its environment as if it were being run as a stand-alone program
347 sees its environment as if it were being run as a stand-alone program
348 (except for sharing global objects such as previously imported
348 (except for sharing global objects such as previously imported
349 modules). But after execution, the IPython interactive namespace gets
349 modules). But after execution, the IPython interactive namespace gets
350 updated with all variables defined in the program (except for __name__
350 updated with all variables defined in the program (except for __name__
351 and sys.argv). This allows for very convenient loading of code for
351 and sys.argv). This allows for very convenient loading of code for
352 interactive work, while giving each program a 'clean sheet' to run in.
352 interactive work, while giving each program a 'clean sheet' to run in.
353
353
354 Options:
354 Options:
355
355
356 -n: __name__ is NOT set to '__main__', but to the running file's name
356 -n: __name__ is NOT set to '__main__', but to the running file's name
357 without extension (as python does under import). This allows running
357 without extension (as python does under import). This allows running
358 scripts and reloading the definitions in them without calling code
358 scripts and reloading the definitions in them without calling code
359 protected by an ' if __name__ == "__main__" ' clause.
359 protected by an ' if __name__ == "__main__" ' clause.
360
360
361 -i: run the file in IPython's namespace instead of an empty one. This
361 -i: run the file in IPython's namespace instead of an empty one. This
362 is useful if you are experimenting with code written in a text editor
362 is useful if you are experimenting with code written in a text editor
363 which depends on variables defined interactively.
363 which depends on variables defined interactively.
364
364
365 -e: ignore sys.exit() calls or SystemExit exceptions in the script
365 -e: ignore sys.exit() calls or SystemExit exceptions in the script
366 being run. This is particularly useful if IPython is being used to
366 being run. This is particularly useful if IPython is being used to
367 run unittests, which always exit with a sys.exit() call. In such
367 run unittests, which always exit with a sys.exit() call. In such
368 cases you are interested in the output of the test results, not in
368 cases you are interested in the output of the test results, not in
369 seeing a traceback of the unittest module.
369 seeing a traceback of the unittest module.
370
370
371 -t: print timing information at the end of the run. IPython will give
371 -t: print timing information at the end of the run. IPython will give
372 you an estimated CPU time consumption for your script, which under
372 you an estimated CPU time consumption for your script, which under
373 Unix uses the resource module to avoid the wraparound problems of
373 Unix uses the resource module to avoid the wraparound problems of
374 time.clock(). Under Unix, an estimate of time spent on system tasks
374 time.clock(). Under Unix, an estimate of time spent on system tasks
375 is also given (for Windows platforms this is reported as 0.0).
375 is also given (for Windows platforms this is reported as 0.0).
376
376
377 If -t is given, an additional -N<N> option can be given, where <N>
377 If -t is given, an additional -N<N> option can be given, where <N>
378 must be an integer indicating how many times you want the script to
378 must be an integer indicating how many times you want the script to
379 run. The final timing report will include total and per run results.
379 run. The final timing report will include total and per run results.
380
380
381 For example (testing the script uniq_stable.py)::
381 For example (testing the script uniq_stable.py)::
382
382
383 In [1]: run -t uniq_stable
383 In [1]: run -t uniq_stable
384
384
385 IPython CPU timings (estimated):\\
385 IPython CPU timings (estimated):\\
386 User : 0.19597 s.\\
386 User : 0.19597 s.\\
387 System: 0.0 s.\\
387 System: 0.0 s.\\
388
388
389 In [2]: run -t -N5 uniq_stable
389 In [2]: run -t -N5 uniq_stable
390
390
391 IPython CPU timings (estimated):\\
391 IPython CPU timings (estimated):\\
392 Total runs performed: 5\\
392 Total runs performed: 5\\
393 Times : Total Per run\\
393 Times : Total Per run\\
394 User : 0.910862 s, 0.1821724 s.\\
394 User : 0.910862 s, 0.1821724 s.\\
395 System: 0.0 s, 0.0 s.
395 System: 0.0 s, 0.0 s.
396
396
397 -d: run your program under the control of pdb, the Python debugger.
397 -d: run your program under the control of pdb, the Python debugger.
398 This allows you to execute your program step by step, watch variables,
398 This allows you to execute your program step by step, watch variables,
399 etc. Internally, what IPython does is similar to calling:
399 etc. Internally, what IPython does is similar to calling:
400
400
401 pdb.run('execfile("YOURFILENAME")')
401 pdb.run('execfile("YOURFILENAME")')
402
402
403 with a breakpoint set on line 1 of your file. You can change the line
403 with a breakpoint set on line 1 of your file. You can change the line
404 number for this automatic breakpoint to be <N> by using the -bN option
404 number for this automatic breakpoint to be <N> by using the -bN option
405 (where N must be an integer). For example::
405 (where N must be an integer). For example::
406
406
407 %run -d -b40 myscript
407 %run -d -b40 myscript
408
408
409 will set the first breakpoint at line 40 in myscript.py. Note that
409 will set the first breakpoint at line 40 in myscript.py. Note that
410 the first breakpoint must be set on a line which actually does
410 the first breakpoint must be set on a line which actually does
411 something (not a comment or docstring) for it to stop execution.
411 something (not a comment or docstring) for it to stop execution.
412
412
413 When the pdb debugger starts, you will see a (Pdb) prompt. You must
413 When the pdb debugger starts, you will see a (Pdb) prompt. You must
414 first enter 'c' (without quotes) to start execution up to the first
414 first enter 'c' (without quotes) to start execution up to the first
415 breakpoint.
415 breakpoint.
416
416
417 Entering 'help' gives information about the use of the debugger. You
417 Entering 'help' gives information about the use of the debugger. You
418 can easily see pdb's full documentation with "import pdb;pdb.help()"
418 can easily see pdb's full documentation with "import pdb;pdb.help()"
419 at a prompt.
419 at a prompt.
420
420
421 -p: run program under the control of the Python profiler module (which
421 -p: run program under the control of the Python profiler module (which
422 prints a detailed report of execution times, function calls, etc).
422 prints a detailed report of execution times, function calls, etc).
423
423
424 You can pass other options after -p which affect the behavior of the
424 You can pass other options after -p which affect the behavior of the
425 profiler itself. See the docs for %prun for details.
425 profiler itself. See the docs for %prun for details.
426
426
427 In this mode, the program's variables do NOT propagate back to the
427 In this mode, the program's variables do NOT propagate back to the
428 IPython interactive namespace (because they remain in the namespace
428 IPython interactive namespace (because they remain in the namespace
429 where the profiler executes them).
429 where the profiler executes them).
430
430
431 Internally this triggers a call to %prun, see its documentation for
431 Internally this triggers a call to %prun, see its documentation for
432 details on the options available specifically for profiling.
432 details on the options available specifically for profiling.
433
433
434 There is one special usage for which the text above doesn't apply:
434 There is one special usage for which the text above doesn't apply:
435 if the filename ends with .ipy, the file is run as ipython script,
435 if the filename ends with .ipy, the file is run as ipython script,
436 just as if the commands were written on IPython prompt.
436 just as if the commands were written on IPython prompt.
437
437
438 -m: specify module name to load instead of script path. Similar to
438 -m: specify module name to load instead of script path. Similar to
439 the -m option for the python interpreter. Use this option last if you
439 the -m option for the python interpreter. Use this option last if you
440 want to combine with other %run options. Unlike the python interpreter
440 want to combine with other %run options. Unlike the python interpreter
441 only source modules are allowed no .pyc or .pyo files.
441 only source modules are allowed no .pyc or .pyo files.
442 For example::
442 For example::
443
443
444 %run -m example
444 %run -m example
445
445
446 will run the example module.
446 will run the example module.
447
447
448 """
448 """
449
449
450 # get arguments and set sys.argv for program to be run.
450 # get arguments and set sys.argv for program to be run.
451 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
451 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
452 mode='list', list_all=1)
452 mode='list', list_all=1)
453 if "m" in opts:
453 if "m" in opts:
454 modulename = opts["m"][0]
454 modulename = opts["m"][0]
455 modpath = find_mod(modulename)
455 modpath = find_mod(modulename)
456 if modpath is None:
456 if modpath is None:
457 warn('%r is not a valid modulename on sys.path'%modulename)
457 warn('%r is not a valid modulename on sys.path'%modulename)
458 return
458 return
459 arg_lst = [modpath] + arg_lst
459 arg_lst = [modpath] + arg_lst
460 try:
460 try:
461 filename = file_finder(arg_lst[0])
461 filename = file_finder(arg_lst[0])
462 except IndexError:
462 except IndexError:
463 warn('you must provide at least a filename.')
463 warn('you must provide at least a filename.')
464 print '\n%run:\n', oinspect.getdoc(self.run)
464 print '\n%run:\n', oinspect.getdoc(self.run)
465 return
465 return
466 except IOError as e:
466 except IOError as e:
467 try:
467 try:
468 msg = str(e)
468 msg = str(e)
469 except UnicodeError:
469 except UnicodeError:
470 msg = e.message
470 msg = e.message
471 error(msg)
471 error(msg)
472 return
472 return
473
473
474 if filename.lower().endswith('.ipy'):
474 if filename.lower().endswith('.ipy'):
475 self.shell.safe_execfile_ipy(filename)
475 self.shell.safe_execfile_ipy(filename)
476 return
476 return
477
477
478 # Control the response to exit() calls made by the script being run
478 # Control the response to exit() calls made by the script being run
479 exit_ignore = 'e' in opts
479 exit_ignore = 'e' in opts
480
480
481 # Make sure that the running script gets a proper sys.argv as if it
481 # Make sure that the running script gets a proper sys.argv as if it
482 # were run from a system shell.
482 # were run from a system shell.
483 save_argv = sys.argv # save it for later restoring
483 save_argv = sys.argv # save it for later restoring
484
484
485 # simulate shell expansion on arguments, at least tilde expansion
485 # simulate shell expansion on arguments, at least tilde expansion
486 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
486 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
487
487
488 sys.argv = [filename] + args # put in the proper filename
488 sys.argv = [filename] + args # put in the proper filename
489 # protect sys.argv from potential unicode strings on Python 2:
489 # protect sys.argv from potential unicode strings on Python 2:
490 if not py3compat.PY3:
490 if not py3compat.PY3:
491 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
491 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
492
492
493 if 'i' in opts:
493 if 'i' in opts:
494 # Run in user's interactive namespace
494 # Run in user's interactive namespace
495 prog_ns = self.shell.user_ns
495 prog_ns = self.shell.user_ns
496 __name__save = self.shell.user_ns['__name__']
496 __name__save = self.shell.user_ns['__name__']
497 prog_ns['__name__'] = '__main__'
497 prog_ns['__name__'] = '__main__'
498 main_mod = self.shell.new_main_mod(prog_ns)
498 main_mod = self.shell.new_main_mod(prog_ns)
499 else:
499 else:
500 # Run in a fresh, empty namespace
500 # Run in a fresh, empty namespace
501 if 'n' in opts:
501 if 'n' in opts:
502 name = os.path.splitext(os.path.basename(filename))[0]
502 name = os.path.splitext(os.path.basename(filename))[0]
503 else:
503 else:
504 name = '__main__'
504 name = '__main__'
505
505
506 main_mod = self.shell.new_main_mod()
506 main_mod = self.shell.new_main_mod()
507 prog_ns = main_mod.__dict__
507 prog_ns = main_mod.__dict__
508 prog_ns['__name__'] = name
508 prog_ns['__name__'] = name
509
509
510 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
510 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
511 # set the __file__ global in the script's namespace
511 # set the __file__ global in the script's namespace
512 prog_ns['__file__'] = filename
512 prog_ns['__file__'] = filename
513
513
514 # pickle fix. See interactiveshell for an explanation. But we need to
514 # pickle fix. See interactiveshell for an explanation. But we need to
515 # make sure that, if we overwrite __main__, we replace it at the end
515 # make sure that, if we overwrite __main__, we replace it at the end
516 main_mod_name = prog_ns['__name__']
516 main_mod_name = prog_ns['__name__']
517
517
518 if main_mod_name == '__main__':
518 if main_mod_name == '__main__':
519 restore_main = sys.modules['__main__']
519 restore_main = sys.modules['__main__']
520 else:
520 else:
521 restore_main = False
521 restore_main = False
522
522
523 # This needs to be undone at the end to prevent holding references to
523 # This needs to be undone at the end to prevent holding references to
524 # every single object ever created.
524 # every single object ever created.
525 sys.modules[main_mod_name] = main_mod
525 sys.modules[main_mod_name] = main_mod
526
526
527 try:
527 try:
528 stats = None
528 stats = None
529 with self.shell.readline_no_record:
529 with self.shell.readline_no_record:
530 if 'p' in opts:
530 if 'p' in opts:
531 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
531 stats = self.prun('', None, False, opts, arg_lst, prog_ns)
532 else:
532 else:
533 if 'd' in opts:
533 if 'd' in opts:
534 deb = debugger.Pdb(self.shell.colors)
534 deb = debugger.Pdb(self.shell.colors)
535 # reset Breakpoint state, which is moronically kept
535 # reset Breakpoint state, which is moronically kept
536 # in a class
536 # in a class
537 bdb.Breakpoint.next = 1
537 bdb.Breakpoint.next = 1
538 bdb.Breakpoint.bplist = {}
538 bdb.Breakpoint.bplist = {}
539 bdb.Breakpoint.bpbynumber = [None]
539 bdb.Breakpoint.bpbynumber = [None]
540 # Set an initial breakpoint to stop execution
540 # Set an initial breakpoint to stop execution
541 maxtries = 10
541 maxtries = 10
542 bp = int(opts.get('b', [1])[0])
542 bp = int(opts.get('b', [1])[0])
543 checkline = deb.checkline(filename, bp)
543 checkline = deb.checkline(filename, bp)
544 if not checkline:
544 if not checkline:
545 for bp in range(bp + 1, bp + maxtries + 1):
545 for bp in range(bp + 1, bp + maxtries + 1):
546 if deb.checkline(filename, bp):
546 if deb.checkline(filename, bp):
547 break
547 break
548 else:
548 else:
549 msg = ("\nI failed to find a valid line to set "
549 msg = ("\nI failed to find a valid line to set "
550 "a breakpoint\n"
550 "a breakpoint\n"
551 "after trying up to line: %s.\n"
551 "after trying up to line: %s.\n"
552 "Please set a valid breakpoint manually "
552 "Please set a valid breakpoint manually "
553 "with the -b option." % bp)
553 "with the -b option." % bp)
554 error(msg)
554 error(msg)
555 return
555 return
556 # if we find a good linenumber, set the breakpoint
556 # if we find a good linenumber, set the breakpoint
557 deb.do_break('%s:%s' % (filename, bp))
557 deb.do_break('%s:%s' % (filename, bp))
558 # Start file run
558 # Start file run
559 print "NOTE: Enter 'c' at the",
559 print "NOTE: Enter 'c' at the",
560 print "%s prompt to start your script." % deb.prompt
560 print "%s prompt to start your script." % deb.prompt
561 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
561 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
562 try:
562 try:
563 deb.run('execfile("%s", prog_ns)' % filename, ns)
563 deb.run('execfile("%s", prog_ns)' % filename, ns)
564
564
565 except:
565 except:
566 etype, value, tb = sys.exc_info()
566 etype, value, tb = sys.exc_info()
567 # Skip three frames in the traceback: the %run one,
567 # Skip three frames in the traceback: the %run one,
568 # one inside bdb.py, and the command-line typed by the
568 # one inside bdb.py, and the command-line typed by the
569 # user (run by exec in pdb itself).
569 # user (run by exec in pdb itself).
570 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
570 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
571 else:
571 else:
572 if runner is None:
572 if runner is None:
573 runner = self.default_runner
573 runner = self.default_runner
574 if runner is None:
574 if runner is None:
575 runner = self.shell.safe_execfile
575 runner = self.shell.safe_execfile
576 if 't' in opts:
576 if 't' in opts:
577 # timed execution
577 # timed execution
578 try:
578 try:
579 nruns = int(opts['N'][0])
579 nruns = int(opts['N'][0])
580 if nruns < 1:
580 if nruns < 1:
581 error('Number of runs must be >=1')
581 error('Number of runs must be >=1')
582 return
582 return
583 except (KeyError):
583 except (KeyError):
584 nruns = 1
584 nruns = 1
585 twall0 = time.time()
585 twall0 = time.time()
586 if nruns == 1:
586 if nruns == 1:
587 t0 = clock2()
587 t0 = clock2()
588 runner(filename, prog_ns, prog_ns,
588 runner(filename, prog_ns, prog_ns,
589 exit_ignore=exit_ignore)
589 exit_ignore=exit_ignore)
590 t1 = clock2()
590 t1 = clock2()
591 t_usr = t1[0] - t0[0]
591 t_usr = t1[0] - t0[0]
592 t_sys = t1[1] - t0[1]
592 t_sys = t1[1] - t0[1]
593 print "\nIPython CPU timings (estimated):"
593 print "\nIPython CPU timings (estimated):"
594 print " User : %10.2f s." % t_usr
594 print " User : %10.2f s." % t_usr
595 print " System : %10.2f s." % t_sys
595 print " System : %10.2f s." % t_sys
596 else:
596 else:
597 runs = range(nruns)
597 runs = range(nruns)
598 t0 = clock2()
598 t0 = clock2()
599 for nr in runs:
599 for nr in runs:
600 runner(filename, prog_ns, prog_ns,
600 runner(filename, prog_ns, prog_ns,
601 exit_ignore=exit_ignore)
601 exit_ignore=exit_ignore)
602 t1 = clock2()
602 t1 = clock2()
603 t_usr = t1[0] - t0[0]
603 t_usr = t1[0] - t0[0]
604 t_sys = t1[1] - t0[1]
604 t_sys = t1[1] - t0[1]
605 print "\nIPython CPU timings (estimated):"
605 print "\nIPython CPU timings (estimated):"
606 print "Total runs performed:", nruns
606 print "Total runs performed:", nruns
607 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
607 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
608 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
608 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
609 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
609 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
610 twall1 = time.time()
610 twall1 = time.time()
611 print "Wall time: %10.2f s." % (twall1 - twall0)
611 print "Wall time: %10.2f s." % (twall1 - twall0)
612
612
613 else:
613 else:
614 # regular execution
614 # regular execution
615 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
615 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
616
616
617 if 'i' in opts:
617 if 'i' in opts:
618 self.shell.user_ns['__name__'] = __name__save
618 self.shell.user_ns['__name__'] = __name__save
619 else:
619 else:
620 # The shell MUST hold a reference to prog_ns so after %run
620 # The shell MUST hold a reference to prog_ns so after %run
621 # exits, the python deletion mechanism doesn't zero it out
621 # exits, the python deletion mechanism doesn't zero it out
622 # (leaving dangling references).
622 # (leaving dangling references).
623 self.shell.cache_main_mod(prog_ns, filename)
623 self.shell.cache_main_mod(prog_ns, filename)
624 # update IPython interactive namespace
624 # update IPython interactive namespace
625
625
626 # Some forms of read errors on the file may mean the
626 # Some forms of read errors on the file may mean the
627 # __name__ key was never set; using pop we don't have to
627 # __name__ key was never set; using pop we don't have to
628 # worry about a possible KeyError.
628 # worry about a possible KeyError.
629 prog_ns.pop('__name__', None)
629 prog_ns.pop('__name__', None)
630
630
631 self.shell.user_ns.update(prog_ns)
631 self.shell.user_ns.update(prog_ns)
632 finally:
632 finally:
633 # It's a bit of a mystery why, but __builtins__ can change from
633 # It's a bit of a mystery why, but __builtins__ can change from
634 # being a module to becoming a dict missing some key data after
634 # being a module to becoming a dict missing some key data after
635 # %run. As best I can see, this is NOT something IPython is doing
635 # %run. As best I can see, this is NOT something IPython is doing
636 # at all, and similar problems have been reported before:
636 # at all, and similar problems have been reported before:
637 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
637 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
638 # Since this seems to be done by the interpreter itself, the best
638 # Since this seems to be done by the interpreter itself, the best
639 # we can do is to at least restore __builtins__ for the user on
639 # we can do is to at least restore __builtins__ for the user on
640 # exit.
640 # exit.
641 self.shell.user_ns['__builtins__'] = builtin_mod
641 self.shell.user_ns['__builtins__'] = builtin_mod
642
642
643 # Ensure key global structures are restored
643 # Ensure key global structures are restored
644 sys.argv = save_argv
644 sys.argv = save_argv
645 if restore_main:
645 if restore_main:
646 sys.modules['__main__'] = restore_main
646 sys.modules['__main__'] = restore_main
647 else:
647 else:
648 # Remove from sys.modules the reference to main_mod we'd
648 # Remove from sys.modules the reference to main_mod we'd
649 # added. Otherwise it will trap references to objects
649 # added. Otherwise it will trap references to objects
650 # contained therein.
650 # contained therein.
651 del sys.modules[main_mod_name]
651 del sys.modules[main_mod_name]
652
652
653 return stats
653 return stats
654
654
655 @skip_doctest
655 @skip_doctest
656 @line_cell_magic
656 @line_cell_magic
657 def timeit(self, line='', cell=None):
657 def timeit(self, line='', cell=None):
658 """Time execution of a Python statement or expression
658 """Time execution of a Python statement or expression
659
659
660 Usage, in line mode:
660 Usage, in line mode:
661 %timeit [-n<N> -r<R> [-t|-c]] statement
661 %timeit [-n<N> -r<R> [-t|-c]] statement
662 or in cell mode:
662 or in cell mode:
663 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
663 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
664 code
664 code
665 code...
665 code...
666
666
667 Time execution of a Python statement or expression using the timeit
667 Time execution of a Python statement or expression using the timeit
668 module. This function can be used both as a line and cell magic:
668 module. This function can be used both as a line and cell magic:
669
669
670 - In line mode you can time a single-line statement (though multiple
670 - In line mode you can time a single-line statement (though multiple
671 ones can be chained with using semicolons).
671 ones can be chained with using semicolons).
672
672
673 - In cell mode, the statement in the first line is used as setup code
673 - In cell mode, the statement in the first line is used as setup code
674 (executed but not timed) and the body of the cell is timed. The cell
674 (executed but not timed) and the body of the cell is timed. The cell
675 body has access to any variables created in the setup code.
675 body has access to any variables created in the setup code.
676
676
677 Options:
677 Options:
678 -n<N>: execute the given statement <N> times in a loop. If this value
678 -n<N>: execute the given statement <N> times in a loop. If this value
679 is not given, a fitting value is chosen.
679 is not given, a fitting value is chosen.
680
680
681 -r<R>: repeat the loop iteration <R> times and take the best result.
681 -r<R>: repeat the loop iteration <R> times and take the best result.
682 Default: 3
682 Default: 3
683
683
684 -t: use time.time to measure the time, which is the default on Unix.
684 -t: use time.time to measure the time, which is the default on Unix.
685 This function measures wall time.
685 This function measures wall time.
686
686
687 -c: use time.clock to measure the time, which is the default on
687 -c: use time.clock to measure the time, which is the default on
688 Windows and measures wall time. On Unix, resource.getrusage is used
688 Windows and measures wall time. On Unix, resource.getrusage is used
689 instead and returns the CPU user time.
689 instead and returns the CPU user time.
690
690
691 -p<P>: use a precision of <P> digits to display the timing result.
691 -p<P>: use a precision of <P> digits to display the timing result.
692 Default: 3
692 Default: 3
693
693
694
694
695 Examples
695 Examples
696 --------
696 --------
697 ::
697 ::
698
698
699 In [1]: %timeit pass
699 In [1]: %timeit pass
700 10000000 loops, best of 3: 53.3 ns per loop
700 10000000 loops, best of 3: 53.3 ns per loop
701
701
702 In [2]: u = None
702 In [2]: u = None
703
703
704 In [3]: %timeit u is None
704 In [3]: %timeit u is None
705 10000000 loops, best of 3: 184 ns per loop
705 10000000 loops, best of 3: 184 ns per loop
706
706
707 In [4]: %timeit -r 4 u == None
707 In [4]: %timeit -r 4 u == None
708 1000000 loops, best of 4: 242 ns per loop
708 1000000 loops, best of 4: 242 ns per loop
709
709
710 In [5]: import time
710 In [5]: import time
711
711
712 In [6]: %timeit -n1 time.sleep(2)
712 In [6]: %timeit -n1 time.sleep(2)
713 1 loops, best of 3: 2 s per loop
713 1 loops, best of 3: 2 s per loop
714
714
715
715
716 The times reported by %timeit will be slightly higher than those
716 The times reported by %timeit will be slightly higher than those
717 reported by the timeit.py script when variables are accessed. This is
717 reported by the timeit.py script when variables are accessed. This is
718 due to the fact that %timeit executes the statement in the namespace
718 due to the fact that %timeit executes the statement in the namespace
719 of the shell, compared with timeit.py, which uses a single setup
719 of the shell, compared with timeit.py, which uses a single setup
720 statement to import function or create variables. Generally, the bias
720 statement to import function or create variables. Generally, the bias
721 does not matter as long as results from timeit.py are not mixed with
721 does not matter as long as results from timeit.py are not mixed with
722 those from %timeit."""
722 those from %timeit."""
723
723
724 import timeit
724 import timeit
725 import math
725 import math
726
726
727 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
727 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
728 # certain terminals. Until we figure out a robust way of
728 # certain terminals. Until we figure out a robust way of
729 # auto-detecting if the terminal can deal with it, use plain 'us' for
729 # auto-detecting if the terminal can deal with it, use plain 'us' for
730 # microseconds. I am really NOT happy about disabling the proper
730 # microseconds. I am really NOT happy about disabling the proper
731 # 'micro' prefix, but crashing is worse... If anyone knows what the
731 # 'micro' prefix, but crashing is worse... If anyone knows what the
732 # right solution for this is, I'm all ears...
732 # right solution for this is, I'm all ears...
733 #
733 #
734 # Note: using
734 # Note: using
735 #
735 #
736 # s = u'\xb5'
736 # s = u'\xb5'
737 # s.encode(sys.getdefaultencoding())
737 # s.encode(sys.getdefaultencoding())
738 #
738 #
739 # is not sufficient, as I've seen terminals where that fails but
739 # is not sufficient, as I've seen terminals where that fails but
740 # print s
740 # print s
741 #
741 #
742 # succeeds
742 # succeeds
743 #
743 #
744 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
744 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
745
745
746 #units = [u"s", u"ms",u'\xb5',"ns"]
746 #units = [u"s", u"ms",u'\xb5',"ns"]
747 units = [u"s", u"ms",u'us',"ns"]
747 units = [u"s", u"ms",u'us',"ns"]
748
748
749 scaling = [1, 1e3, 1e6, 1e9]
749 scaling = [1, 1e3, 1e6, 1e9]
750
750
751 opts, stmt = self.parse_options(line,'n:r:tcp:',
751 opts, stmt = self.parse_options(line,'n:r:tcp:',
752 posix=False, strict=False)
752 posix=False, strict=False)
753 if stmt == "":
753 if stmt == "":
754 return
754 return
755 timefunc = timeit.default_timer
755 timefunc = timeit.default_timer
756 number = int(getattr(opts, "n", 0))
756 number = int(getattr(opts, "n", 0))
757 repeat = int(getattr(opts, "r", timeit.default_repeat))
757 repeat = int(getattr(opts, "r", timeit.default_repeat))
758 precision = int(getattr(opts, "p", 3))
758 precision = int(getattr(opts, "p", 3))
759 if hasattr(opts, "t"):
759 if hasattr(opts, "t"):
760 timefunc = time.time
760 timefunc = time.time
761 if hasattr(opts, "c"):
761 if hasattr(opts, "c"):
762 timefunc = clock
762 timefunc = clock
763
763
764 timer = timeit.Timer(timer=timefunc)
764 timer = timeit.Timer(timer=timefunc)
765 # this code has tight coupling to the inner workings of timeit.Timer,
765 # this code has tight coupling to the inner workings of timeit.Timer,
766 # but is there a better way to achieve that the code stmt has access
766 # but is there a better way to achieve that the code stmt has access
767 # to the shell namespace?
767 # to the shell namespace?
768
768
769 if cell is None:
769 if cell is None:
770 # called as line magic
770 # called as line magic
771 setup = 'pass'
771 setup = 'pass'
772 stmt = timeit.reindent(stmt, 8)
772 stmt = timeit.reindent(stmt, 8)
773 else:
773 else:
774 setup = timeit.reindent(stmt, 4)
774 setup = timeit.reindent(stmt, 4)
775 stmt = timeit.reindent(cell, 8)
775 stmt = timeit.reindent(cell, 8)
776
776
777 src = timeit.template % dict(stmt=stmt, setup=setup)
777 # From Python 3.3, this template uses new-style string formatting.
778 if sys.version_info >= (3, 3):
779 src = timeit.template.format(stmt=stmt, setup=setup)
780 else:
781 src = timeit.template % dict(stmt=stmt, setup=setup)
782
778 # Track compilation time so it can be reported if too long
783 # Track compilation time so it can be reported if too long
779 # Minimum time above which compilation time will be reported
784 # Minimum time above which compilation time will be reported
780 tc_min = 0.1
785 tc_min = 0.1
781
786
782 t0 = clock()
787 t0 = clock()
783 code = compile(src, "<magic-timeit>", "exec")
788 code = compile(src, "<magic-timeit>", "exec")
784 tc = clock()-t0
789 tc = clock()-t0
785
790
786 ns = {}
791 ns = {}
787 exec code in self.shell.user_ns, ns
792 exec code in self.shell.user_ns, ns
788 timer.inner = ns["inner"]
793 timer.inner = ns["inner"]
789
794
790 if number == 0:
795 if number == 0:
791 # determine number so that 0.2 <= total time < 2.0
796 # determine number so that 0.2 <= total time < 2.0
792 number = 1
797 number = 1
793 for i in range(1, 10):
798 for i in range(1, 10):
794 if timer.timeit(number) >= 0.2:
799 if timer.timeit(number) >= 0.2:
795 break
800 break
796 number *= 10
801 number *= 10
797
802
798 best = min(timer.repeat(repeat, number)) / number
803 best = min(timer.repeat(repeat, number)) / number
799
804
800 if best > 0.0 and best < 1000.0:
805 if best > 0.0 and best < 1000.0:
801 order = min(-int(math.floor(math.log10(best)) // 3), 3)
806 order = min(-int(math.floor(math.log10(best)) // 3), 3)
802 elif best >= 1000.0:
807 elif best >= 1000.0:
803 order = 0
808 order = 0
804 else:
809 else:
805 order = 3
810 order = 3
806 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
811 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
807 precision,
812 precision,
808 best * scaling[order],
813 best * scaling[order],
809 units[order])
814 units[order])
810 if tc > tc_min:
815 if tc > tc_min:
811 print "Compiler time: %.2f s" % tc
816 print "Compiler time: %.2f s" % tc
812
817
813 @skip_doctest
818 @skip_doctest
814 @needs_local_scope
819 @needs_local_scope
815 @line_magic
820 @line_magic
816 def time(self,parameter_s, user_locals):
821 def time(self,parameter_s, user_locals):
817 """Time execution of a Python statement or expression.
822 """Time execution of a Python statement or expression.
818
823
819 The CPU and wall clock times are printed, and the value of the
824 The CPU and wall clock times are printed, and the value of the
820 expression (if any) is returned. Note that under Win32, system time
825 expression (if any) is returned. Note that under Win32, system time
821 is always reported as 0, since it can not be measured.
826 is always reported as 0, since it can not be measured.
822
827
823 This function provides very basic timing functionality. In Python
828 This function provides very basic timing functionality. In Python
824 2.3, the timeit module offers more control and sophistication, so this
829 2.3, the timeit module offers more control and sophistication, so this
825 could be rewritten to use it (patches welcome).
830 could be rewritten to use it (patches welcome).
826
831
827 Examples
832 Examples
828 --------
833 --------
829 ::
834 ::
830
835
831 In [1]: time 2**128
836 In [1]: time 2**128
832 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
837 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
833 Wall time: 0.00
838 Wall time: 0.00
834 Out[1]: 340282366920938463463374607431768211456L
839 Out[1]: 340282366920938463463374607431768211456L
835
840
836 In [2]: n = 1000000
841 In [2]: n = 1000000
837
842
838 In [3]: time sum(range(n))
843 In [3]: time sum(range(n))
839 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
844 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
840 Wall time: 1.37
845 Wall time: 1.37
841 Out[3]: 499999500000L
846 Out[3]: 499999500000L
842
847
843 In [4]: time print 'hello world'
848 In [4]: time print 'hello world'
844 hello world
849 hello world
845 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
850 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
846 Wall time: 0.00
851 Wall time: 0.00
847
852
848 Note that the time needed by Python to compile the given expression
853 Note that the time needed by Python to compile the given expression
849 will be reported if it is more than 0.1s. In this example, the
854 will be reported if it is more than 0.1s. In this example, the
850 actual exponentiation is done by Python at compilation time, so while
855 actual exponentiation is done by Python at compilation time, so while
851 the expression can take a noticeable amount of time to compute, that
856 the expression can take a noticeable amount of time to compute, that
852 time is purely due to the compilation:
857 time is purely due to the compilation:
853
858
854 In [5]: time 3**9999;
859 In [5]: time 3**9999;
855 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
860 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
856 Wall time: 0.00 s
861 Wall time: 0.00 s
857
862
858 In [6]: time 3**999999;
863 In [6]: time 3**999999;
859 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
864 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
860 Wall time: 0.00 s
865 Wall time: 0.00 s
861 Compiler : 0.78 s
866 Compiler : 0.78 s
862 """
867 """
863
868
864 # fail immediately if the given expression can't be compiled
869 # fail immediately if the given expression can't be compiled
865
870
866 expr = self.shell.prefilter(parameter_s,False)
871 expr = self.shell.prefilter(parameter_s,False)
867
872
868 # Minimum time above which compilation time will be reported
873 # Minimum time above which compilation time will be reported
869 tc_min = 0.1
874 tc_min = 0.1
870
875
871 try:
876 try:
872 mode = 'eval'
877 mode = 'eval'
873 t0 = clock()
878 t0 = clock()
874 code = compile(expr,'<timed eval>',mode)
879 code = compile(expr,'<timed eval>',mode)
875 tc = clock()-t0
880 tc = clock()-t0
876 except SyntaxError:
881 except SyntaxError:
877 mode = 'exec'
882 mode = 'exec'
878 t0 = clock()
883 t0 = clock()
879 code = compile(expr,'<timed exec>',mode)
884 code = compile(expr,'<timed exec>',mode)
880 tc = clock()-t0
885 tc = clock()-t0
881 # skew measurement as little as possible
886 # skew measurement as little as possible
882 glob = self.shell.user_ns
887 glob = self.shell.user_ns
883 wtime = time.time
888 wtime = time.time
884 # time execution
889 # time execution
885 wall_st = wtime()
890 wall_st = wtime()
886 if mode=='eval':
891 if mode=='eval':
887 st = clock2()
892 st = clock2()
888 out = eval(code, glob, user_locals)
893 out = eval(code, glob, user_locals)
889 end = clock2()
894 end = clock2()
890 else:
895 else:
891 st = clock2()
896 st = clock2()
892 exec code in glob, user_locals
897 exec code in glob, user_locals
893 end = clock2()
898 end = clock2()
894 out = None
899 out = None
895 wall_end = wtime()
900 wall_end = wtime()
896 # Compute actual times and report
901 # Compute actual times and report
897 wall_time = wall_end-wall_st
902 wall_time = wall_end-wall_st
898 cpu_user = end[0]-st[0]
903 cpu_user = end[0]-st[0]
899 cpu_sys = end[1]-st[1]
904 cpu_sys = end[1]-st[1]
900 cpu_tot = cpu_user+cpu_sys
905 cpu_tot = cpu_user+cpu_sys
901 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
906 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
902 (cpu_user,cpu_sys,cpu_tot)
907 (cpu_user,cpu_sys,cpu_tot)
903 print "Wall time: %.2f s" % wall_time
908 print "Wall time: %.2f s" % wall_time
904 if tc > tc_min:
909 if tc > tc_min:
905 print "Compiler : %.2f s" % tc
910 print "Compiler : %.2f s" % tc
906 return out
911 return out
907
912
908 @skip_doctest
913 @skip_doctest
909 @line_magic
914 @line_magic
910 def macro(self, parameter_s=''):
915 def macro(self, parameter_s=''):
911 """Define a macro for future re-execution. It accepts ranges of history,
916 """Define a macro for future re-execution. It accepts ranges of history,
912 filenames or string objects.
917 filenames or string objects.
913
918
914 Usage:\\
919 Usage:\\
915 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
920 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
916
921
917 Options:
922 Options:
918
923
919 -r: use 'raw' input. By default, the 'processed' history is used,
924 -r: use 'raw' input. By default, the 'processed' history is used,
920 so that magics are loaded in their transformed version to valid
925 so that magics are loaded in their transformed version to valid
921 Python. If this option is given, the raw input as typed as the
926 Python. If this option is given, the raw input as typed as the
922 command line is used instead.
927 command line is used instead.
923
928
924 This will define a global variable called `name` which is a string
929 This will define a global variable called `name` which is a string
925 made of joining the slices and lines you specify (n1,n2,... numbers
930 made of joining the slices and lines you specify (n1,n2,... numbers
926 above) from your input history into a single string. This variable
931 above) from your input history into a single string. This variable
927 acts like an automatic function which re-executes those lines as if
932 acts like an automatic function which re-executes those lines as if
928 you had typed them. You just type 'name' at the prompt and the code
933 you had typed them. You just type 'name' at the prompt and the code
929 executes.
934 executes.
930
935
931 The syntax for indicating input ranges is described in %history.
936 The syntax for indicating input ranges is described in %history.
932
937
933 Note: as a 'hidden' feature, you can also use traditional python slice
938 Note: as a 'hidden' feature, you can also use traditional python slice
934 notation, where N:M means numbers N through M-1.
939 notation, where N:M means numbers N through M-1.
935
940
936 For example, if your history contains (%hist prints it)::
941 For example, if your history contains (%hist prints it)::
937
942
938 44: x=1
943 44: x=1
939 45: y=3
944 45: y=3
940 46: z=x+y
945 46: z=x+y
941 47: print x
946 47: print x
942 48: a=5
947 48: a=5
943 49: print 'x',x,'y',y
948 49: print 'x',x,'y',y
944
949
945 you can create a macro with lines 44 through 47 (included) and line 49
950 you can create a macro with lines 44 through 47 (included) and line 49
946 called my_macro with::
951 called my_macro with::
947
952
948 In [55]: %macro my_macro 44-47 49
953 In [55]: %macro my_macro 44-47 49
949
954
950 Now, typing `my_macro` (without quotes) will re-execute all this code
955 Now, typing `my_macro` (without quotes) will re-execute all this code
951 in one pass.
956 in one pass.
952
957
953 You don't need to give the line-numbers in order, and any given line
958 You don't need to give the line-numbers in order, and any given line
954 number can appear multiple times. You can assemble macros with any
959 number can appear multiple times. You can assemble macros with any
955 lines from your input history in any order.
960 lines from your input history in any order.
956
961
957 The macro is a simple object which holds its value in an attribute,
962 The macro is a simple object which holds its value in an attribute,
958 but IPython's display system checks for macros and executes them as
963 but IPython's display system checks for macros and executes them as
959 code instead of printing them when you type their name.
964 code instead of printing them when you type their name.
960
965
961 You can view a macro's contents by explicitly printing it with::
966 You can view a macro's contents by explicitly printing it with::
962
967
963 print macro_name
968 print macro_name
964
969
965 """
970 """
966 opts,args = self.parse_options(parameter_s,'r',mode='list')
971 opts,args = self.parse_options(parameter_s,'r',mode='list')
967 if not args: # List existing macros
972 if not args: # List existing macros
968 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
973 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
969 isinstance(v, Macro))
974 isinstance(v, Macro))
970 if len(args) == 1:
975 if len(args) == 1:
971 raise UsageError(
976 raise UsageError(
972 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
977 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
973 name, codefrom = args[0], " ".join(args[1:])
978 name, codefrom = args[0], " ".join(args[1:])
974
979
975 #print 'rng',ranges # dbg
980 #print 'rng',ranges # dbg
976 try:
981 try:
977 lines = self.shell.find_user_code(codefrom, 'r' in opts)
982 lines = self.shell.find_user_code(codefrom, 'r' in opts)
978 except (ValueError, TypeError) as e:
983 except (ValueError, TypeError) as e:
979 print e.args[0]
984 print e.args[0]
980 return
985 return
981 macro = Macro(lines)
986 macro = Macro(lines)
982 self.shell.define_macro(name, macro)
987 self.shell.define_macro(name, macro)
983 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
988 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
984 print '=== Macro contents: ==='
989 print '=== Macro contents: ==='
985 print macro,
990 print macro,
General Comments 0
You need to be logged in to leave comments. Login now