IPython – Tips & Tricks



IPython – Tips & Tricks

0 0


ipython_presentation


On Github switowski / ipython_presentation

IPython

Tips & Tricks

Created by Sebastian Witowski

Hi everyone. If you saw the Lightning Talks last time probably you remember the nice presentation that Alehandro gave about IPython notebooks. Today, I want to talk about technology that powers the notebooks - the IPython itself.

2 kinds of Pythonistas

First of all - what is this IPython ? It's the same thing as the standard Python REPL but it has a few cool features on top of it. So let's talk about those features.

Live demo #1

I've decided that the best way to show you the capabilities of IPython is with a short live demo. This is how those demos usually ends. Nevertheless, let's give it a try. So let's start the IPython REPL OFFLINE NOTES: increase the font ! import requests ->TAB # Let's import a random module, you can see that pressing TAB gives you the auto completion requests.get? # Adding a question mark will show you the doc string of that function requests.get?? # Adding two question marks will show you the whole body of a function requests.cookies # It works also with classes or modules

%magic

Another nice feature is that IPython comes with a bunch of magic functions built in. Yes, they are seriously called 'magic functions'. Let's see what we can do with that. Let's try to write a simple function. And by write, I mean copy and paste it from the next slide.

%edit

def hello(name):
    print 'Hello', name '!'
                    
So, what happens if I paste this code into IPython ? Well, I get an error, cause I forgot the comma. I could press arrow up and try to fix it, but when the function has multiple lines this can become quite a hassle. So we can use a magic function called %edit. It will open your favorite text editor (or Vim, if you haven't configured it) and allow you to edit your code there. After you save and exit, it will execute the code. I could write my function here, but I'm too lazy to do this. Instead, I can use the %edit command and specify a line number, and this line will be automatically copied inside the editor. There is also another great feature of %edit command, if you run if with -p option, it will reopen the same file as last time. So you can keep iterating on your function until you fix all the errors.

%debug

def hello(name):
    print 'Hello', name, '!'
    print number + 42
                    
But what happens if the function is more complicated and I don't see the error right away ? IPython has a nice function that you can use in this case, called %debug. It will automatically find the error in the code and fix it for you. How cool is that ? No, I'm kidding, it won't do that (yet!), instead it will start the debugger session in the place where the error has occurred, so you can poke around and find the error. So, let's see what is the problem. Name is defined... Ahh, but the number is not defined. I could now use the %edit command to fix it, but let's continue with another example.

%cpaste

def hello(name):
    print 'Hello', name, '!'
    print number + 42
number = 0
                    
So, now we have fully working function. Except that if you try to copy it into IPython, it will complain about indentation error. And yes, IPython has a magic function to fix that, it's called %cpaste. It will allow you to paste some code and automatically adjust the indentations. It's a very handy function if you want to copy a huge chunk of code.
%timeit function()
%pastebin 10-20
# 'https://gist.github.com/8f03e623fe366'
%macro foo 10-20
%save foo.py 10-20
There are many more interesting functions in IPython, I will just quickly show a few: - you can use %timeit to measure the execution time of a function - if you think that some code is worth sharing, you can use %pastebin command. It creates a gist on github with lines 10-20 and return the URL that you can share with your colleagues. - you can save some lines as a macro with %macro command. This command will save lines 10-20 as a macro called foo, so each time you type foo, it will execute those 10 lines. - and if you are particularly happy with some code you just wrote, you can save it to a file with %save command. This example with save lines 10-20 in a file called foo.py.

Shell commands:

ls
pwd
cd ..
!ping www.google.com

You can also execute shell commands directly from IPython. Some basic ones will work out of the box but with more advanced ones, you need to add an exclamation mark as a prefix.

Debugging your code with IPython:

some code
import IPython; IPython.embed()
some other code that fails

My last piece of advice is that you can use IPython for debugging any of your scripts. You just import IPython and then call IPython.embed() function anywhere in your code and if the code execution reaches this place, it will open IPython session in your terminal.It works very well for debugging websites that you have hosted locally.

Summary

  • Tab completion
  • Easy access to documentation with ? and ??
  • %magic function
  • Shell commands
  • Debugging

Where can I get it ?

http://ipython.org/install.html
So to sum up, IPython has many advantages over the standard Python REPL. You get the tab completion that helps you typing faster. You have easy access to the documentation. You have plenty of magic functions to help you with most common tasks. You can execute shell commands without leaving IPython. And you can use all those features when debugging your code. So, if you are intested, go to the IPython website to learn how to install it.

Questions ?

Thank you, do you have any questions ?
IPython Tips & Tricks Created by Sebastian Witowski Hi everyone. If you saw the Lightning Talks last time probably you remember the nice presentation that Alehandro gave about IPython notebooks. Today, I want to talk about technology that powers the notebooks - the IPython itself.