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