Python Tutorial #3

23
. . . Python: Interactive computing with bpython and IPython Netsoc Stephen Shaw 2010 Netsoc (Stephen Shaw) <[email protected]> 2010 1 / 23

description

Python Tutorial #3

Transcript of Python Tutorial #3

Page 1: Python Tutorial #3

.

.

. ..

.

.

Python: Interactive computing with bpython and IPython

Netsoc

Stephen Shaw

2010

Netsoc (Stephen Shaw) <[email protected]> 2010 1 / 23

Page 2: Python Tutorial #3

Getting started

SSH to one of our servers$ ssh [email protected]: Enter login.netsoc.tcd.ie as the hostnameNX on cube if you want - all you need is a shell though

No netsoc account?CS: macneill.scss.tcd.ieMaths servers?Talk to an admin before you leave so you have an account for next time

Netsoc (Stephen Shaw) <[email protected]> 2010 2 / 23

Page 3: Python Tutorial #3

The plan

ScheduleThis week - The bpython interpreter, the IPython interpreter, scipy,numpy, sympy, pylab

These slides: http://www.netsoc.tcd.ie/tutorials/2011/02/08/python-tutorial-3/

Netsoc (Stephen Shaw) <[email protected]> 2010 3 / 23

Page 4: Python Tutorial #3

Interactive computing

As we've seen, the REPL is great for playing around with python quickly

Standard REPL is quite minimal

Gets a bit tedious after a while

We can do a lot better

Netsoc (Stephen Shaw) <[email protected]> 2010 4 / 23

Page 5: Python Tutorial #3

bpython

bpython is a fancy interface to the REPLDoes all sorts of cool things:

syntax highlightingtab completiondocumentation viewingrecording and playbackSaving history to diskPosting to pastebin

It iscustomizablefreeeasy to set upinstalled on cube

Netsoc (Stephen Shaw) <[email protected]> 2010 5 / 23

Page 6: Python Tutorial #3

IPython

bpython is great, but IPython is even better

A completely new REPL, designed specifically for scientific computing

Object introspection (for tab completion)

macros

System shell access - ipython -p sh

Can be embedded within other applications

Pretty exception tracebacks

Really nice interface to pdb, the python debugger

Netsoc (Stephen Shaw) <[email protected]> 2010 6 / 23

Page 7: Python Tutorial #3

IPython commands

IPython adds magic functions to python's standard lexicon

Magic functions are used to control IPython itself

preceded with a %Crack open an object with ? and ??

Not quite punching the duck

Usual tab completion

You can embed an IPython shell in your own programs

We'll see this later

Netsoc (Stephen Shaw) <[email protected]> 2010 7 / 23

Page 8: Python Tutorial #3

Magic Functions

Command What it does%Pprint toggle pretty-printing

%autoindent toggle automatic indentation%autocall call functions without parentheses

%bg send a job to a separate thread%cpaste paste in code from the clipboard%debug activate the interactive debugger%edit start your $EDITOR%hist display your command history

%macro define a macro%p shorthand for python's print

%prun profile a code block%psearch search for an object in the current namespace

%pycat syntax-highlight a code file%run run a module inside IPython

Netsoc (Stephen Shaw) <[email protected]> 2010 8 / 23

Page 9: Python Tutorial #3

Introspection

In the regular REPL, can get information about an object with dir():

1 Python 2.6.6 (r266 :84292 , Dec 26 2010, 22:31:48)2 [GCC 4.4.5] on linux23 Type "help", "copyright", "credits" or "license" for more information.4 >>> import sys5 >>> dir(sys)6 ['__displayhook__ ', '__doc__ ', '__excepthook__ ', '__name__ ', '�

__package__ ', '__stderr__ ', '__stdin__ ', '__stdout__ ', '�_clear_type_cache ', '_current_frames ', '_getframe ', 'api_version ',�'argv', 'builtin_module_names ', 'byteorder ', 'call_tracing ', '�

callstats ', 'copyright ', 'displayhook ', 'dont_write_bytecode ', '�exc_clear ', 'exc_info ', 'exc_type ', 'excepthook ', 'exec_prefix ', '�executable ', 'exit ', 'flags ', 'float_info ', 'getcheckinterval ', '�getdefaultencoding ', 'getdlopenflags ', 'getfilesystemencoding ', '�getprofile ', 'getrecursionlimit ', 'getrefcount ', 'getsizeof ', '�gettrace ', 'hexversion ', 'maxint ', 'maxsize ', 'maxunicode ', '�meta_path ', 'modules ', 'path', 'path_hooks ', 'path_importer_cache '�, 'platform ', 'prefix ', 'ps1', 'ps2', 'py3kwarning ', 'pydebug ', '�setcheckinterval ', 'setdlopenflags ', 'setprofile ', '�setrecursionlimit ', 'settrace ', 'stderr ', 'stdin ', 'stdout ', '�subversion ', 'version ', 'version_info ', 'warnoptions ']

Netsoc (Stephen Shaw) <[email protected]> 2010 9 / 23

Page 10: Python Tutorial #3

Introspection

MessyMuch better in IPython:

1 In [1]: import sys2 In [2]: sys?3 Type: module4 Base Class: <type 'module '>5 String Form: <module 'sys' (built - in )>6 Namespace: Interactive7 Docstring:8 This module provides access to some objects used or maintained by the9 interpreter and to functions that interact strongly with the �

interpreter.10

11 Dynamic objects:12

13 argv -- command line arguments; argv [0] i s the script pathname i f �known

14 path -- module search path; path [0] i s the script directory , else �''

15 modules -- dictionary of loaded modules16 ...

Netsoc (Stephen Shaw) <[email protected]> 2010 10 / 23

Page 11: Python Tutorial #3

Aliases

Run a command in your shell by prefixing it with !:

1 In [1]: !uptime2 15:10:29 up 143 days , 23:12 , 128 users , load �

average: 1.44, 1.24, 1.08

Can start a whole shell this way:

1 In [1]: !zsh2 stesh@cube :˜ [21:14]% echo $SHELL3 /usr/bin/zsh

Capture STDOUT by assigning to a variable:

1 In [2]: dirs = !ls /

Netsoc (Stephen Shaw) <[email protected]> 2010 11 / 23

Page 12: Python Tutorial #3

History

A lot of advanced functionality relies on history

1 In [1]: for i in range (99,0,-1):2 ...: pr int '%d bottles of beer on the wall ' % i3 ...:4 ...:5 # A lot of output6

7 In [2]: %hist8 1:9 for i in range (99,0,-1):

10 pr int '%d bottles of beer on the wall' % i11

12

13 2: _ip.magic("hist ")

We can modify previous lines in an editor:

1 In [3]: %ed 1:22 IPython will make a temporary file named: /tmp/ipython_edit_am8xPW.py3 Editing ... done. Executing edited code ...

Netsoc (Stephen Shaw) <[email protected]> 2010 12 / 23

Page 13: Python Tutorial #3

Macros

`Shorthand' for a sequence of lines

%macro <macro name> <history line number(s)>

1 In [4]: %macro bottles 12 Macro `bottles ` created. To execute , type its name �

(without quotes).3 Macro contents:4 for i in range (99,0,-1):5 pr int '%d bottles of beer on the wall ' % i

Can now run this entire block of code by just typing bottles

Macros can have arguments too

store them in a global list called _margv

Netsoc (Stephen Shaw) <[email protected]> 2010 13 / 23

Page 14: Python Tutorial #3

NumPy

IPython is ideal for once-off computations

A number of packages are especially suited to this

NumPy is a package with fast implementations of common mathematicalalgorithms

Really efficient n-ary array class and associated operations

Standard operations on matrices: norm, inverse, determinant,exponentation

Gauss-jordan elimination, Eigenvalues, eigenvectors, QR decomposition,Cholesky decomposition

Fast Fourier Transform

Netsoc (Stephen Shaw) <[email protected]> 2010 14 / 23

Page 15: Python Tutorial #3

Playing with matrices

Create the matrix A =

1 2 34 5 56 6 8

, and compute its transpose AT

1 In [1]: import numpy2

3 In [2]: from numpy import *4

5 In [3]: A = matrix('1 2 3; 4 5 5; 6 6 8')6

7 In [4]: A8 Out [4]:9 matrix ([[1, 2, 3],

10 [4, 5, 5],11 [6, 6, 8]])12

13 In [5]: A.T14 Out [5]:15 matrix ([[1, 4, 6],16 [2, 5, 6],17 [3, 5, 8]])

Netsoc (Stephen Shaw) <[email protected]> 2010 15 / 23

Page 16: Python Tutorial #3

Playing with matrices

1 In [6]: B = matrix('4 5 7; 2 9 5; 7 2 5')2

3 In [7]: A * B4 Out [7]:5 matrix ([[ 29, 29, 32],6 [ 61, 75, 78],7 [ 92, 100, 112]])8

9 In [8]: numpy.linalg.solve(A,B)10 Out [8]:11 matrix ([[ -0.75 , -4.83333333 , -4.58333333] ,12 [ -1.75 , 7.16666667 , 2.41666667] ,13 [ 2.75 , -1.5 , 2.25 ]])

solve is a function with takes two non-singular square matrices A andB, and returns the matrix x such that Ax = B

Netsoc (Stephen Shaw) <[email protected]> 2010 16 / 23

Page 17: Python Tutorial #3

SymPy

SymPy lets you do symbolic computations

You declare mathematical symbols, then call functions on them

1 In [1]: from sympy import *2

3 In [2]: x, y = Symbol('x'), Symbol('y')4

5 In [3]: y = x**2 + 4*x - 126

7 In [4]: solve(y,x)8 Out [4]: [2, -6]9

10 In [5]: diff(y,x)11 Out [5]: 4 + 2*x12

13 In [6]: integrate(y,x)14 Out [6]: -12*x + 2*x**2 + (1/3)*x**3

Netsoc (Stephen Shaw) <[email protected]> 2010 17 / 23

Page 18: Python Tutorial #3

SymPy

solve(y, x) - solve for y with respect to x

diff(y, x) - differentiate y with respect to x

integrate(y, x) - integrate y with respect to x

Declaring Symbols explicitly is a pain

isympy is a program which embeds an IPython shell, and declares all theconstants for you

Netsoc (Stephen Shaw) <[email protected]> 2010 18 / 23

Page 19: Python Tutorial #3

NetworkX

High-performance graph library

Useful for anything from network topology to linguistic analysis

Supports undirected graphs, directed graphs, multigraphs, etc.

Use any hashable object as vertex

Use any hashable object as a labelLoads of algorithms:

TraversalCentralityCycle detectionShortest paths (including A∗)Isomorphisms1

1Can only tell you if two graphs definitely aren't isomorphic - determining if they definitelyare is very complicated (not known to be NP complete!)

Netsoc (Stephen Shaw) <[email protected]> 2010 19 / 23

Page 20: Python Tutorial #3

A simple graph

1 In [1]: import networkx as nx2

3 In [2]: G = nx.Graph ()4

5 In [3]: G.add_nodes_from ([1,2,3,4,5,6,7,8])6

7 In [4]: G.add_edges_from ([(1 ,4) ,(6,7) ,(6,8) ,(3,4) ,(3,7) ,(1,8) ,(1,7)])8

9 In [5]: G.number_of_nodes ()10 Out [5]: 811

12 In [6]: G[1]13 Out [6]: {4: {}, 7: {}, 8: {}}14

15 In [7]: nx.degree(G,6)16 Out [7]: 2

Netsoc (Stephen Shaw) <[email protected]> 2010 20 / 23

Page 21: Python Tutorial #3

Plotting graphs

MatPlotLib and PyLab are `standard' plotting tools

Very high quality results

Several formats

1 In [8]: import matplotlib.pyplot as plt2 In [9]: nx.draw(G)

You can view plots on screen, or write them to any of several formats

Can customize colors, thickness, fonts, etc.

Netsoc (Stephen Shaw) <[email protected]> 2010 21 / 23

Page 22: Python Tutorial #3

Plotting graphs

Netsoc (Stephen Shaw) <[email protected]> 2010 22 / 23

Page 23: Python Tutorial #3

Annnyway...

We have only scratched the surface

Small-scale

IPython is really scalable: can drive entire clusters

All of these packages and modules are freely available

Questions?

Netsoc (Stephen Shaw) <[email protected]> 2010 23 / 23