[Edu-sig] A testing framework for new students of VPython

kirby urner kirby.urner at gmail.com
Sat Apr 28 03:01:11 CEST 2007


"""
Here's the kind of thing I'm using with my middle and high schoolers
in class tomorrow, to help 'em get their feet wet with Python +
VPython.

Students have already slogged through Dog and Monkey as subclasses of
Mammal before getting here.

We replace Mammal with Vector, conceptually, and talk more about
vectors as 'math objects' with a graphical representation, not just a
lexical one.

In stickworks.py, said Vector class is tail originating at (0,0,0),
but we're able to edge-connect any two vector tips with an Edge.

We use IDLE to import and run test(), with no need to close the
VPython window between loops.

The boilerplate the follows suggests easy extension to other VPython
primitives, i.e. provides a sandbox and just enough sand for beginning
coders to copy and/or elaborate upon.

"""


# ==== viztoyz.py =====

"""
Kirby Urner
4D Solutions
Apr 27 2007

Simple framework for studying VPython primitives plus
an imported Vector & Edge, wrapper classes from my stickworks.py
http://www.4dsolutions.net/ocn/stickworks.html
"""

from visual import sphere, color, display, cylinder
from random import randint
from stickworks import Vector, Edge

red = color.red
green = color.green
blue = color.blue

def makespheres(n=100):
    for i in range(n):  # a loop
        x, y, z = randint(-10, 10), randint(-10,10), randint(-10,10)
        theball = sphere(pos=(x,y,z),  # linebreak OK cuz within ( )
                         radius = 0.1 * randint(1,5),
                         color = [red, blue, green][randint(0,2)])

def makecyls(n=100):
    for i in range(n):  # a loop
        x, y, z = randint(-10, 10), randint(-10,10), randint(-10,10)
        thecyl = cylinder(pos=(0,0,0), axis=(x,y,z),
                          radius = 0.1 * randint(1,5), # decimalize
                          color = [red, blue, green][randint(0,2)])

def maketent():
    """
    Imagine a cube centered at the origin and with
    a positive octant vertex at (1,1,1).  Inscribe
    a regular tetrahedron as six face diagonals therein.
    """

    # 4 vertices
    a = Vector((-1, -1, 1))
    b = Vector((-1,  1, -1))
    c = Vector((1, 1, 1))
    d = Vector((1, -1, -1))

    # 6 edges
    tetrahedron = [Edge(a,b), Edge(b,c), Edge(c,d),
                   Edge(a,c), Edge(a,d), Edge(b,d)]

    for e in tetrahedron:
        e.draw()


def sometest():
    pass

def test():

    # create a scene window
    scene2 = display(title='Some VPython Tests',
        width=600, height=600,
        center=(0,0,0),
        background=(0,1,1))

    """list the functions"""
    thetests = [
        makespheres,  # balloon qyoob
        makecyls,     # silly cyls
        maketent,     # tetra tent
        sometest]     # user defined, could be any number

    while True:
        print """
        Choose:
        0  Balloon Qyoob
        1  Silly Cyls
        2  Tetra Tent
        3  ... your test goes here
        Q  Outta here!

        """

        ans = raw_input('Choice? ')

        if ans in 'Qq':
            break

        # trap more errors here

        thetests[int(ans)]()  # perform user selection (or crash?)

        print "View output, hit Enter to continue..."

        # pause to look in the VPython window
        ok = raw_input()

        for obj in scene2.objects:  # erase all objects
            obj.visible = False

    # outta here
    scene2.visible = False  # destroy the scene window
    return # null


More information about the Edu-sig mailing list