##// END OF EJS Templates
Save worst time as well:...
Ivan Pozdeev -
Show More
@@ -1,1351 +1,1352 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Implementation of execution-related magic functions."""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 from __future__ import print_function
8 8
9 9 import ast
10 10 import bdb
11 11 import gc
12 12 import itertools
13 13 import os
14 14 import sys
15 15 import time
16 16 import timeit
17 17 from pdb import Restart
18 18
19 19 # cProfile was added in Python2.5
20 20 try:
21 21 import cProfile as profile
22 22 import pstats
23 23 except ImportError:
24 24 # profile isn't bundled by default in Debian for license reasons
25 25 try:
26 26 import profile, pstats
27 27 except ImportError:
28 28 profile = pstats = None
29 29
30 30 from IPython.core import debugger, oinspect
31 31 from IPython.core import magic_arguments
32 32 from IPython.core import page
33 33 from IPython.core.error import UsageError
34 34 from IPython.core.macro import Macro
35 35 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
36 36 line_cell_magic, on_off, needs_local_scope)
37 37 from IPython.testing.skipdoctest import skip_doctest
38 38 from IPython.utils import py3compat
39 39 from IPython.utils.py3compat import builtin_mod, iteritems, PY3
40 40 from IPython.utils.contexts import preserve_keys
41 41 from IPython.utils.capture import capture_output
42 42 from IPython.utils.ipstruct import Struct
43 43 from IPython.utils.module_paths import find_mod
44 44 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
45 45 from IPython.utils.timing import clock, clock2
46 46 from IPython.utils.warn import warn, error
47 47
48 48 if PY3:
49 49 from io import StringIO
50 50 else:
51 51 from StringIO import StringIO
52 52
53 53 #-----------------------------------------------------------------------------
54 54 # Magic implementation classes
55 55 #-----------------------------------------------------------------------------
56 56
57 57
58 58 class TimeitResult(object):
59 59 """
60 60 Object returned by the timeit magic with info about the run.
61 61
62 62 Contain the following attributes :
63 63
64 64 loops: (int) number of loop done per measurement
65 65 repeat: (int) number of time the mesurement has been repeated
66 66 best: (float) best execusion time / number
67 67 all_runs: (list of float) execusion time of each run (in s)
68 68 compile_time: (float) time of statement compilation (s)
69 69
70 70 """
71 71
72 def __init__(self, loops, repeat, best, all_runs, compile_time, precision):
72 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
73 73 self.loops = loops
74 74 self.repeat = repeat
75 75 self.best = best
76 self.worst = worst
76 77 self.all_runs = all_runs
77 78 self.compile_time = compile_time
78 79 self._precision = precision
79 80
80 81 def _repr_pretty_(self, p , cycle):
81 82 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
82 83 _format_time(self.best, self._precision))
83 84 p.text(u'<TimeitResult : '+unic+u'>')
84 85
85 86
86 87 class TimeitTemplateFiller(ast.NodeTransformer):
87 88 """Fill in the AST template for timing execution.
88 89
89 90 This is quite closely tied to the template definition, which is in
90 91 :meth:`ExecutionMagics.timeit`.
91 92 """
92 93 def __init__(self, ast_setup, ast_stmt):
93 94 self.ast_setup = ast_setup
94 95 self.ast_stmt = ast_stmt
95 96
96 97 def visit_FunctionDef(self, node):
97 98 "Fill in the setup statement"
98 99 self.generic_visit(node)
99 100 if node.name == "inner":
100 101 node.body[:1] = self.ast_setup.body
101 102
102 103 return node
103 104
104 105 def visit_For(self, node):
105 106 "Fill in the statement to be timed"
106 107 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
107 108 node.body = self.ast_stmt.body
108 109 return node
109 110
110 111
111 112 class Timer(timeit.Timer):
112 113 """Timer class that explicitly uses self.inner
113 114
114 115 which is an undocumented implementation detail of CPython,
115 116 not shared by PyPy.
116 117 """
117 118 # Timer.timeit copied from CPython 3.4.2
118 119 def timeit(self, number=timeit.default_number):
119 120 """Time 'number' executions of the main statement.
120 121
121 122 To be precise, this executes the setup statement once, and
122 123 then returns the time it takes to execute the main statement
123 124 a number of times, as a float measured in seconds. The
124 125 argument is the number of times through the loop, defaulting
125 126 to one million. The main statement, the setup statement and
126 127 the timer function to be used are passed to the constructor.
127 128 """
128 129 it = itertools.repeat(None, number)
129 130 gcold = gc.isenabled()
130 131 gc.disable()
131 132 try:
132 133 timing = self.inner(it, self.timer)
133 134 finally:
134 135 if gcold:
135 136 gc.enable()
136 137 return timing
137 138
138 139
139 140 @magics_class
140 141 class ExecutionMagics(Magics):
141 142 """Magics related to code execution, debugging, profiling, etc.
142 143
143 144 """
144 145
145 146 def __init__(self, shell):
146 147 super(ExecutionMagics, self).__init__(shell)
147 148 if profile is None:
148 149 self.prun = self.profile_missing_notice
149 150 # Default execution function used to actually run user code.
150 151 self.default_runner = None
151 152
152 153 def profile_missing_notice(self, *args, **kwargs):
153 154 error("""\
154 155 The profile module could not be found. It has been removed from the standard
155 156 python packages because of its non-free license. To use profiling, install the
156 157 python-profiler package from non-free.""")
157 158
158 159 @skip_doctest
159 160 @line_cell_magic
160 161 def prun(self, parameter_s='', cell=None):
161 162
162 163 """Run a statement through the python code profiler.
163 164
164 165 Usage, in line mode:
165 166 %prun [options] statement
166 167
167 168 Usage, in cell mode:
168 169 %%prun [options] [statement]
169 170 code...
170 171 code...
171 172
172 173 In cell mode, the additional code lines are appended to the (possibly
173 174 empty) statement in the first line. Cell mode allows you to easily
174 175 profile multiline blocks without having to put them in a separate
175 176 function.
176 177
177 178 The given statement (which doesn't require quote marks) is run via the
178 179 python profiler in a manner similar to the profile.run() function.
179 180 Namespaces are internally managed to work correctly; profile.run
180 181 cannot be used in IPython because it makes certain assumptions about
181 182 namespaces which do not hold under IPython.
182 183
183 184 Options:
184 185
185 186 -l <limit>
186 187 you can place restrictions on what or how much of the
187 188 profile gets printed. The limit value can be:
188 189
189 190 * A string: only information for function names containing this string
190 191 is printed.
191 192
192 193 * An integer: only these many lines are printed.
193 194
194 195 * A float (between 0 and 1): this fraction of the report is printed
195 196 (for example, use a limit of 0.4 to see the topmost 40% only).
196 197
197 198 You can combine several limits with repeated use of the option. For
198 199 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
199 200 information about class constructors.
200 201
201 202 -r
202 203 return the pstats.Stats object generated by the profiling. This
203 204 object has all the information about the profile in it, and you can
204 205 later use it for further analysis or in other functions.
205 206
206 207 -s <key>
207 208 sort profile by given key. You can provide more than one key
208 209 by using the option several times: '-s key1 -s key2 -s key3...'. The
209 210 default sorting key is 'time'.
210 211
211 212 The following is copied verbatim from the profile documentation
212 213 referenced below:
213 214
214 215 When more than one key is provided, additional keys are used as
215 216 secondary criteria when the there is equality in all keys selected
216 217 before them.
217 218
218 219 Abbreviations can be used for any key names, as long as the
219 220 abbreviation is unambiguous. The following are the keys currently
220 221 defined:
221 222
222 223 ============ =====================
223 224 Valid Arg Meaning
224 225 ============ =====================
225 226 "calls" call count
226 227 "cumulative" cumulative time
227 228 "file" file name
228 229 "module" file name
229 230 "pcalls" primitive call count
230 231 "line" line number
231 232 "name" function name
232 233 "nfl" name/file/line
233 234 "stdname" standard name
234 235 "time" internal time
235 236 ============ =====================
236 237
237 238 Note that all sorts on statistics are in descending order (placing
238 239 most time consuming items first), where as name, file, and line number
239 240 searches are in ascending order (i.e., alphabetical). The subtle
240 241 distinction between "nfl" and "stdname" is that the standard name is a
241 242 sort of the name as printed, which means that the embedded line
242 243 numbers get compared in an odd way. For example, lines 3, 20, and 40
243 244 would (if the file names were the same) appear in the string order
244 245 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
245 246 line numbers. In fact, sort_stats("nfl") is the same as
246 247 sort_stats("name", "file", "line").
247 248
248 249 -T <filename>
249 250 save profile results as shown on screen to a text
250 251 file. The profile is still shown on screen.
251 252
252 253 -D <filename>
253 254 save (via dump_stats) profile statistics to given
254 255 filename. This data is in a format understood by the pstats module, and
255 256 is generated by a call to the dump_stats() method of profile
256 257 objects. The profile is still shown on screen.
257 258
258 259 -q
259 260 suppress output to the pager. Best used with -T and/or -D above.
260 261
261 262 If you want to run complete programs under the profiler's control, use
262 263 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
263 264 contains profiler specific options as described here.
264 265
265 266 You can read the complete documentation for the profile module with::
266 267
267 268 In [1]: import profile; profile.help()
268 269 """
269 270 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
270 271 list_all=True, posix=False)
271 272 if cell is not None:
272 273 arg_str += '\n' + cell
273 274 arg_str = self.shell.input_splitter.transform_cell(arg_str)
274 275 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
275 276
276 277 def _run_with_profiler(self, code, opts, namespace):
277 278 """
278 279 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
279 280
280 281 Parameters
281 282 ----------
282 283 code : str
283 284 Code to be executed.
284 285 opts : Struct
285 286 Options parsed by `self.parse_options`.
286 287 namespace : dict
287 288 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
288 289
289 290 """
290 291
291 292 # Fill default values for unspecified options:
292 293 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
293 294
294 295 prof = profile.Profile()
295 296 try:
296 297 prof = prof.runctx(code, namespace, namespace)
297 298 sys_exit = ''
298 299 except SystemExit:
299 300 sys_exit = """*** SystemExit exception caught in code being profiled."""
300 301
301 302 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
302 303
303 304 lims = opts.l
304 305 if lims:
305 306 lims = [] # rebuild lims with ints/floats/strings
306 307 for lim in opts.l:
307 308 try:
308 309 lims.append(int(lim))
309 310 except ValueError:
310 311 try:
311 312 lims.append(float(lim))
312 313 except ValueError:
313 314 lims.append(lim)
314 315
315 316 # Trap output.
316 317 stdout_trap = StringIO()
317 318 stats_stream = stats.stream
318 319 try:
319 320 stats.stream = stdout_trap
320 321 stats.print_stats(*lims)
321 322 finally:
322 323 stats.stream = stats_stream
323 324
324 325 output = stdout_trap.getvalue()
325 326 output = output.rstrip()
326 327
327 328 if 'q' not in opts:
328 329 page.page(output)
329 330 print(sys_exit, end=' ')
330 331
331 332 dump_file = opts.D[0]
332 333 text_file = opts.T[0]
333 334 if dump_file:
334 335 dump_file = unquote_filename(dump_file)
335 336 prof.dump_stats(dump_file)
336 337 print('\n*** Profile stats marshalled to file',\
337 338 repr(dump_file)+'.',sys_exit)
338 339 if text_file:
339 340 text_file = unquote_filename(text_file)
340 341 pfile = open(text_file,'w')
341 342 pfile.write(output)
342 343 pfile.close()
343 344 print('\n*** Profile printout saved to text file',\
344 345 repr(text_file)+'.',sys_exit)
345 346
346 347 if 'r' in opts:
347 348 return stats
348 349 else:
349 350 return None
350 351
351 352 @line_magic
352 353 def pdb(self, parameter_s=''):
353 354 """Control the automatic calling of the pdb interactive debugger.
354 355
355 356 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
356 357 argument it works as a toggle.
357 358
358 359 When an exception is triggered, IPython can optionally call the
359 360 interactive pdb debugger after the traceback printout. %pdb toggles
360 361 this feature on and off.
361 362
362 363 The initial state of this feature is set in your configuration
363 364 file (the option is ``InteractiveShell.pdb``).
364 365
365 366 If you want to just activate the debugger AFTER an exception has fired,
366 367 without having to type '%pdb on' and rerunning your code, you can use
367 368 the %debug magic."""
368 369
369 370 par = parameter_s.strip().lower()
370 371
371 372 if par:
372 373 try:
373 374 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
374 375 except KeyError:
375 376 print ('Incorrect argument. Use on/1, off/0, '
376 377 'or nothing for a toggle.')
377 378 return
378 379 else:
379 380 # toggle
380 381 new_pdb = not self.shell.call_pdb
381 382
382 383 # set on the shell
383 384 self.shell.call_pdb = new_pdb
384 385 print('Automatic pdb calling has been turned',on_off(new_pdb))
385 386
386 387 @skip_doctest
387 388 @magic_arguments.magic_arguments()
388 389 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
389 390 help="""
390 391 Set break point at LINE in FILE.
391 392 """
392 393 )
393 394 @magic_arguments.argument('statement', nargs='*',
394 395 help="""
395 396 Code to run in debugger.
396 397 You can omit this in cell magic mode.
397 398 """
398 399 )
399 400 @line_cell_magic
400 401 def debug(self, line='', cell=None):
401 402 """Activate the interactive debugger.
402 403
403 404 This magic command support two ways of activating debugger.
404 405 One is to activate debugger before executing code. This way, you
405 406 can set a break point, to step through the code from the point.
406 407 You can use this mode by giving statements to execute and optionally
407 408 a breakpoint.
408 409
409 410 The other one is to activate debugger in post-mortem mode. You can
410 411 activate this mode simply running %debug without any argument.
411 412 If an exception has just occurred, this lets you inspect its stack
412 413 frames interactively. Note that this will always work only on the last
413 414 traceback that occurred, so you must call this quickly after an
414 415 exception that you wish to inspect has fired, because if another one
415 416 occurs, it clobbers the previous one.
416 417
417 418 If you want IPython to automatically do this on every exception, see
418 419 the %pdb magic for more details.
419 420 """
420 421 args = magic_arguments.parse_argstring(self.debug, line)
421 422
422 423 if not (args.breakpoint or args.statement or cell):
423 424 self._debug_post_mortem()
424 425 else:
425 426 code = "\n".join(args.statement)
426 427 if cell:
427 428 code += "\n" + cell
428 429 self._debug_exec(code, args.breakpoint)
429 430
430 431 def _debug_post_mortem(self):
431 432 self.shell.debugger(force=True)
432 433
433 434 def _debug_exec(self, code, breakpoint):
434 435 if breakpoint:
435 436 (filename, bp_line) = breakpoint.split(':', 1)
436 437 bp_line = int(bp_line)
437 438 else:
438 439 (filename, bp_line) = (None, None)
439 440 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
440 441
441 442 @line_magic
442 443 def tb(self, s):
443 444 """Print the last traceback with the currently active exception mode.
444 445
445 446 See %xmode for changing exception reporting modes."""
446 447 self.shell.showtraceback()
447 448
448 449 @skip_doctest
449 450 @line_magic
450 451 def run(self, parameter_s='', runner=None,
451 452 file_finder=get_py_filename):
452 453 """Run the named file inside IPython as a program.
453 454
454 455 Usage::
455 456
456 457 %run [-n -i -e -G]
457 458 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
458 459 ( -m mod | file ) [args]
459 460
460 461 Parameters after the filename are passed as command-line arguments to
461 462 the program (put in sys.argv). Then, control returns to IPython's
462 463 prompt.
463 464
464 465 This is similar to running at a system prompt ``python file args``,
465 466 but with the advantage of giving you IPython's tracebacks, and of
466 467 loading all variables into your interactive namespace for further use
467 468 (unless -p is used, see below).
468 469
469 470 The file is executed in a namespace initially consisting only of
470 471 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
471 472 sees its environment as if it were being run as a stand-alone program
472 473 (except for sharing global objects such as previously imported
473 474 modules). But after execution, the IPython interactive namespace gets
474 475 updated with all variables defined in the program (except for __name__
475 476 and sys.argv). This allows for very convenient loading of code for
476 477 interactive work, while giving each program a 'clean sheet' to run in.
477 478
478 479 Arguments are expanded using shell-like glob match. Patterns
479 480 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
480 481 tilde '~' will be expanded into user's home directory. Unlike
481 482 real shells, quotation does not suppress expansions. Use
482 483 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
483 484 To completely disable these expansions, you can use -G flag.
484 485
485 486 Options:
486 487
487 488 -n
488 489 __name__ is NOT set to '__main__', but to the running file's name
489 490 without extension (as python does under import). This allows running
490 491 scripts and reloading the definitions in them without calling code
491 492 protected by an ``if __name__ == "__main__"`` clause.
492 493
493 494 -i
494 495 run the file in IPython's namespace instead of an empty one. This
495 496 is useful if you are experimenting with code written in a text editor
496 497 which depends on variables defined interactively.
497 498
498 499 -e
499 500 ignore sys.exit() calls or SystemExit exceptions in the script
500 501 being run. This is particularly useful if IPython is being used to
501 502 run unittests, which always exit with a sys.exit() call. In such
502 503 cases you are interested in the output of the test results, not in
503 504 seeing a traceback of the unittest module.
504 505
505 506 -t
506 507 print timing information at the end of the run. IPython will give
507 508 you an estimated CPU time consumption for your script, which under
508 509 Unix uses the resource module to avoid the wraparound problems of
509 510 time.clock(). Under Unix, an estimate of time spent on system tasks
510 511 is also given (for Windows platforms this is reported as 0.0).
511 512
512 513 If -t is given, an additional ``-N<N>`` option can be given, where <N>
513 514 must be an integer indicating how many times you want the script to
514 515 run. The final timing report will include total and per run results.
515 516
516 517 For example (testing the script uniq_stable.py)::
517 518
518 519 In [1]: run -t uniq_stable
519 520
520 521 IPython CPU timings (estimated):
521 522 User : 0.19597 s.
522 523 System: 0.0 s.
523 524
524 525 In [2]: run -t -N5 uniq_stable
525 526
526 527 IPython CPU timings (estimated):
527 528 Total runs performed: 5
528 529 Times : Total Per run
529 530 User : 0.910862 s, 0.1821724 s.
530 531 System: 0.0 s, 0.0 s.
531 532
532 533 -d
533 534 run your program under the control of pdb, the Python debugger.
534 535 This allows you to execute your program step by step, watch variables,
535 536 etc. Internally, what IPython does is similar to calling::
536 537
537 538 pdb.run('execfile("YOURFILENAME")')
538 539
539 540 with a breakpoint set on line 1 of your file. You can change the line
540 541 number for this automatic breakpoint to be <N> by using the -bN option
541 542 (where N must be an integer). For example::
542 543
543 544 %run -d -b40 myscript
544 545
545 546 will set the first breakpoint at line 40 in myscript.py. Note that
546 547 the first breakpoint must be set on a line which actually does
547 548 something (not a comment or docstring) for it to stop execution.
548 549
549 550 Or you can specify a breakpoint in a different file::
550 551
551 552 %run -d -b myotherfile.py:20 myscript
552 553
553 554 When the pdb debugger starts, you will see a (Pdb) prompt. You must
554 555 first enter 'c' (without quotes) to start execution up to the first
555 556 breakpoint.
556 557
557 558 Entering 'help' gives information about the use of the debugger. You
558 559 can easily see pdb's full documentation with "import pdb;pdb.help()"
559 560 at a prompt.
560 561
561 562 -p
562 563 run program under the control of the Python profiler module (which
563 564 prints a detailed report of execution times, function calls, etc).
564 565
565 566 You can pass other options after -p which affect the behavior of the
566 567 profiler itself. See the docs for %prun for details.
567 568
568 569 In this mode, the program's variables do NOT propagate back to the
569 570 IPython interactive namespace (because they remain in the namespace
570 571 where the profiler executes them).
571 572
572 573 Internally this triggers a call to %prun, see its documentation for
573 574 details on the options available specifically for profiling.
574 575
575 576 There is one special usage for which the text above doesn't apply:
576 577 if the filename ends with .ipy[nb], the file is run as ipython script,
577 578 just as if the commands were written on IPython prompt.
578 579
579 580 -m
580 581 specify module name to load instead of script path. Similar to
581 582 the -m option for the python interpreter. Use this option last if you
582 583 want to combine with other %run options. Unlike the python interpreter
583 584 only source modules are allowed no .pyc or .pyo files.
584 585 For example::
585 586
586 587 %run -m example
587 588
588 589 will run the example module.
589 590
590 591 -G
591 592 disable shell-like glob expansion of arguments.
592 593
593 594 """
594 595
595 596 # get arguments and set sys.argv for program to be run.
596 597 opts, arg_lst = self.parse_options(parameter_s,
597 598 'nidtN:b:pD:l:rs:T:em:G',
598 599 mode='list', list_all=1)
599 600 if "m" in opts:
600 601 modulename = opts["m"][0]
601 602 modpath = find_mod(modulename)
602 603 if modpath is None:
603 604 warn('%r is not a valid modulename on sys.path'%modulename)
604 605 return
605 606 arg_lst = [modpath] + arg_lst
606 607 try:
607 608 filename = file_finder(arg_lst[0])
608 609 except IndexError:
609 610 warn('you must provide at least a filename.')
610 611 print('\n%run:\n', oinspect.getdoc(self.run))
611 612 return
612 613 except IOError as e:
613 614 try:
614 615 msg = str(e)
615 616 except UnicodeError:
616 617 msg = e.message
617 618 error(msg)
618 619 return
619 620
620 621 if filename.lower().endswith(('.ipy', '.ipynb')):
621 622 with preserve_keys(self.shell.user_ns, '__file__'):
622 623 self.shell.user_ns['__file__'] = filename
623 624 self.shell.safe_execfile_ipy(filename)
624 625 return
625 626
626 627 # Control the response to exit() calls made by the script being run
627 628 exit_ignore = 'e' in opts
628 629
629 630 # Make sure that the running script gets a proper sys.argv as if it
630 631 # were run from a system shell.
631 632 save_argv = sys.argv # save it for later restoring
632 633
633 634 if 'G' in opts:
634 635 args = arg_lst[1:]
635 636 else:
636 637 # tilde and glob expansion
637 638 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
638 639
639 640 sys.argv = [filename] + args # put in the proper filename
640 641 # protect sys.argv from potential unicode strings on Python 2:
641 642 if not py3compat.PY3:
642 643 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
643 644
644 645 if 'i' in opts:
645 646 # Run in user's interactive namespace
646 647 prog_ns = self.shell.user_ns
647 648 __name__save = self.shell.user_ns['__name__']
648 649 prog_ns['__name__'] = '__main__'
649 650 main_mod = self.shell.user_module
650 651
651 652 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
652 653 # set the __file__ global in the script's namespace
653 654 # TK: Is this necessary in interactive mode?
654 655 prog_ns['__file__'] = filename
655 656 else:
656 657 # Run in a fresh, empty namespace
657 658 if 'n' in opts:
658 659 name = os.path.splitext(os.path.basename(filename))[0]
659 660 else:
660 661 name = '__main__'
661 662
662 663 # The shell MUST hold a reference to prog_ns so after %run
663 664 # exits, the python deletion mechanism doesn't zero it out
664 665 # (leaving dangling references). See interactiveshell for details
665 666 main_mod = self.shell.new_main_mod(filename, name)
666 667 prog_ns = main_mod.__dict__
667 668
668 669 # pickle fix. See interactiveshell for an explanation. But we need to
669 670 # make sure that, if we overwrite __main__, we replace it at the end
670 671 main_mod_name = prog_ns['__name__']
671 672
672 673 if main_mod_name == '__main__':
673 674 restore_main = sys.modules['__main__']
674 675 else:
675 676 restore_main = False
676 677
677 678 # This needs to be undone at the end to prevent holding references to
678 679 # every single object ever created.
679 680 sys.modules[main_mod_name] = main_mod
680 681
681 682 if 'p' in opts or 'd' in opts:
682 683 if 'm' in opts:
683 684 code = 'run_module(modulename, prog_ns)'
684 685 code_ns = {
685 686 'run_module': self.shell.safe_run_module,
686 687 'prog_ns': prog_ns,
687 688 'modulename': modulename,
688 689 }
689 690 else:
690 691 if 'd' in opts:
691 692 # allow exceptions to raise in debug mode
692 693 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
693 694 else:
694 695 code = 'execfile(filename, prog_ns)'
695 696 code_ns = {
696 697 'execfile': self.shell.safe_execfile,
697 698 'prog_ns': prog_ns,
698 699 'filename': get_py_filename(filename),
699 700 }
700 701
701 702 try:
702 703 stats = None
703 704 with self.shell.readline_no_record:
704 705 if 'p' in opts:
705 706 stats = self._run_with_profiler(code, opts, code_ns)
706 707 else:
707 708 if 'd' in opts:
708 709 bp_file, bp_line = parse_breakpoint(
709 710 opts.get('b', ['1'])[0], filename)
710 711 self._run_with_debugger(
711 712 code, code_ns, filename, bp_line, bp_file)
712 713 else:
713 714 if 'm' in opts:
714 715 def run():
715 716 self.shell.safe_run_module(modulename, prog_ns)
716 717 else:
717 718 if runner is None:
718 719 runner = self.default_runner
719 720 if runner is None:
720 721 runner = self.shell.safe_execfile
721 722
722 723 def run():
723 724 runner(filename, prog_ns, prog_ns,
724 725 exit_ignore=exit_ignore)
725 726
726 727 if 't' in opts:
727 728 # timed execution
728 729 try:
729 730 nruns = int(opts['N'][0])
730 731 if nruns < 1:
731 732 error('Number of runs must be >=1')
732 733 return
733 734 except (KeyError):
734 735 nruns = 1
735 736 self._run_with_timing(run, nruns)
736 737 else:
737 738 # regular execution
738 739 run()
739 740
740 741 if 'i' in opts:
741 742 self.shell.user_ns['__name__'] = __name__save
742 743 else:
743 744 # update IPython interactive namespace
744 745
745 746 # Some forms of read errors on the file may mean the
746 747 # __name__ key was never set; using pop we don't have to
747 748 # worry about a possible KeyError.
748 749 prog_ns.pop('__name__', None)
749 750
750 751 with preserve_keys(self.shell.user_ns, '__file__'):
751 752 self.shell.user_ns.update(prog_ns)
752 753 finally:
753 754 # It's a bit of a mystery why, but __builtins__ can change from
754 755 # being a module to becoming a dict missing some key data after
755 756 # %run. As best I can see, this is NOT something IPython is doing
756 757 # at all, and similar problems have been reported before:
757 758 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
758 759 # Since this seems to be done by the interpreter itself, the best
759 760 # we can do is to at least restore __builtins__ for the user on
760 761 # exit.
761 762 self.shell.user_ns['__builtins__'] = builtin_mod
762 763
763 764 # Ensure key global structures are restored
764 765 sys.argv = save_argv
765 766 if restore_main:
766 767 sys.modules['__main__'] = restore_main
767 768 else:
768 769 # Remove from sys.modules the reference to main_mod we'd
769 770 # added. Otherwise it will trap references to objects
770 771 # contained therein.
771 772 del sys.modules[main_mod_name]
772 773
773 774 return stats
774 775
775 776 def _run_with_debugger(self, code, code_ns, filename=None,
776 777 bp_line=None, bp_file=None):
777 778 """
778 779 Run `code` in debugger with a break point.
779 780
780 781 Parameters
781 782 ----------
782 783 code : str
783 784 Code to execute.
784 785 code_ns : dict
785 786 A namespace in which `code` is executed.
786 787 filename : str
787 788 `code` is ran as if it is in `filename`.
788 789 bp_line : int, optional
789 790 Line number of the break point.
790 791 bp_file : str, optional
791 792 Path to the file in which break point is specified.
792 793 `filename` is used if not given.
793 794
794 795 Raises
795 796 ------
796 797 UsageError
797 798 If the break point given by `bp_line` is not valid.
798 799
799 800 """
800 801 deb = debugger.Pdb(self.shell.colors)
801 802 # reset Breakpoint state, which is moronically kept
802 803 # in a class
803 804 bdb.Breakpoint.next = 1
804 805 bdb.Breakpoint.bplist = {}
805 806 bdb.Breakpoint.bpbynumber = [None]
806 807 if bp_line is not None:
807 808 # Set an initial breakpoint to stop execution
808 809 maxtries = 10
809 810 bp_file = bp_file or filename
810 811 checkline = deb.checkline(bp_file, bp_line)
811 812 if not checkline:
812 813 for bp in range(bp_line + 1, bp_line + maxtries + 1):
813 814 if deb.checkline(bp_file, bp):
814 815 break
815 816 else:
816 817 msg = ("\nI failed to find a valid line to set "
817 818 "a breakpoint\n"
818 819 "after trying up to line: %s.\n"
819 820 "Please set a valid breakpoint manually "
820 821 "with the -b option." % bp)
821 822 raise UsageError(msg)
822 823 # if we find a good linenumber, set the breakpoint
823 824 deb.do_break('%s:%s' % (bp_file, bp_line))
824 825
825 826 if filename:
826 827 # Mimic Pdb._runscript(...)
827 828 deb._wait_for_mainpyfile = True
828 829 deb.mainpyfile = deb.canonic(filename)
829 830
830 831 # Start file run
831 832 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
832 833 try:
833 834 if filename:
834 835 # save filename so it can be used by methods on the deb object
835 836 deb._exec_filename = filename
836 837 while True:
837 838 try:
838 839 deb.run(code, code_ns)
839 840 except Restart:
840 841 print("Restarting")
841 842 if filename:
842 843 deb._wait_for_mainpyfile = True
843 844 deb.mainpyfile = deb.canonic(filename)
844 845 continue
845 846 else:
846 847 break
847 848
848 849
849 850 except:
850 851 etype, value, tb = sys.exc_info()
851 852 # Skip three frames in the traceback: the %run one,
852 853 # one inside bdb.py, and the command-line typed by the
853 854 # user (run by exec in pdb itself).
854 855 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
855 856
856 857 @staticmethod
857 858 def _run_with_timing(run, nruns):
858 859 """
859 860 Run function `run` and print timing information.
860 861
861 862 Parameters
862 863 ----------
863 864 run : callable
864 865 Any callable object which takes no argument.
865 866 nruns : int
866 867 Number of times to execute `run`.
867 868
868 869 """
869 870 twall0 = time.time()
870 871 if nruns == 1:
871 872 t0 = clock2()
872 873 run()
873 874 t1 = clock2()
874 875 t_usr = t1[0] - t0[0]
875 876 t_sys = t1[1] - t0[1]
876 877 print("\nIPython CPU timings (estimated):")
877 878 print(" User : %10.2f s." % t_usr)
878 879 print(" System : %10.2f s." % t_sys)
879 880 else:
880 881 runs = range(nruns)
881 882 t0 = clock2()
882 883 for nr in runs:
883 884 run()
884 885 t1 = clock2()
885 886 t_usr = t1[0] - t0[0]
886 887 t_sys = t1[1] - t0[1]
887 888 print("\nIPython CPU timings (estimated):")
888 889 print("Total runs performed:", nruns)
889 890 print(" Times : %10s %10s" % ('Total', 'Per run'))
890 891 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
891 892 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
892 893 twall1 = time.time()
893 894 print("Wall time: %10.2f s." % (twall1 - twall0))
894 895
895 896 @skip_doctest
896 897 @line_cell_magic
897 898 def timeit(self, line='', cell=None):
898 899 """Time execution of a Python statement or expression
899 900
900 901 Usage, in line mode:
901 902 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
902 903 or in cell mode:
903 904 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
904 905 code
905 906 code...
906 907
907 908 Time execution of a Python statement or expression using the timeit
908 909 module. This function can be used both as a line and cell magic:
909 910
910 911 - In line mode you can time a single-line statement (though multiple
911 912 ones can be chained with using semicolons).
912 913
913 914 - In cell mode, the statement in the first line is used as setup code
914 915 (executed but not timed) and the body of the cell is timed. The cell
915 916 body has access to any variables created in the setup code.
916 917
917 918 Options:
918 919 -n<N>: execute the given statement <N> times in a loop. If this value
919 920 is not given, a fitting value is chosen.
920 921
921 922 -r<R>: repeat the loop iteration <R> times and take the best result.
922 923 Default: 3
923 924
924 925 -t: use time.time to measure the time, which is the default on Unix.
925 926 This function measures wall time.
926 927
927 928 -c: use time.clock to measure the time, which is the default on
928 929 Windows and measures wall time. On Unix, resource.getrusage is used
929 930 instead and returns the CPU user time.
930 931
931 932 -p<P>: use a precision of <P> digits to display the timing result.
932 933 Default: 3
933 934
934 935 -q: Quiet, do not print result.
935 936
936 937 -o: return a TimeitResult that can be stored in a variable to inspect
937 938 the result in more details.
938 939
939 940
940 941 Examples
941 942 --------
942 943 ::
943 944
944 945 In [1]: %timeit pass
945 946 10000000 loops, best of 3: 53.3 ns per loop
946 947
947 948 In [2]: u = None
948 949
949 950 In [3]: %timeit u is None
950 951 10000000 loops, best of 3: 184 ns per loop
951 952
952 953 In [4]: %timeit -r 4 u == None
953 954 1000000 loops, best of 4: 242 ns per loop
954 955
955 956 In [5]: import time
956 957
957 958 In [6]: %timeit -n1 time.sleep(2)
958 959 1 loops, best of 3: 2 s per loop
959 960
960 961
961 962 The times reported by %timeit will be slightly higher than those
962 963 reported by the timeit.py script when variables are accessed. This is
963 964 due to the fact that %timeit executes the statement in the namespace
964 965 of the shell, compared with timeit.py, which uses a single setup
965 966 statement to import function or create variables. Generally, the bias
966 967 does not matter as long as results from timeit.py are not mixed with
967 968 those from %timeit."""
968 969
969 970 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
970 971 posix=False, strict=False)
971 972 if stmt == "" and cell is None:
972 973 return
973 974
974 975 timefunc = timeit.default_timer
975 976 number = int(getattr(opts, "n", 0))
976 977 repeat = int(getattr(opts, "r", timeit.default_repeat))
977 978 precision = int(getattr(opts, "p", 3))
978 979 quiet = 'q' in opts
979 980 return_result = 'o' in opts
980 981 if hasattr(opts, "t"):
981 982 timefunc = time.time
982 983 if hasattr(opts, "c"):
983 984 timefunc = clock
984 985
985 986 timer = Timer(timer=timefunc)
986 987 # this code has tight coupling to the inner workings of timeit.Timer,
987 988 # but is there a better way to achieve that the code stmt has access
988 989 # to the shell namespace?
989 990 transform = self.shell.input_splitter.transform_cell
990 991
991 992 if cell is None:
992 993 # called as line magic
993 994 ast_setup = self.shell.compile.ast_parse("pass")
994 995 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
995 996 else:
996 997 ast_setup = self.shell.compile.ast_parse(transform(stmt))
997 998 ast_stmt = self.shell.compile.ast_parse(transform(cell))
998 999
999 1000 ast_setup = self.shell.transform_ast(ast_setup)
1000 1001 ast_stmt = self.shell.transform_ast(ast_stmt)
1001 1002
1002 1003 # This codestring is taken from timeit.template - we fill it in as an
1003 1004 # AST, so that we can apply our AST transformations to the user code
1004 1005 # without affecting the timing code.
1005 1006 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1006 1007 ' setup\n'
1007 1008 ' _t0 = _timer()\n'
1008 1009 ' for _i in _it:\n'
1009 1010 ' stmt\n'
1010 1011 ' _t1 = _timer()\n'
1011 1012 ' return _t1 - _t0\n')
1012 1013
1013 1014 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1014 1015 timeit_ast = ast.fix_missing_locations(timeit_ast)
1015 1016
1016 1017 # Track compilation time so it can be reported if too long
1017 1018 # Minimum time above which compilation time will be reported
1018 1019 tc_min = 0.1
1019 1020
1020 1021 t0 = clock()
1021 1022 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1022 1023 tc = clock()-t0
1023 1024
1024 1025 ns = {}
1025 1026 exec(code, self.shell.user_ns, ns)
1026 1027 timer.inner = ns["inner"]
1027 1028
1028 1029 # This is used to check if there is a huge difference between the
1029 1030 # best and worst timings.
1030 1031 # Issue: https://github.com/ipython/ipython/issues/6471
1031 1032 worst_tuning = 0
1032 1033 if number == 0:
1033 1034 # determine number so that 0.2 <= total time < 2.0
1034 1035 number = 1
1035 1036 for _ in range(1, 10):
1036 1037 time_number = timer.timeit(number)
1037 1038 worst_tuning = max(worst_tuning, time_number / number)
1038 1039 if time_number >= 0.2:
1039 1040 break
1040 1041 number *= 10
1041 1042 all_runs = timer.repeat(repeat, number)
1042 1043 best = min(all_runs) / number
1043 1044 if not quiet :
1044 1045 worst = max(all_runs) / number
1045 1046 if worst_tuning:
1046 1047 worst = max(worst, worst_tuning)
1047 1048 # Check best timing is greater than zero to avoid a
1048 1049 # ZeroDivisionError.
1049 1050 # In cases where the slowest timing is lesser than a micosecond
1050 1051 # we assume that it does not really matter if the fastest
1051 1052 # timing is 4 times faster than the slowest timing or not.
1052 1053 if worst > 4 * best and best > 0 and worst > 1e-6:
1053 1054 print("The slowest run took %0.2f times longer than the "
1054 1055 "fastest. This could mean that an intermediate result "
1055 1056 "is being cached " % (worst / best))
1056 1057 print(u"%d loops, best of %d: %s per loop" % (number, repeat,
1057 1058 _format_time(best, precision)))
1058 1059 if tc > tc_min:
1059 1060 print("Compiler time: %.2f s" % tc)
1060 1061 if return_result:
1061 return TimeitResult(number, repeat, best, all_runs, tc, precision)
1062 return TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1062 1063
1063 1064 @skip_doctest
1064 1065 @needs_local_scope
1065 1066 @line_cell_magic
1066 1067 def time(self,line='', cell=None, local_ns=None):
1067 1068 """Time execution of a Python statement or expression.
1068 1069
1069 1070 The CPU and wall clock times are printed, and the value of the
1070 1071 expression (if any) is returned. Note that under Win32, system time
1071 1072 is always reported as 0, since it can not be measured.
1072 1073
1073 1074 This function can be used both as a line and cell magic:
1074 1075
1075 1076 - In line mode you can time a single-line statement (though multiple
1076 1077 ones can be chained with using semicolons).
1077 1078
1078 1079 - In cell mode, you can time the cell body (a directly
1079 1080 following statement raises an error).
1080 1081
1081 1082 This function provides very basic timing functionality. Use the timeit
1082 1083 magic for more control over the measurement.
1083 1084
1084 1085 Examples
1085 1086 --------
1086 1087 ::
1087 1088
1088 1089 In [1]: %time 2**128
1089 1090 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1090 1091 Wall time: 0.00
1091 1092 Out[1]: 340282366920938463463374607431768211456L
1092 1093
1093 1094 In [2]: n = 1000000
1094 1095
1095 1096 In [3]: %time sum(range(n))
1096 1097 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1097 1098 Wall time: 1.37
1098 1099 Out[3]: 499999500000L
1099 1100
1100 1101 In [4]: %time print 'hello world'
1101 1102 hello world
1102 1103 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1103 1104 Wall time: 0.00
1104 1105
1105 1106 Note that the time needed by Python to compile the given expression
1106 1107 will be reported if it is more than 0.1s. In this example, the
1107 1108 actual exponentiation is done by Python at compilation time, so while
1108 1109 the expression can take a noticeable amount of time to compute, that
1109 1110 time is purely due to the compilation:
1110 1111
1111 1112 In [5]: %time 3**9999;
1112 1113 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1113 1114 Wall time: 0.00 s
1114 1115
1115 1116 In [6]: %time 3**999999;
1116 1117 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1117 1118 Wall time: 0.00 s
1118 1119 Compiler : 0.78 s
1119 1120 """
1120 1121
1121 1122 # fail immediately if the given expression can't be compiled
1122 1123
1123 1124 if line and cell:
1124 1125 raise UsageError("Can't use statement directly after '%%time'!")
1125 1126
1126 1127 if cell:
1127 1128 expr = self.shell.input_transformer_manager.transform_cell(cell)
1128 1129 else:
1129 1130 expr = self.shell.input_transformer_manager.transform_cell(line)
1130 1131
1131 1132 # Minimum time above which parse time will be reported
1132 1133 tp_min = 0.1
1133 1134
1134 1135 t0 = clock()
1135 1136 expr_ast = self.shell.compile.ast_parse(expr)
1136 1137 tp = clock()-t0
1137 1138
1138 1139 # Apply AST transformations
1139 1140 expr_ast = self.shell.transform_ast(expr_ast)
1140 1141
1141 1142 # Minimum time above which compilation time will be reported
1142 1143 tc_min = 0.1
1143 1144
1144 1145 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1145 1146 mode = 'eval'
1146 1147 source = '<timed eval>'
1147 1148 expr_ast = ast.Expression(expr_ast.body[0].value)
1148 1149 else:
1149 1150 mode = 'exec'
1150 1151 source = '<timed exec>'
1151 1152 t0 = clock()
1152 1153 code = self.shell.compile(expr_ast, source, mode)
1153 1154 tc = clock()-t0
1154 1155
1155 1156 # skew measurement as little as possible
1156 1157 glob = self.shell.user_ns
1157 1158 wtime = time.time
1158 1159 # time execution
1159 1160 wall_st = wtime()
1160 1161 if mode=='eval':
1161 1162 st = clock2()
1162 1163 out = eval(code, glob, local_ns)
1163 1164 end = clock2()
1164 1165 else:
1165 1166 st = clock2()
1166 1167 exec(code, glob, local_ns)
1167 1168 end = clock2()
1168 1169 out = None
1169 1170 wall_end = wtime()
1170 1171 # Compute actual times and report
1171 1172 wall_time = wall_end-wall_st
1172 1173 cpu_user = end[0]-st[0]
1173 1174 cpu_sys = end[1]-st[1]
1174 1175 cpu_tot = cpu_user+cpu_sys
1175 1176 # On windows cpu_sys is always zero, so no new information to the next print
1176 1177 if sys.platform != 'win32':
1177 1178 print("CPU times: user %s, sys: %s, total: %s" % \
1178 1179 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1179 1180 print("Wall time: %s" % _format_time(wall_time))
1180 1181 if tc > tc_min:
1181 1182 print("Compiler : %s" % _format_time(tc))
1182 1183 if tp > tp_min:
1183 1184 print("Parser : %s" % _format_time(tp))
1184 1185 return out
1185 1186
1186 1187 @skip_doctest
1187 1188 @line_magic
1188 1189 def macro(self, parameter_s=''):
1189 1190 """Define a macro for future re-execution. It accepts ranges of history,
1190 1191 filenames or string objects.
1191 1192
1192 1193 Usage:\\
1193 1194 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1194 1195
1195 1196 Options:
1196 1197
1197 1198 -r: use 'raw' input. By default, the 'processed' history is used,
1198 1199 so that magics are loaded in their transformed version to valid
1199 1200 Python. If this option is given, the raw input as typed at the
1200 1201 command line is used instead.
1201 1202
1202 1203 -q: quiet macro definition. By default, a tag line is printed
1203 1204 to indicate the macro has been created, and then the contents of
1204 1205 the macro are printed. If this option is given, then no printout
1205 1206 is produced once the macro is created.
1206 1207
1207 1208 This will define a global variable called `name` which is a string
1208 1209 made of joining the slices and lines you specify (n1,n2,... numbers
1209 1210 above) from your input history into a single string. This variable
1210 1211 acts like an automatic function which re-executes those lines as if
1211 1212 you had typed them. You just type 'name' at the prompt and the code
1212 1213 executes.
1213 1214
1214 1215 The syntax for indicating input ranges is described in %history.
1215 1216
1216 1217 Note: as a 'hidden' feature, you can also use traditional python slice
1217 1218 notation, where N:M means numbers N through M-1.
1218 1219
1219 1220 For example, if your history contains (print using %hist -n )::
1220 1221
1221 1222 44: x=1
1222 1223 45: y=3
1223 1224 46: z=x+y
1224 1225 47: print x
1225 1226 48: a=5
1226 1227 49: print 'x',x,'y',y
1227 1228
1228 1229 you can create a macro with lines 44 through 47 (included) and line 49
1229 1230 called my_macro with::
1230 1231
1231 1232 In [55]: %macro my_macro 44-47 49
1232 1233
1233 1234 Now, typing `my_macro` (without quotes) will re-execute all this code
1234 1235 in one pass.
1235 1236
1236 1237 You don't need to give the line-numbers in order, and any given line
1237 1238 number can appear multiple times. You can assemble macros with any
1238 1239 lines from your input history in any order.
1239 1240
1240 1241 The macro is a simple object which holds its value in an attribute,
1241 1242 but IPython's display system checks for macros and executes them as
1242 1243 code instead of printing them when you type their name.
1243 1244
1244 1245 You can view a macro's contents by explicitly printing it with::
1245 1246
1246 1247 print macro_name
1247 1248
1248 1249 """
1249 1250 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1250 1251 if not args: # List existing macros
1251 1252 return sorted(k for k,v in iteritems(self.shell.user_ns) if\
1252 1253 isinstance(v, Macro))
1253 1254 if len(args) == 1:
1254 1255 raise UsageError(
1255 1256 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1256 1257 name, codefrom = args[0], " ".join(args[1:])
1257 1258
1258 1259 #print 'rng',ranges # dbg
1259 1260 try:
1260 1261 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1261 1262 except (ValueError, TypeError) as e:
1262 1263 print(e.args[0])
1263 1264 return
1264 1265 macro = Macro(lines)
1265 1266 self.shell.define_macro(name, macro)
1266 1267 if not ( 'q' in opts) :
1267 1268 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1268 1269 print('=== Macro contents: ===')
1269 1270 print(macro, end=' ')
1270 1271
1271 1272 @magic_arguments.magic_arguments()
1272 1273 @magic_arguments.argument('output', type=str, default='', nargs='?',
1273 1274 help="""The name of the variable in which to store output.
1274 1275 This is a utils.io.CapturedIO object with stdout/err attributes
1275 1276 for the text of the captured output.
1276 1277
1277 1278 CapturedOutput also has a show() method for displaying the output,
1278 1279 and __call__ as well, so you can use that to quickly display the
1279 1280 output.
1280 1281
1281 1282 If unspecified, captured output is discarded.
1282 1283 """
1283 1284 )
1284 1285 @magic_arguments.argument('--no-stderr', action="store_true",
1285 1286 help="""Don't capture stderr."""
1286 1287 )
1287 1288 @magic_arguments.argument('--no-stdout', action="store_true",
1288 1289 help="""Don't capture stdout."""
1289 1290 )
1290 1291 @magic_arguments.argument('--no-display', action="store_true",
1291 1292 help="""Don't capture IPython's rich display."""
1292 1293 )
1293 1294 @cell_magic
1294 1295 def capture(self, line, cell):
1295 1296 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1296 1297 args = magic_arguments.parse_argstring(self.capture, line)
1297 1298 out = not args.no_stdout
1298 1299 err = not args.no_stderr
1299 1300 disp = not args.no_display
1300 1301 with capture_output(out, err, disp) as io:
1301 1302 self.shell.run_cell(cell)
1302 1303 if args.output:
1303 1304 self.shell.user_ns[args.output] = io
1304 1305
1305 1306 def parse_breakpoint(text, current_file):
1306 1307 '''Returns (file, line) for file:line and (current_file, line) for line'''
1307 1308 colon = text.find(':')
1308 1309 if colon == -1:
1309 1310 return current_file, int(text)
1310 1311 else:
1311 1312 return text[:colon], int(text[colon+1:])
1312 1313
1313 1314 def _format_time(timespan, precision=3):
1314 1315 """Formats the timespan in a human readable form"""
1315 1316 import math
1316 1317
1317 1318 if timespan >= 60.0:
1318 1319 # we have more than a minute, format that in a human readable form
1319 1320 # Idea from http://snipplr.com/view/5713/
1320 1321 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1321 1322 time = []
1322 1323 leftover = timespan
1323 1324 for suffix, length in parts:
1324 1325 value = int(leftover / length)
1325 1326 if value > 0:
1326 1327 leftover = leftover % length
1327 1328 time.append(u'%s%s' % (str(value), suffix))
1328 1329 if leftover < 1:
1329 1330 break
1330 1331 return " ".join(time)
1331 1332
1332 1333
1333 1334 # Unfortunately the unicode 'micro' symbol can cause problems in
1334 1335 # certain terminals.
1335 1336 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1336 1337 # Try to prevent crashes by being more secure than it needs to
1337 1338 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1338 1339 units = [u"s", u"ms",u'us',"ns"] # the save value
1339 1340 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1340 1341 try:
1341 1342 u'\xb5'.encode(sys.stdout.encoding)
1342 1343 units = [u"s", u"ms",u'\xb5s',"ns"]
1343 1344 except:
1344 1345 pass
1345 1346 scaling = [1, 1e3, 1e6, 1e9]
1346 1347
1347 1348 if timespan > 0.0:
1348 1349 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1349 1350 else:
1350 1351 order = 3
1351 1352 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
General Comments 0
You need to be logged in to leave comments. Login now