Upload
tcdnetsoc
View
185
Download
8
Embed Size (px)
DESCRIPTION
Python Tutorial #2
Citation preview
.
.
. ..
.
.
Python: classes, modules and packages
Netsoc
Stephen Shaw
2010
Netsoc (Stephen Shaw) <[email protected]> 2010 1 / 25
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 / 25
The plan
ScheduleThis week - Classes, modules, packages, regular expressions,documentation, the PyPIWeek 3 - Miscellaneous fun with scientific computing
These slides: http://www.netsoc.tcd.ie/~stesh/python/2.pdf
Netsoc (Stephen Shaw) <[email protected]> 2010 3 / 25
Object-orientation
Python is very object-oriented (maybe even more than Java?)
Nearly everything is an object
Numbers are objects:
1 >>> from math import pi2 >>> pi.is_integer ()3 False
Classes are objects
Multiple inheritance - add as many superclasses as you want
Netsoc (Stephen Shaw) <[email protected]> 2010 4 / 25
Our first class
A silly example:
1 c l a s s Student(object):2 ''' Represents a student '''3 def __init__(self , name , age , course):4 self.name = name5 self.age = age6 self.course = course7
8 def do_assignment(self , assignment):9 for section in assignment:
10 section.complete_dilligently ()11
12 c l a s s Programmer(student):13 ''' Represents a programmer '''14 def __init__(self , name , age , course=CS):15 super(student , self).__init__(name , age , course):16 self.lives_with_parents = True17
18 def do_assignment(self , assignment):19 i f assignment.deadline () == today:20 for section in assignment:21 section.do_hurriedly ()
Netsoc (Stephen Shaw) <[email protected]> 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:
1 def is_loser(self):2 return self.age > 40 and self.lives_with_parents ()
call methods like so:
1 >>> bob = Programmer("Bob", 23)2 >>> bob.is_loser ()3 False
Netsoc (Stephen Shaw) <[email protected]> 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 moduleThousands 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
1Obligatory plugNetsoc (Stephen Shaw) <[email protected]> 2010 7 / 25
Modules
Subdivide your code into useful functions, etc.
Define a function called main() (or anything else that pleases you)
1 i f __name__ == '__main__ ':2 main()
Only execute the main() function if the module is being run as a script
Netsoc (Stephen Shaw) <[email protected]> 2010 8 / 25
Modules
1 #!/usr/bin/env python32
3 nato = '''alpha bravo charlie delta echo foxtrot golf hotel india �juliet
4 kilo lima mike november oscar papa quebec romeo sierra tango5 uniform vector whiskey x-ray yankee zulu '''.split ()6
7
8 def phon(letter , alphabet=nato):9 ''' Return the phonetic representation of letter in alphabet '''
10 return {x[0]:x for x in alphabet }[ letter]11
12 def transcribe(utterance , alphabet=nato):13 ''' Return utterance transcribed in the given phonetic alphabet �
'''14 return ' '.join(phon(c, alphabet) for c in utterance.lower () i f c.�
isalpha ())15
16 def main():17 pr int (transcribe(input('utterance: ')))18
19 i f __name__ == '__main__ ':20 main()
Netsoc (Stephen Shaw) <[email protected]> 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:
1 >>> math.cos(math.pi)2 -1.03
4 >>> math.log (1)5 0.0
Can also import specific things: from math import degrees
We'll see this in action with the re module, and learn about regularexpressions in the process
Netsoc (Stephen Shaw) <[email protected]> 2010 10 / 25
Regular expressions for mathematicians
Formal language theory
Mathematicians and computational linguists use regular expressions todefine regular sets
The same expressive power as regular grammmars
All regular expressions have a generatively-equivalent finite-stateautomaton
Given a finite alphabet Σ, the empty set ∅, and the empty string ϵ, wedefine the closure operators of concatenation, alternation (|),Kleene-plus (+) and Kleene-star (∗) as follows. . .
. . .Who cares?
Netsoc (Stephen Shaw) <[email protected]> 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 moreoccurences 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:
1 die unless chomp($line) =˜ s /(\ s +\w+(<\w+>)?,?)�{1,});/gi;
Made available in the re module instead
Netsoc (Stephen Shaw) <[email protected]> 2010 12 / 25
Some regular expressions
Expression What it recognizesa a single occurrence of a. a single occurrence of any character
a* zero or more occurrences of aa+ 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 thestart of a string, followed by any number ofany character, optionally termined by someinteger between 4 and 9 inclusive
Netsoc (Stephen Shaw) <[email protected]> 2010 13 / 25
Who is logged in whose username begins with a vowel?
1 #!/usr/bin/env python3 .12
3 import re4 import os5
6 begins_with_vowel = re.compile('ˆ[ AEIOUaeiou ].*$')7
8 people_online = os.popen('who')9 for person in people_online:
10 person = person.split(' ')[0]11 i f re.match(begins_with_vowel , person):12 pr int (person)
Netsoc (Stephen Shaw) <[email protected]> 2010 14 / 25
Show me all the links on some website
1 #!/usr/bin/env python32
3 import re # for regular expressions4 from sys import argv , stderr , exit # for system operations5 import urllib.request # for deailing with http requests6
7 # Get the URL from the command line8 website = exit("Give me a URL!") i f len(argv) < 2 else argv [1]9
10 # request data from the remote host11 response = urllib.request.urlopen(website)12
13 # read the response and decode it to Unicode14 data = response.read()15 html = data.decode('utf -8')16
17 # I've probably got this wrong18 urls = re.compile('[a-z][\w -]+:/{1 ,3}.[a-z\d.\ -]+[.][a-z]{2 ,4}/?')19
20 # find them all and print them21 for link in re.findall(urls , html):22 pr int (link)
Netsoc (Stephen Shaw) <[email protected]> 2010 15 / 25
Know your limits
Regular expressions are good atBeing flexibleBeing fast
Regular expressions are bad atNatural languagesMost programming languagesHTMLAnything which relies on bracket balancing
You can prove that this is the case
Netsoc (Stephen Shaw) <[email protected]> 2010 16 / 25
Documentation
Programmers like documentation
Usually a pain
As painless as possible in python
Netsoc (Stephen Shaw) <[email protected]> 2010 17 / 25
Documentation
1 /**2 * Generate a random list of ShoppingItems from the given list ,3 * of the given size.4 *5 * @param items6 * @param desiredSize7 * @return A ShoppingList object containing the given number of �
items8 * @throws InsufficientItemsException if the given size is less9 * than the total number of items available in the given list
10 */11 protected ShoppingList getShoppingList(List <ShoppingItem > items , �
i n t12 desiredSize);
No.
Netsoc (Stephen Shaw) <[email protected]> 2010 18 / 25
Documentation
Python is self-documenting (almost)
1 def factorial(n):2 ''' Return the factorial of n. This implementation is recursive3
4 Arguments:5 n -- a non -negative integer6
7 Exceptions:8 ValueError -- if n is negative9
10 >>> factorial (7)11 504012
13 >>> [factorial(n) for n in range (10)]14 [1, 1, 2, 6, 24, 120, 720, 5040, 40320 , 362880]15
16 '''17 i f n == 0:18 return 119 else :20 return n * factorial(n - 1)
Netsoc (Stephen Shaw) <[email protected]> 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 fora 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) <[email protected]> 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
1 i f __name__ == '__main__ ':2 import doctest3 doctest.testmod ()
Run the module as a script to test it
Netsoc (Stephen Shaw) <[email protected]> 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) <[email protected]> 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) <[email protected]> 2010 23 / 25
Main differences
Python 2 Python 3print statement print functionByte strings Unicode strings (at last)range returns a list range returns an iterator/ returns floor for ints Single division operatordistinction 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
1 >>> 7 / 52 13 >>> from __future__ import division4 >>> 7 / 55 1.3999999999999999
Netsoc (Stephen Shaw) <[email protected]> 2010 24 / 25
Phew
Next week we will look at some specific modulesnumpy, scipy, sympy, matplotlibPython is better than Microsoft excelThe iPython interpreterDistributed 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) <[email protected]> 2010 25 / 25