IPython Tips and Tricks
Recently I have been working on Python automation scripts. Very often I use IPython to develop/debug the code.
IPython is an advanced interactive python shell. It is a powerful tool which has many more features. However, here I would like to share some of the cool tricks of IPython.
Typing object_name? will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes.
In : import datetime In : datetime.datetime? Docstring: datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]) The year, month and day arguments are required. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be ints or longs. File: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/datetime.so Type: type
This will bring up an editor to type multiline code and execute the resulting code.
In : %edit IPython will make a temporary file named: /var/folders/xh/2m0ydjs51qxd_3y2k7x50hjc0000gn/T/ipython_edit_jnVJ51/ipython_edit_NdnenL.py
In : %edit -p
This will bring up the editor with the same data as the previous time it was used or saved. (in the current session)
Run a script
This will execute the script and print the results.
In : %run date_sample.py Current date and time: 2015-06-18 16:10:34.444674 Or like this: 15-06-18-16-10 Week number of the year: 24 Weekday of the week: 4
Activate the interactive debugger.
In : %run date.py Current date and time: 2015-06-18 16:12:32.417691 Or like this: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/kannan/playground/date.py in <module>() 3 4 print "Current date and time: " , datetime.datetime.now() ----> 5 print "Or like this: " ,datetime.datetime.strftime("%y-%m-%d-%H-%M") 6 print "Week number of the year: ", datetime.date.today().strftime("%W") 7 print "Weekday of the week: ", datetime.date.today().strftime("%w") TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str' In : %debug > /Users/kannan/playground/date.py(5)<module>() 4 print "Current date and time: " , datetime.datetime.now() ----> 5 print "Or like this: " ,datetime.datetime.strftime("%y-%m-%d-%H-%M") 6 print "Week number of the year: ", datetime.date.today().strftime("%W") ipdb> </module></module>
I made a error in the line number 5, it should have to look like this. So %debug command took me into the Python debugger.
print "Or like this: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
This will save the specified lines to a given file. You can pass any number of arguments separated by space.
In : %save hello.py 1-2 2-3 The following commands were written to file `hello.py`: import datetime datetime.datetime? %edit %edit -p
Repeat a command, or get command to input line for editing.
In : %recall 21 In : import datetime
Time execution of a Python statement or expression
It can be one line or multiline statement. In a one liner we can pass through multiple ones separated by semicolon.
In : %timeit range(100) 1000000 loops, best of 3: 752 ns per loop
Basic UNIX shell integration (you can run simple shell commands such as cp, ls, rm, cp, etc. directly from the ipython command line)
To execute any other shell commands we just need to add “!” beginning of the command line. We can assign the result of the system command to a Python variable to further use.
In : list_of_files = !ls In : list_of_files Out: ['lg-live-build', 'lg-live-image', 'lg-peruse-a-rue', 'lg_chef', 'lg_cms', 'playground']
Print input history, with most recent last.
In : %history 20-22 ls import datetime datetime.datetime.now()
%history ~1/4 #Line 4, from last session
This will list the previous session history.
This will upload the specifed input commands to Github’s Gist paste bin, and display the URL
It will upload the code as anonymous user
In : %pastebin [-d “Date Example”] 20-23 Out: u'https://gist.github.com/a660948b8323280a0d27'
For more info on this topic: