Capture the result of an IPython magic function

eric chiang

I'm trying to capture the resulting object of IPython Notebook magic function. Specifically %timeit

So the following code...

import time
def say_hello(n):
    time.sleep(n)
    print "hello"

t = %timeit say_hello(5)

Prints to stdout:

1 loops, best of 3: 5 s per loop

However, I'd like to capture the result of %timeit say_hello(5) in the variable t.

A resulting object called TimeitResult is generated by %timeit, but I can't figure out how to access it from within a Notebook.

I'd like a cleaner solution than having to manually capture stdout using sys.stdout tricks (this code will be part of a presentation so I'm trying to keep it as straight forward as possible). Anyone have any ideas?

dsemi

In the source file you linked to, the docstring shows the options for running the timeit magic function; one of which is returning an object result:

-o: return a TimeitResult that can be stored in a variable to inspect
        the result in more details.

So, if you run

obj = %timeit -o somefunc()

obj will reference the result object that was returned (hint: use tab completion on the object, that will show you the attributes it has).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iPython autoreload magic function not found

From Dev

IPython: Alias which uses a magic function itself

From Dev

ipython notebook R cell magic as a python function

From Dev

What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

From Dev

IPython custom tab-completion for user magic function

From Dev

IPython custom tab-completion for user magic function

From Dev

Ignore IPython magic in python

From Dev

Capture bash function's result and allow it to exit

From Dev

Memory address of ipython magic functions

From Dev

Pipe Ipython magic output to a variable?

From Dev

Bash cell magic in IPython script

From Dev

Capture multi-line result with mysql_fetch_array() function

From Dev

How to capture the result returned by a callback function call on a Bokeh TextInput widget?

From Dev

Cython in Ipython: ERROR: Cell magic `%%cython` not found

From Dev

Custom magic in ipython notebooks - Code does not execute

From Dev

iPython - set up magic commands in configuration file

From Dev

ipython %%javascript magic output into cell and console

From Dev

Pylab / matplotlib magic in IPython: supress loading message

From Dev

Time python scripts using IPython magic

From Dev

Split IPython Magic Shell Multiple Lines

From Dev

Force iPython %cd magic command to not print path

From Dev

-n and -r arguments to IPython's %timeit magic

From Dev

IPython Notebook Magic Functions with environment variables

From Dev

Magic function timeit

From Dev

Quirky __set() magic function

From Dev

capture expected result with regex

From Dev

How to store the result from %%timeit cell magic?

From Dev

IPython run magic: How create an alias for "run -i"?

From Dev

IPython Cell Magic to Run this Cell in Client and in All Engines

Related Related

  1. 1

    iPython autoreload magic function not found

  2. 2

    IPython: Alias which uses a magic function itself

  3. 3

    ipython notebook R cell magic as a python function

  4. 4

    What is the pure Python equivalent to the IPython magic function call %matplotlib inline?

  5. 5

    IPython custom tab-completion for user magic function

  6. 6

    IPython custom tab-completion for user magic function

  7. 7

    Ignore IPython magic in python

  8. 8

    Capture bash function's result and allow it to exit

  9. 9

    Memory address of ipython magic functions

  10. 10

    Pipe Ipython magic output to a variable?

  11. 11

    Bash cell magic in IPython script

  12. 12

    Capture multi-line result with mysql_fetch_array() function

  13. 13

    How to capture the result returned by a callback function call on a Bokeh TextInput widget?

  14. 14

    Cython in Ipython: ERROR: Cell magic `%%cython` not found

  15. 15

    Custom magic in ipython notebooks - Code does not execute

  16. 16

    iPython - set up magic commands in configuration file

  17. 17

    ipython %%javascript magic output into cell and console

  18. 18

    Pylab / matplotlib magic in IPython: supress loading message

  19. 19

    Time python scripts using IPython magic

  20. 20

    Split IPython Magic Shell Multiple Lines

  21. 21

    Force iPython %cd magic command to not print path

  22. 22

    -n and -r arguments to IPython's %timeit magic

  23. 23

    IPython Notebook Magic Functions with environment variables

  24. 24

    Magic function timeit

  25. 25

    Quirky __set() magic function

  26. 26

    capture expected result with regex

  27. 27

    How to store the result from %%timeit cell magic?

  28. 28

    IPython run magic: How create an alias for "run -i"?

  29. 29

    IPython Cell Magic to Run this Cell in Client and in All Engines

HotTag

Archive