[Edu-sig] Design patterns
Kirby Urner
urnerk at qwest.net
Wed Aug 24 16:52:36 CEST 2005
> -----Original Message-----
> From: edu-sig-bounces+urnerk=qwest.net at python.org [mailto:edu-sig-
> bounces+urnerk=qwest.net at python.org] On Behalf Of Chuck Allison
> Sent: Tuesday, August 23, 2005 6:19 PM
> To: Kirby Urner
> Cc: edu-sig at python.org
> Subject: Re: [Edu-sig] Design patterns
>
> Hello Kirby,
>
> Thanks for this good input (you too, Scott). My syllabus is at
> http://uvsc.freshsources.com/html/cns_439r.html. I like your point
> about cutesy having its place. I just don't see a way to use what's in
> HFDP as the basis for a programming assignment, but maybe it will come
> after a few days. As a professor, I like having my ducks in a row a
> priori, but that's not happening this first time around. Since I teach
> 3 upper division courses, I'm just a little nervous about having time
> to invent sufficient material. I was just hoping someone had had a
> similar teaching experience I could "borrow" from. All comments
> appreciated. Thanks again.
Hi Chuck --
I don't have HFDP in front if me, but if it does MVC at all, I can imagine
using the Triangle we've been discussing as a model, then adding viewer and
controller objects, making the triangle change in some way (controlling it),
while displaying (viewing it).
Here's some code, which as yet doesn't actually make use of VPython -- but
that's where it's going.
Note that my viewer expects to work with a 'shape' object, which is supposed
to have attributes verts (a dictionary of labeled vertices) and edges (a
list of vertex pairs, using those labels). Since my triangle object wasn't
written that way, I subclass a Triangle2 as TriAdapter, solely for the
purpose of making it behave like a shape.
Kirby
===
# uses code already/recently posted to this list for BaseTriangle
class Triangle2(BaseTriangle):
"""
Model
"""
@property
def coords(self):
return {'A':self._pA, 'B':self._pB, 'C':self._pC}
def _reset(self):
a,b,c = self.a, self.b, self.c # for brevity
self.perimeter = a + b + c
s = 0.5 * self.perimeter
self.area = math.sqrt(s * (s - a)*(s - b)*(s - c))
self.A = math.acos((-a**2 + b**2 + c**2)/(2.0*b*c))
self.B = math.acos(( a**2 - b**2 + c**2)/(2.0*a*c))
self.C = math.acos(( a**2 + b**2 - c**2)/(2.0*a*b))
self._pA = (0.0, 0.0, 0.0)
self._pB = (a , 0.0, 0.0)
self._pC = ((a**2 + b**2 - c**2)/(2.0*a),
math.sqrt((-a+b+c)*(a-b+c)*(a+b-c)*(a+b+c))/(2*a),
0.0)
def __mul__(self, scalar):
a = self.a * scalar
b = self.b * scalar
c = self.c * scalar
return self.__class__(a,b,c)
__rmul__ = __mul__
class TriAdapter(Triangle2):
"""
Make a triangle work like a Shape, i.e. add
edges and verts attributes
"""
def __init__(self, a,b,c):
Triangle2.__init__(self, a,b,c)
self.edges = [('A','B'),('B','C'),('C','A')]
@property
def verts(self):
return self.coords
class Pulser(object):
"""
A controller: makes a shape bigger in steps,
then smaller, back to original
"""
def __init__(self, shape, viewer):
self.shape = shape
self.viewer = viewer
def run(self):
for i in range(15):
self.viewer.display(self.shape)
self.shape = self.shape * 1.1
for i in range(16):
self.viewer.display(self.shape)
self.shape = self.shape * (1/1.1)
class Vpyview(object):
"""
A viewer: view using VPython -- very unfinished
"""
def display(self, shape):
self._showverts(shape.verts)
self._showedges(shape.edges)
def _showedges(self, edges):
# stub method
for e in edges:
print e
def _showverts(self, verts):
# stub method
for v in verts:
print v, verts[v]
def main():
"""
Main call sequence -- maybe loop through random
triangles eventually
"""
theviewer = Vpyview()
theshape = TriAdapter(3,3,3)
pulser = Pulser(theshape, theviewer)
pulser.run()
More information about the Edu-sig
mailing list