Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IPython: tips, tricks & magic

IPython: tips, tricks & magic

My talk at PyWaw #14.
http://www.pywaw.org/16-07-2012

Zbigniew Siciarz

July 16, 2012
Tweet

More Decks by Zbigniew Siciarz

Other Decks in Programming

Transcript

  1. IPython
    Tips, tricks & magic
    PyWaw #14, 16.07.2012
    Zbigniew Siciarz @zsiciarz http://siciarz.net

    View full-size slide

  2. Boring, boring, boring

    View full-size slide

  3. pip install ipython

    View full-size slide

  4. Let’s get ready to rumble

    View full-size slide

  5. Getting help
    In [1]: from os.path import basename
    In [2]: basename?
    Type: function
    String Form:
    File: c:\users\user\v\ipython-pywaw\lib\ntpath.py
    Definition: basename(p)
    Docstring: Returns the final component of a pathname

    View full-size slide

  6. Looking outside
    In [5]: !ls
    1.txt 2.txt hello.txt
    In [6]: files = !ls
    In [7]: files
    Out[7]: ['1.txt', '2.txt', 'hello.txt']
    In [8]: files.s
    Out[8]: '1.txt 2.txt hello.txt'
    In [9]: files.n
    Out[9]: '1.txt\n2.txt\nhello.txt'
    In [10]: files.p
    Out[10]: [path(u'1.txt'), path(u'2.txt'), path(u'hello.txt')]

    View full-size slide

  7. History
    In [1]: 40 + 2
    Out[1]: 42
    In [2]: range(10)
    Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    In [3]: print _, __
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 42
    In [4]: print _2
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    In [5]: print Out[1]
    42

    View full-size slide

  8. History
    In [6]: import math
    In [7]: spam = math.sqrt(27)
    In [8]: print spam
    5.19615242271
    In [9]: %history 6-8
    import math
    spam = math.sqrt(27)
    print spam
    In [10]: %save test.py 6-8
    The following commands were written to file `test.py`:
    import math
    spam = math.sqrt(27)
    print spam

    View full-size slide

  9. Session logging
    In [1]: %logst
    %logstart %logstate %logstop
    In [1]: %logstart
    Activating auto-logging. Current session state plus future
    input saved.
    Filename : ipython_log.py
    Mode : rotate
    Output logging : False
    Raw input log : False
    Timestamping : False
    State : active
    In [2]: x = 5
    In [3]:
    Do you really want to exit ([y]/n)? y

    View full-size slide

  10. Session logging
    zbyszek@deimos ~
    % ipython -i ipython_log.py
    Python 2.7.3 (default, Apr 20 2012, 22:39:59)
    Type "copyright", "credits" or "license" for more information.

    In [1]: x
    Out[1]: 5

    View full-size slide

  11. More than a list
    In [6]: files = !ls
    In [7]: files
    Out[7]: ['1.txt', '2.txt', 'hello.txt']
    In [8]: files.s
    Out[8]: '1.txt 2.txt hello.txt'
    In [9]: files.n
    Out[9]: '1.txt\n2.txt\nhello.txt'
    In [10]: files.p
    Out[10]: [path(u'1.txt'), path(u'2.txt'), path(u'hello.txt')]
    In [11]: files.grep(r'^\d')
    Out[11]: ['1.txt', '2.txt']

    View full-size slide

  12. Removing unversioned files
    In [11]: !hg st
    A hello.txt
    ? 1.txt
    ? 2.txt
    In [12]: lines = !hg st
    In [13]: files = lines.grep(r'^\?').fields(1)
    In [14]: files
    Out[14]: ['1.txt', '2.txt']
    In [15]: !rm $files.s
    In [16]: !ls
    hello.txt

    View full-size slide

  13. Quick doctests
    In [27]: def make_everything_awesome(x):
    ....: setattr(x, 'awesome', True)
    ....: return x
    ....:
    In [28]: class Talk(object): pass
    In [29]: this_talk = Talk()
    In [30]: this_talk = make_everything_awesome(this_talk)
    In [31]: this_talk.awesome
    Out[31]: True
    In [32]: %history -op 28-31
    >>> class Talk(object): pass
    >>> this_talk = Talk()
    >>> this_talk = make_everything_awesome(this_talk)
    >>> this_talk.awesome
    True

    View full-size slide

  14. Built-in magics
    • %run
    • %edit
    • %save
    • %timeit
    • %paste
    • %pastebin
    • …and more

    View full-size slide

  15. %timeit
    In [13]: def factorial(n):
    ....: if n in [0, 1]:
    ....: return 1
    ....: else:
    ....: return n * factorial(n - 1)
    ....:
    In [14]: %timeit factorial(120)
    10000 loops, best of 3: 32.5 us per loop
    In [28]: def factorial2(n):
    ....: return reduce(operator.mul, xrange(1, n + 1))
    ....:
    In [29]: %timeit factorial2(120)
    100000 loops, best of 3: 15.9 us per loop

    View full-size slide

  16. %pastebin
    In [6]: words = ['Hello', 'PyWaw']
    In [7]: print " ".join(words) + '!'
    Hello PyWaw!
    In [8]: %pastebin 6-7
    Out[8]: u'https://gist.github.com/3119227'

    View full-size slide

  17. Cell magic
    In [48]: %%capture out
    ....: print "aaa"
    ....: print 666
    ....:
    In [49]: out.stdout
    Out[49]: 'aaa\n666\n‘
    In [50]: %%ruby
    ....: puts (1..10).inject {|sum, n| sum + n }
    ....:
    55

    View full-size slide

  18. How to be a magician
    ~/.config/ipython/profile_default/startup/pep8_magics.py:
    import subprocess
    from IPython.core.magic import Magics, line_magic, magics_class
    @magics_class
    class Pep8Magics(Magics):
    @line_magic
    def pep8(self, filename):
    try:
    output = subprocess.check_output(['pep8', filename])
    print "Congratulations, no problems"
    print output
    except subprocess.CalledProcessError as e:
    print "PEP8 complains about:"
    print e.output
    ip = get_ipython()
    ip.register_magics(Pep8Magics)

    View full-size slide

  19. Hire me!
    • > 2 years in Python
    • mostly web apps (Django, Flask)
    • some desktop experience (Qt)
    • powered by curiosity (and caffeine)
    [email protected]
    • http://www.goldenline.pl/zbigniew-siciarz

    View full-size slide

  20. Credits
    • Fireworks photo by bayasaa:
    http://www.flickr.com/photos/bayasaa/2693171833/
    • Clippy screenshot from Wikipedia:
    http://en.wikipedia.org/wiki/File:Clippy-letter.PNG
    • Pythocat by Cameron McEfee: http://octodex.github.com/pythocat/

    View full-size slide