Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Boring, boring, boring

Slide 3

Slide 3 text

pip install ipython

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Let’s get ready to rumble

Slide 6

Slide 6 text

Tips

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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')]

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Tricks

Slide 14

Slide 14 text

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']

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Magic

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

%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

Slide 20

Slide 20 text

%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'

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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)

Slide 23

Slide 23 text

Questions?

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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/

Slide 26

Slide 26 text

Thank you!