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