15 minute presentation on python?

Jim Dennis jimd at vega.starshine.org
Wed Feb 27 05:30:01 EST 2002


In article <3C7ADE12.C6D895A0 at engcorp.com>, Peter Hansen wrote:
> Emile van Sebille wrote:

>> "Joshua Muskovitz" <joshm at taconic.net> wrote in message
>> news:3c7ab974_4 at corp.newsgroups.com...
>>> Hey all,

>>> If you had to give a 15 minute introduction to Python, what would you
>> cover?
>>> What is a good example of a simple, but useful, text-based app for a
>> Wintel environment, that has good "wow" quality?

>> Hmm... text-based... "wow" appeal... oxymoron?  ;-)
>> Otherwise, what Sheila said.

> Something text-based that retrieves some useful information
> from the web (a weather site?) and parses the result and
> displays something useful with a few lines of Python code
> using the standard modules would have enough "wow" appeal
> for anyone used to other languages...

> At least, it "wow"ed me when I did this sort of thing
> from the interactive prompt for the first time... :)

> -Peter

 I agree with the first comments that ask: 

  	What is the purpose of your demonstration?
	What is the background of the audience?

 From what you've said, you're giving a presentation to a 
 group of (high school?) teachers.  Is this to assess your
 skills as an instructor?  Are they considering the suitability
 of Python for introductory programming classes? Are these 
 teachers familiar with modern programming (or are they stuck
 in the academic traditions of BASIC, and Pascal)?  Are they 
 likely to know about the Fibonacci series and it's traditional
 role as a very basic first term programming assignment?


 One cool "wow" demo script that I just whipped up is a simple
 threaded URL checker in about 50 lines of code.  This isn't fancy, 
 and I'm sure the local NG wizards can improve it immensely, but it 
 works and it makes for an impressive demonstration of how simply
 one can use Python's networking, HTTP/HTML and threading modules,
 and basic exception handling though it shows only the most rudimentary 
 data types (just a couple of lists).

 Here's the code:

#!/usr/bin/env python2.2
## Based on code from ASPN Python Cookbook (by Andy McKay)
##   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/101276
## JimD <jimd at starshine.org>: Wed Feb 27 02:26:08 PST 2002
##	- hacked to take command line args and be multi-threaded

from httplib import HTTP 
from urlparse import urlparse 
import threading 

class URLCheckResults:
	def __init__(self):
		self.lock=threading.Lock()
		self.failure=[]
		self.success=[]
	def succeed(self,url):
		self.lock.acquire()
		self.success.append(url)
		self.lock.release()
	def fail(self,url):
		self.lock.acquire()
		self.failure.append(url)
		self.lock.release()

class URLChecker(threading.Thread):
	def __init__(self, results, url):
		threading.Thread.__init__(self)
		self.url=url
		self.results=results
	def run(self): 
		print "start:\t%s" % repr(self)
		# Gratuitous tuple assignment/unpacking
		# <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
		(x, host, path, x, x, x) = urlparse(self.url)
		try:
			h = HTTP(host) 
			h.putrequest('HEAD', path) 
			h.endheaders() 
			if h.getreply()[0] == 200: self.results.succeed(self.url)
			else: self.results.fail(self.url)
		## except (IOError):
		except: 	# Warning! catchall!
			self.results.fail(self.url)
		print "end:  \t%s" % repr(self)

if __name__ == '__main__': 
	import sys
	results = URLCheckResults()
	threads = []
	for each in sys.argv[1:]:
		threads.append(URLChecker(results, each))
		threads[-1].start()
	for each in threads:
		each.join()
	for each in results.success:
		print "Reached: " + each
	for each in results.failure:
		print "Problem: " + each
	sys.exit(len(results.failure))

 
 It's pretty simplistic, but I was able to demonstrate it, and
 explain it to my wife (a programmer and webmaster, with no
 Python experience and just a little armchair book knowledge of
 its syntax) in under ten minutes.
 
 I think it's "cool" because it mixes a few hot buzzy programming
 needs into a small, fairly short space.  It shouldn't take much
 imagination for your audience to think of ways that a few hundred
 lines of additional code could classify the connection results
 into more categories based on other h.getresults() and more
 specific exceptions, use GET requests instead of HEADs, parse 
 results, URL encode arguments (form data) for GETs or POSTs, 
 etc.  You can spur those notions by mentioning these other module
 features in passing as you walk through the code example.

 This code also has some deliberate flaws, things that would be 
 worth discussing in a class as targets for refactoring.





More information about the Python-list mailing list