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