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