17
Dojo: Python for Web Interaction Rob Sanderson, Ed Summers Dev8D, Feb 24-27 2010, London Rob Sanderson [email protected] ‐ @azaroth42 Ed Summers [email protected] ‐ @edsu http://www.flickr.com/photos/42311564@N00/2355590274/

Python Web Tutorial

Embed Size (px)

DESCRIPTION

Python Not-Quite-A-Dojo slides from Dev8D

Citation preview

Page 1: Python Web Tutorial

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

RobSanderson‐[email protected]‐@azaroth42

EdSummers‐[email protected]‐@edsu

http://www.flickr.com/photos/42311564@N00/2355590274/

Page 2: Python Web Tutorial

Overview

Python Dojo

•  Python Introduction

•  Web Programming Kata: •  Find bands on Wikipedia •  Find nationality of band •  Find population of nation •  Create chart of bands normalized by population

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 3: Python Web Tutorial

Python: Data Types

>>> ‘string’ “string” # strings are immutable >>> u’unicode: \xe4‘ # ä >>> “””string that can have multiple lines”””

>>> 1 # int >>> 1L # long int >>> 1.0 # float

>>> [list, of, items] # mutable >>> (tuple, of, items) # immutable

>>> {key : value, key2 : value2, …}

>>> None

>>> True, False # booleans

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 4: Python Web Tutorial

Python: Flow Control

>>> for x in iterable: ... # process x

>>> if test or test2 and not test3: ... # do something ... elif test4: ... # do something else ... else: ... # otherwise do this

>>> while test: ... # do something

>>> try: ... # try something ... except ExceptionType: ... # recover

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 5: Python Web Tutorial

Python: Classes, Functions

>>> def functionName(arg1, arg2=default): ... # do something

>>> class ClassName (ParentClass): ... property = value ... ... def __init__(self): ... # initialize ... ... def method(self, arg1, arg2=default): ... # do something

>>> what = ClassName() >>> what.method(“arg1”, arg2=“arg2”)

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 6: Python Web Tutorial

Python: Other Important Stuff

>>> break

>>> continue

>>> raise Exception

>>> return value

>>> pass # no operation

>>> import module >>> from module import something >>> from module import * # import everything

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 7: Python Web Tutorial

Python Master Class: Basics

>>> ‘abcd’[0] = ‘z’ # error, as strings are immutable

>>> ‘’.join([list of strings]) # create string from list

>>> var = [x for x in iterable] # list comprehension

>>> a, b, c = (“a”, “b”, “c”) # scattered assignment

>>> var = a if test else b # ternary operator (test ? a : b)

>>> anonFn = lambda x: x+1 # anonFn(1) 2

>>> var = Function # var() run Function >>> var = Class # var() new instance of Class

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 8: Python Web Tutorial

Python Master Class: Iterators

class Reverse: "Iterator for looping over a sequence backwards" def __init__(self, data): self.data = data self.index = len(data) def __iter__(self): return self def next(self): if self.index == 0: raise StopIteration self.index = self.index - 1 return self.data[self.index]

>>> for char in Reverse('spam'): ... print char m a p s

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 9: Python Web Tutorial

Python Master Class: Generators

def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index]

>>> for char in reverse('golf'): ... print char ... f l o g

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 10: Python Web Tutorial

urllib

>>> import urllib >>> urllib.quote('~azaroth/s?q=http://foo.com/') '%7Eazaroth/s%3Fq%3Dhttp%3A//foo.com/'

>>> urllib.unquote('%7Eazaroth/s%3Fq%3Dhttp%3A//foo.com/') '~azaroth/s?q=http://foo.com/'

>>> fh = urllib.urlopen('http://www.google.com/') >>> html = fh.read() >>> fh.close()

>>> fh.getcode() 200 >>> fh.headers.dict['content-type'] 'text/html; charset=ISO-8859-1'

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

Page 11: Python Web Tutorial

urllib2

>>> import urllib2 >>> ph = urllib2.ProxyHandler(

{'http' : 'http://proxyout.lanl.gov:8080/'}) >>> opener = urllib2.build_opener(ph) >>> urllib2.install_opener(opener) >>> # From now on, all requests will go through proxy

>>> r = urllib2.Request('http://www.google.com/') >>> r.add_header('Referrer', 'http://www.somewhere.net') >>> fh = urllib2.urlopen(r) >>> html = fh.read() >>> fh.close()

>>> # fh is the same as urllib's for headers/status

Dojo: Python for Web Interaction Rob Sanderson, Ed Summerrs

Dev8D, Feb 24-27 2010, London

Page 12: Python Web Tutorial

urlparse

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

>>> import urlparse >>> pr = urlparse.urlparse( 'https://www.google.com/search?q=foo&bar=bz#frag')

>>> pr.scheme 'https' >>> pr.hostname 'www.google.com' >>> pr.path '/search' >>> pr.query 'q=foo&bar=bz' >>> pr.fragment 'frag'

Page 13: Python Web Tutorial

httplib

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

>>> import httplib >>> cxn = httplib.HTTPConnection('www.google.com') >>> hdrs = {'Accept' : 'application/rdf+xml'} >>> path = "/search?q=some+search+query"

>>> cxn.request("HEAD", path, headers=hdrs) >>> resp = cxn.getresponse()

>>> resp.status 200 >>> resp_hdrs = dict(resp.getheaders()) >>> resp_hdrs['content-type'] # :( 'text/html; charset=ISO-8859-1'

>>> data = resp.read() >>> cxn.close()

Page 14: Python Web Tutorial

lxml

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

$ easy_install lxml

>>> from lxml import etree >>> et = etree.XML('<a b="B"> A <c>C</c> </a>') >>> et.text ' A ' >>> et.attrib['b'] 'B' >>> for elem in et.iterchildren(): ... print elem <Element c at 16d1ed0>

>>> html = etree.parse(StringIO.StringIO("<html><p>hi"), parser=etree.HTMLParser()) >>> html.xpath('/html/body/p') [<Element p at 16e00f0>]

Page 15: Python Web Tutorial

rdflib

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

$ easy_install rdflib

>>> import rdflib as rdf >>> inp = rdf.URLInputSource(

'http://xmlns.com/foaf/spec/20100101.rdf') >>> inp2 = rdf.StringInputSource("<a> <b> <c> .") >>> graph = rdf.ConjunctiveGraph() >>> graph.parse(inp)

>>> sparql = "SELECT ?l WHERE {?w rdfs:label ?l . }" >>> res = graph.query(sparql, initNs={'rdfs':rdf.RDFS.RDFSNS})) >>> res.selected[0] rdf.Literal(u'Given name')

>>> nt = graph.serialize(format='nt')

Page 16: Python Web Tutorial

json / simplejson

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

>>> try: import simplejson as json ... except ImportError: import json

>>> data = {'o' : (True, None, 1.0), "ints" : [1,2,3]} >>> json.dumps(data) '{"o": [true, null, 1.0], "ints": [1, 2, 3]}'

>>> json.dumps(data, separators=(',', ':')) # compact '{"o":[true,null,1.0],"ints":[1,2,3]}'

>>> json.loads('[1,2,"foo",null]') [1, 2, u'foo', None]

Page 17: Python Web Tutorial

mod_python, mod_wsgi

Dojo: Python for Web Interaction Rob Sanderson, Ed Summers

Dev8D, Feb 24-27 2010, London

import cgitb from mod_python import apache from mod_python.util import FieldStorage

def handler(req): try: form = FieldStorage(req) # dict-like object for query path = req.uri req.status = 200 req.content_type = "text/plain" req.send_http_header() req.write(path) except: req.content_type = "text/html" cgitb.Hook(file=req).handle() return apache.OK