[Edu-sig] More on intro to Python (today's 3 hr training)

Kirby Urner urnerk at qwest.net
Tue Mar 29 05:05:39 CEST 2005


So below is a module I tossed up on the big screen in my hotel conference
room (the typical scene:  breakout rooms, a vendor/exhibitor space, big
lunch room with an interesting talk (and it *was* very interesting)).

This was probably the most advanced topic I dealt with (decorators) and I
didn't even show 'em their archetypal context, inside of classes (talked
about it though). But given these were GIS folks (very visual), I thought
this'd be a good excuse to at least throw out a couple plots (graphs of
curves and their derivatives).

I introduced decorators under the heading of "functions as top level" i.e.
passable as arguments, valid as returned objects, allowing factory functions
returning closures and blah blah.

The itertools import shouldn't be there.  I was experimenting with giving
matplotlib generators to munch on, but that failed -- I think it wants to
see the whole list first, to figure out scaling.  Makes sense.

I quick showed my hypertoon (under a minute I think), just to show off what
add-ons can do (i.e. VPython).  Didn't even get to my example of using ADO
to add a record to the Products table in Northwind.mdb.  Pity.  And for some
reason my feeble pre-talk attempt to open a Word document using COM crashed
Python after opening said Greeting.doc (a problem with wrapping Word Objects
11.0 in Office 2003, or is it just me?).

Speaking of hypertoons, Scott sent me a cool refactoring of said module
using decorators.  I just got it this AM and haven't gotten around to
actually studying the revisions, but I plan to.  That'd really break 2.3
compatibility, but I have reason to think 2.3 isn't showing some of the
cartoons properly anyway.

BTW, 2.4 experimental VPython failed to work with gcurve in my experiments
-- I was going to use its plotting facility in the demo, but ended up
switching to matplotlib for this very reason.  Could be that I just didn't
know how to use it right?  If anyone has a working gcurve plot in VPython
2.4 experimental, I'd like to test it on my setup.

Kirby

=============================

# Decorators (new in 2.4)
from itertools import izip
import math
from pylab import arange, plot, xlabel, ylabel, title, grid, show

def adderfactory(n):
    """
    Return an adder function that adds n to its argument
    """
    def adder(x):
        return x + n
    return adder

def test0():
    add2  = adderfactory(2)
    add10 = adderfactory(10)
    print "add2(17) : %s"  % add2(17)
    print "add10(17): %s" % add10(17)
    
def derivative(f):
    """
    Factory function:  accepts a function, returns a closure
    """
    def df(x, h=1e-8):
        return (f(x + h) - f(x))/h
    return df

def g(x):      return x*x
def curve(x):  return pow(x, 3)

@derivative    # same as dg = derivative(dg)
def dg(x):     return x*x

@derivative    # same as dcurve = derivative(dcurve)
def dcurve(x): return x*x*x

@derivative 
def dcos(x):  return math.cos(x)

def test1(a=-5,b=6):
    print "g(x) : %s" % [g(x) for x in range(a,b)]
    print "dg(x): %s" % [round(dg(x),2) for x in range(a,b)]
    print "="*30
    print "curve(x) : %s" % [curve(x) for x in range(a,b)]
    print "dcurve(x): %s" % [round(dcurve(x),2) for x in range(a,b)]

def test2():
        """
    Use matplotlib to plot graph
    """
    x  = arange(-10,10,0.1)
    y  = [curve(i) for i in x]
    dy = [dcurve(i) for i in x]
    plot(x, y,  linewidth=1.0)
    plot(x, dy, linewidth=1.0)
    
    xlabel('x')
    ylabel('y')
    
    title('Graph of x**3 & 3*x**2')
    grid(True)
    show()
    
def test3():
    """
    Use matplotlib to plot graph
    """
    x  = arange(-10,10,0.1)
    y  = [dcos(i) for i in x]
    dy = [math.cos(i) for i in x]    
    plot(x,  y, linewidth=1.0)
    plot(x, dy, linewidth=1.0)    
    xlabel('x')
    ylabel('y')
    title('Graph of cos & dcos(x)')
    grid(True)
    show()




More information about the Edu-sig mailing list