[Edu-sig] More GnuMath stuff

kirby urner kirby.urner at gmail.com
Thu Sep 4 22:34:04 CEST 2008


This code segment is a bit on the esoteric side but shows how a
generator, an iterable like a list comprehension, may be pressed into
service to populate an SQL table using pysqlite's executemany command.

import rbf

def pop(con):
    cur = con.cursor()
    gen = (
           (str(v), rbf.Vset[v].x, rbf.Vset[v].y, rbf.Vset[v].z)
           for v in rbf.Vset
           if v in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           )
    cur.executemany("insert into Vectors(label, x, y, z) values (?, ?,
?, ?)", gen)
    con.commit()

Note we're not using %-operator dictionary substitution, but question
marks, with iterator as second parameter, playing the role of
tuple-generator (fill in the blanks).

What this looks like after running, in terms of raw data, is 26
"points of interest" used to anchor the aforementioned
whole-number-volumed polyhedra, tetrahedron as unit:

kirby at dell:~$ sqlite3 gnugeom
SQLite version 3.4.2
Enter ".help" for instructions
sqlite> select * from Vectors;
Z|0.707106781186547|0.707106781186547|0
A|0.353553390593274|0.353553390593274|0.353553390593274
C|-0.353553390593274|0.353553390593274|-0.353553390593274
B|-0.353553390593274|-0.353553390593274|0.353553390593274
E|-0.353553390593274|-0.353553390593274|-0.353553390593274
D|0.353553390593274|-0.353553390593274|-0.353553390593274
G|0.353553390593274|-0.353553390593274|0.353553390593274
F|0.353553390593274|0.353553390593274|-0.353553390593274
I|0|0|0.707106781186547
H|-0.353553390593274|0.353553390593274|0.353553390593274
K|0.707106781186547|0|0
J|0|0.707106781186547|0
M|0|-0.707106781186547|0
L|-0.707106781186547|0|0
O|0|0.707106781186547|0.707106781186547
N|0|0|-0.707106781186547
Q|-0.707106781186547|0|0.707106781186547
P|0.707106781186547|0|0.707106781186547
S|0|0.707106781186547|-0.707106781186547
R|0|-0.707106781186547|0.707106781186547
U|-0.707106781186547|0|-0.707106781186547
T|0.707106781186547|0|-0.707106781186547
W|-0.707106781186547|0.707106781186547|0
V|0|-0.707106781186547|-0.707106781186547
Y|0.707106781186547|-0.707106781186547|0
X|-0.707106781186547|-0.707106781186547|0
sqlite>

Here's a related graphic from:

Using Polyhedra to Teach OOP
and Coordinate Geometry Concepts
Chapter 7: Playing with Python
by Kirby Urner
First posted: August 28, 1999
Last modified: September 1, 1999
http://www.4dsolutions.net/ocn/oop7.html

A concept we're after here is primitive ORM (object-relational
mapping) in that we'll like have a Vector class, used to instantiate
each row of the Vectors table as a "vector object".  Vector objects
support __add__ and scalar multiplication (__mul__), plus may have a
self.draw() method for outputting to VPython, or some text file in
preparation for rendering.

Of course the whole Polyhedron might well be a class, probably is.
I'm not saying there's a one-size-fits-all solution here, would
encourage groups of students to cobble together their own databases in
most cases, at least smallish ones, as half the fun is using CREATE
TABLE, defining the relationships.

Note that Vectors are included in some secondary school level
curricula, though that's been increasingly pushed to remedial college
as the universal college expectation begets grade / degree inflation.

Our purpose here is to give high schoolers more opportunities as
job-ready entrepreneurs per the LEP High model (a charter school in
Portland).  Here's a paragraph I've shared with some of my
AlgebraFirst buddies:

"""
My angle is it's less the degree or credential that matters and more
the evidence of skills, something to brag about in a job interview,
that you know SQL, even though just fresh out of high school.  I'm
aiming towards a local economy in which a high school degree might be
sufficient again, for some entry level job, with college and company
life really a lot congruent in future, as work/study people farm
themselves out to this or that training, including history and
geography when traveling to that region... ends up being very like a
college degree, but measured more in promotions within the company (an
old template, no original thinking here, except it all sounds kinda
retro, now that we take quasi-universal college for granted).
"""

Those of you who've seen my Chicago talk (Pycon, on Showmedo), may
also remember TECC, another charter in the wannabe category, wondering
of all the spotlight on Wasilla with help Anna's cause at all.

So all the information you'd need to actually draw a Tetrahedron in
VPython could be garnered from some a SELECT statement such as the
following:

sqlite> select m.shortname, f.facet_id, f.vertex_id, v.x, v.y, v.z
from polyhedra m, facets f, vectors v where m.shortname = f.shortname
and f.label = v.label and m.shortname = "tetra";
tetra|0|0|0.353553390593274|0.353553390593274|0.353553390593274
tetra|0|1|-0.353553390593274|-0.353553390593274|0.353553390593274
tetra|0|2|-0.353553390593274|0.353553390593274|-0.353553390593274
tetra|1|0|0.353553390593274|0.353553390593274|0.353553390593274
tetra|1|1|-0.353553390593274|0.353553390593274|-0.353553390593274
tetra|1|2|0.353553390593274|-0.353553390593274|-0.353553390593274
tetra|2|0|0.353553390593274|0.353553390593274|0.353553390593274
tetra|2|1|0.353553390593274|-0.353553390593274|-0.353553390593274
tetra|2|2|-0.353553390593274|-0.353553390593274|0.353553390593274
tetra|3|0|-0.353553390593274|-0.353553390593274|0.353553390593274
tetra|3|1|-0.353553390593274|0.353553390593274|-0.353553390593274
tetra|3|2|0.353553390593274|-0.353553390593274|-0.353553390593274
sqlite>

For those less familiar with SQLite 3, it gives you a MySQL-like
administrative environment with Pysqlite2 the SQLite3 driver (yeah,
confusing).  You've got a pretty good implementation of SQL engine
syntax, so moving from this sandbox to something more robust is an
easy transition.  Some low volume websites never need to make the
transition.

Kirby


More information about the Edu-sig mailing list