Python Hour - 25 August 2014
-
Parker introduced us to the itertools.permutations function. You can use this function to return all possible permutations of an iterable object, such as a list. For example, the code snippet below returns a list of all the permutations of
0,1,2
:import itertools a = range(3) a_perm = list(itertools.permutations(a)) print a_perm
This will print:
[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0)]
This apparently comes in handy when designing a python script to play tic-tac-toe.
-
Parker also told us about the
KDTree
class from thescipy.spatial
module. KDTree provides efficient methods for finding the nearest-neighbors of a point (or a set of points) in a grid of spatial points. In particular,KDTree.query
is useful when trying to interpolate values on a grid where the spacings are uneven. An example of its usage is provided here. -
We discussed a few ways to debug code in ipython. First, there is the magic command
%debug
, which launches a post-mortem debugging session after an exception is thrown. That is, if a script crashes, running%debug
activates the python debugger (pbd) and brings you to the line in the script where the error happened. The pdb gives you a special command line interface to explore the namespace of the code, execute commands and set breakpoints. Another alternative is the Tracer object from theIPython.core.debugger
module. The Tracer object can be used to activate a pdb session from anywhere in a script, which means you can use it as a breakpoint. For example:from IPython.core.debugger import Tracer debug_here = Tracer() #later in code debug_here() #code stops here and pdb is launched.
-
We briefly talked about keyword arguments and the different ways we can supply inputs to a function. Most of what we discussed is covered in Section 4.7 on this python doc page. With keyword arguments, we can specify function arguments in any order, provided that the right keyword is used. Keyword arguments are particularly useful for setting default argument values. Here is an example:
def fun(x,y, col='r', lw=2): import matplotlib.pyplot as plt plt.figure() plt.plot(x, y, color=col, linewidth=lw) plt.show() #some good ways to call the function a = arange(10) b = a**2 fun(a,b) fun(a, b, color='violet') fun(a, b, 'orange', 3) fun(x=a, y=b) #using x and y as keywords fun(col='crimson', x=a, lw=3, y=b) #order of keyword arguments does not matter