Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

.

.
Python: classes, modules and packages
.
.. .

.
Netsoc

Stephen Shaw

2010

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 1 / 25


Getting started

SSH to one of our servers


$ ssh you@login.netsoc.tcd.ie
PuTTY: Enter login.netsoc.tcd.ie as the hostname
NX on cube if you want - all you need is a shell though
No netsoc account?
CS: macneill.scss.tcd.ie
Maths servers?
Talk to an admin before you leave so you have an account for next time

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 2 / 25


The plan

Schedule
This week - Classes, modules, packages, regular expressions,
documentation, the PyPI
Week 3 - Miscellaneous fun with scientific computing
These slides: http://www.netsoc.tcd.ie/~stesh/python/2.pdf

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 3 / 25


Object-orientation

Python is very object-oriented (maybe even more than Java?)


Nearly everything is an object
Numbers are objects:

>>> from math import pi


>>> pi . is_integer ()
False

Classes are objects


Multiple inheritance - add as many superclasses as you want

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 4 / 25


Our first class
A silly example:
c l a s s Student ( object ) :
''' Represents a student '''
def __init__ ( self , name , age , course ) :
self . name = name
self . age = age
self . course = course

def do_assignment ( self , assignment ) :


f o r section i n assignment :
section . complete_dilligently ()

c l a s s Programmer ( student ) :
''' Represents a programmer '''
def __init__ ( self , name , age , course = CS ) :
super ( student , self ) . __init__ ( name , age , course ) :
self . lives_with_parents = True

def do_assignment ( self , assignment ) :


i f assignment . deadline () == today :
f o r section i n assignment :
section . do_hurriedly ()

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 5 / 25


Our first class

__init__ is the constructor


self refers to the current object
Have to pass self as an argument to method definitions:

def is_loser ( self ) :


return self . age > 40 and self . lives_with_parents ()

call methods like so:

>>> bob = Programmer ( " Bob " , 23)


>>> bob . is_loser ()
False

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 6 / 25


Modules

Organized bits of code that add functionality to Python


What is module?
a file with extension .py
That's it!
Even the tiniest script is a module
Thousands of modules out there:
CGI, MySQL, regular expressions, scientific computing
(scipy, numpy, sympy), graphics(PyGraph), GUIs (PyGTK), linguistics1
(nltk)
We'll look at a few of these today

1 Obligatory plug
Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 7 / 25
Modules

Subdivide your code into useful functions, etc.


Define a function called main() (or anything else that pleases you)

i f __name__ == ' __main__ ':


main ()

Only execute the main() function if the module is being run as a script

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 8 / 25


Modules
# !/ usr / bin / env python3

nato = ''' alpha bravo charlie delta echo foxtrot golf hotel india �
juliet
kilo lima mike november oscar papa quebec romeo sierra tango
uniform vector whiskey x - ray yankee zulu '''. split ()

def phon ( letter , alphabet = nato ) :


''' Return the phonetic representation of letter in alphabet '''
return { x [0]: x f o r x i n alphabet }[ letter ]

def transcribe ( utterance , alphabet = nato ) :


''' Return utterance transcribed in the given phonetic alphabet �
'''
return ' '. join ( phon (c , alphabet ) f o r c i n utterance . lower () i f c .�
isalpha () )

def main () :
p r i n t ( transcribe ( input ( ' utterance : ') ) )

i f __name__ == ' __main__ ':


main ()

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 9 / 25


Modules

to access a module, use the import keyword


import math - give me access to mathematical functions and constants
Now we can treat math like an object with methods:

>>> math . cos ( math . pi )


-1.0

>>> math . log (1)


0.0

Can also import specific things: from math import degrees


We'll see this in action with the re module, and learn about regular
expressions in the process

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 10 / 25


Regular expressions for mathematicians

Formal language theory


Mathematicians and computational linguists use regular expressions to
define regular sets
The same expressive power as regular grammmars
All regular expressions have a generatively-equivalent finite-state
automaton
Given a finite alphabet Σ, the empty set ∅, and the empty string ϵ, we
define the closure operators of concatenation, alternation (|),
Kleene-plus (+) and Kleene-star (∗) as follows. . .

. . . Who cares?
Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 11 / 25
Regular expressions for programmers

Programmers use regular expressions to match strings in text


'In this file, how many times does the letter a follow two or more
occurences of a number divisible by 3?'
'Is the email address this user just gave me valid?'
Can also capture matched strings and replace them
'Change all the semicolons to apostrophes'
'Please sanitize this $_POST so my webapp doesn't get pwned'
Not defined in python's syntax (c.f. perl, ruby)
Probably a good thing:

die u n l e s s chomp( $ line ) = ˜ s /(\ s +\ w +( <\ w + >) ? ,?)�


{1 ,}) ;/ gi ;

Made available in the re module instead

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 12 / 25


Some regular expressions

Expression What it recognizes


a a single occurrence of a
. a single occurrence of any character
a* zero or more occurrences of a
a+ one or more occurrences of a
a|b a single occurrence a or of b (but not both)
ab a single a followed by a single b
ab? a single a, optionally followed by a single b
(ab){1-3} one, two or three occurences of ab
ˆ[a-zA-Z0-9]+.*[4-9]?$ one or more alphanumeric characters at the
start of a string, followed by any number of
any character, optionally termined by some
integer between 4 and 9 inclusive

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 13 / 25


Who is logged in whose username begins with a vowel?

# !/ usr / bin / env python3 .1

import re
import os

begins_with_vowel = re . compile ( ' ˆ [ AEIOUaeiou ].* $ ')

people_online = os . popen ( ' who ')


f o r person i n people_online :
person = person . split ( ' ') [0]
i f re . match ( begins_with_vowel , person ) :
p r i n t ( person )

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 14 / 25


Show me all the links on some website
# !/ usr / bin / env python3

import re # for regular expressions


from sys import argv , stderr , exit # for system operations
import urllib . request # for deailing with http requests

# Get the URL from the command line


website = exit ( " Give me a URL ! " ) i f len ( argv ) < 2 e l s e argv [1]

# request data from the remote host


response = urllib . request . urlopen ( website )

# read the response and decode it to Unicode


data = response . read ()
html = data . decode ( 'utf -8 ')

# I ' ve probably got this wrong


urls = re . compile ( '[a - z ][\ w -]+:/{1 ,3}.[ a - z \ d .\ -]+[.][ a - z ]{2 ,4}/? ')

# find them all and print them


f o r link i n re . findall ( urls , html ) :
p r i n t ( link )

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 15 / 25


Know your limits

Regular expressions are good at


Being flexible
Being fast
Regular expressions are bad at
Natural languages
Most programming languages
HTML
Anything which relies on bracket balancing
You can prove that this is the case

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 16 / 25


Documentation

Programmers like documentation


Usually a pain
As painless as possible in python

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 17 / 25


Documentation

/* *
* Generate a random list of ShoppingItems from the given list ,
* of the given size .
*
* @param items
* @param desiredSize
* @return A ShoppingList object containing the given number of �
items
* @throws InsufficientItemsException if the given size is less
* than the total number of items available in the given list
*/
protected ShoppingList getShoppingList ( List < ShoppingItem > items , �
int
desiredSize ) ;

No.

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 18 / 25


Documentation
Python is self-documenting (almost)
def factorial ( n ) :
''' Return the factorial of n . This implementation is recursive

Arguments :
n -- a non - negative integer

Exceptions :
ValueError -- if n is negative

>>> factorial (7)


5040

>>> [ factorial ( n ) for n in range (10) ]


[1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880]

'''
i f n == 0:
return 1
else :
return n * factorial ( n - 1)

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 19 / 25


Documentation

A multiline string just after a class or function definition


Called a docstring
Documentation there for you when you're writing code
Call help(thing) to generate nice formatted documentation
pydoc somemodule on the command line generates documentation for
a module or package
A package is just a collection of modules
pydoc -p someport makes a nice web interface to your documentation
Just about everything in python is documented in this way

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 20 / 25


Doctests

This is really cool


Include example usage in function/method docstrings
Python can check if they do what you expect them to do
Like a baby unit test
It's common to end a module with

i f __name__ == ' __main__ ':


import doctest
doctest . testmod ()

Run the module as a script to test it

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 21 / 25


The PyPI

Python Package Index


The source for python software
Thousands of packages and modules
http://pypi.python.org/pypi
Installing a package: pip install <package>

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 22 / 25


2 or 3?

Python is in a bit of a crisis at the moment


Python 3 is the latest release
Deliberately not backwards-compatible
Cleaning up the language
Developers are afraid to move their code from python 2 to python 3
Hundreds of really awesome packages aren't available for python 3 yet
2to3 - attempt to convert python 2 code to python 3 code
3to2 - attempt to convert python 3 code to python 2 code
The way of the future!

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 23 / 25


Main differences
Python 2 Python 3
print statement print function
Byte strings Unicode strings (at last)
range returns a list range returns an iterator
/ returns floor for ints Single division operator
distinction between int and long no distinction
These are generally good things
Many python 3 features already in python 2.6 and up
Can access new features in the __future__ module

>>> 7 / 5
1
>>> from __future__ import division
>>> 7 / 5
1.3999999999999999

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 24 / 25


Phew

Next week we will look at some specific modules


numpy, scipy, sympy, matplotlib
Python is better than Microsoft excel
The iPython interpreter
Distributed computing?
These slides: http://www.netsoc.tcd.ie/~stesh/python/2.pdf
Last weeks' slides:
http://www.netsoc.tcd.ie/~stesh/python/1.pdf
Questions?

Netsoc (Stephen Shaw) <stesh@netsoc.tcd.ie> 2010 25 / 25

You might also like