Friday, May 17, 2013
XML files. I use the ElementTree interface, which represents an
XML file as a tree of Element nodes. This interface is
excellent: it reads or writes trees with a single method call,
and has provisions for validating a document against its schema.
The old way
Usually, when I have needed to read an XML file, I built a Python
class that encapsulates the XML structure, and makes its content
available through attributes and method calls.
Case in point: I recently set up an app that scrapes the official
Banner class schedules for each semester and puts them up at a
given URL as an XML file. So for example the top-level class,
ClassSchedule, has a .timeStamp attribute that says when it was
scraped, and a .lookupSemester() method that selects a specific
semester's schedules. Helper classes represent the other
entities: SemesterSchedule, DeptSchedule, CourseSchedule, and
Now, if the data in such a Python object needs to be written back
out using the same XML schema, my usual approach has been to add
a .write() method that rebuilds the XML as an ElementTree and
then reserializes it back out as XML.
The new way
For a while now I've been toying with a different approach to
writing these XML interface classes.
Rather than writing code to convert the XML into an ElementTree,
convert the ElementTree to lists and hashes and so forth, and
then also writing code to convert the lists and hashes back to an
ElementTree and then serialize that back to XML, I thought, why
not use the ElementTree itself to hold all the data?
In this view, my classes would basically be thin wrappers around
Element nodes. My classes would still encapsulate the details
of the XML schema and provide access through attributes and
methods---so the external interface would not change---but
they would also provide methods that did things like search
the tree and read/write XML and so forth.
My first attempt uses this approach only for reading. You can
read the external spec and the internals, but it's not necessary
that you read these to comment intelligently on my design
question, which follows.
My current problem
My next interface is a read-write interface. It can read the
XML file, let you change the data, and then write it back out.
For a very simple case, consider a three-level structure: the XML
represents a department and has course children, and each course
has section children. So we make three wrapper classes called
Dept, Course, and Section, and each of these classes has a .node
attribute which is the corresponding Element node that contains
the actual data for that entity.
One purpose of this approach is to make the conversion of the
ElementTree back into XML trivial: it is literally one method
So the caller of the Python interface wants to add a new Section
child to an existing Course instance. In the underlying
ElementTree, this means that I'll create a new Section node and
add it as the next child of the Course node. That's very
Now let's consider the implementation of the Course.genSections()
method: given a Course instance, this generates all of its child
It's easy for the Course instance to go to its .node and
find all that node's child Element instances. But how do I
go backwards: given the Element node for a section, find
the Section instance that wraps that Element node, which is
what I want to give back to the caller?
The brute-force way is to break encapsulation of the Element
instance and cram a .myThing attribute into it that points back
at the corresponding wrapper instance. But as we highly evolved
software masters know, every time you break encapsulation, an
angel cries, and Satan drowns an innocent kitten. So clearly
that is not even worth considering.
My current solution is not that ugly, but it's pretty ugly.
Each Course instance maintains an internal list, ._sectionList,
that contains the child Section wrapper instances.
This list obviously has to have a 1-to-1 correspondence to
the actual nodes in the tree. Therefore every time I change
the set of child nodes in the tree, I have to make the
corresponding change in the wrapper class's list of wrapped
child nodes. This seems to me like not only a lot of extra
code but a lot of opportunity for things to get out of sync
and then Bad Things Happen.
Can anyone suggest a better approach? Think of the poor
Thursday, February 7, 2013
Tuesday, December 18, 2012
Arm the blastocysts!
Solves the abortion problem and the dreadful lack-of-guns problem!
With today's advanced microgenetics technology, clearly there's no good reason we can't modify the spermatozoon to deliver advanced defense technology to the egg along with the father's DNA. Think of it: we empower the zygote to decide for itself whether it wants to come to term. If it senses an impending dilation and curettage, it could go with the flow, or take arms against a sea of consequences.
Also, think of the profits we could make on the ensuing arms race. Better nanomissiles for the blastocyst, better defenses for the OB/GYN, and round and round we go.
Friday, November 9, 2012
Theory without practice is lame. Practice without theory is blind.Mr. Gaar, my ninth-grade science teacher, kept a copy of the famous quote from Albert Einstein prominently displayed in his classroom: “Science without religion is lame. Religion without science is blind.” Tonight as I was talking about the tension between theory and practice with some of Today's Young People, the above mutilation occurred to me. I like it better than the original.
Wednesday, October 3, 2012
I saw my first Common Poorwill (Phalaenoptilus nuttallii) in June 1985 in Water Canyon in the Magdalena Mountains west of Socorro, NM. It is a smaller relative of Whip-poor-wills and nighthawks. It is nocturnal and is often found sitting on the ground near dusk and dawn. I have never before managed to photograph one.
Tonight I had a wonderful bit of luck: around 8:30pm, there was one sitting on the street where I live. I grabbed my camera and managed to get this picture using the built-in flash.
Cute little guy, isn't he? About 8" long. As I drove away I found a second one half a block further on. It's fall migration; they must be moving.
Friday, August 10, 2012
I forget which guru, probably Stewart Brand, once suggested that it's a good idea to dive into a completely alien culture once in a while. Following that maxim, I've always made it a point occasionally to pick up a publication I wouldn't normally read, like Better Homes & Gardens, and really study it, just to get a viewpoint almost completely outside my normal orbit.
And just such an experience was my visit to the local nail salon today. I went there out of necessity, not curiosity: grubbing around in the back of a drawer the other day, I managed to ram the index finger of my dominant hand into a hidden projecting metal plate and rip up the nail. The fissure went longitudinally, then sideways, leaving me with a square chunk of nail hanging by a thread at one corner.
Fortunately some folks at our breakfast group recommended Socorro's USA Nails. I go in, tell them my problem, they make a nearly invisible repair that keeps the fragment in place, and charge me three dollars. I'm outta there maybe twenty minutes after I walk in there at midmorning without an appointment. I've always been curious about the world of nail salons, but it's a competitive market, and I figure that any place that's been in business for a while must be delivering the goods, because their clientele runs to the very finicky and also to those of limited means.
And now for the cultural anthropology payoff!
A couple of really attractively dressed and made-up women peered at me as if to say, “What planet are you from, young man?” As I'm currently 62, I don't get so much of the cougar action nowadays.
There was a young mom with her infant sleeping in the carrier. I didn't see any women leave without really attractive nails, although your definition of attractive may vary. As a card-carrying nerdette-chasing nerd, most of my dating history is with women who were pragmatists and kept their nails short and undecorated. So I haven't consorted that much with those that go for glitter or longer extensions or little computer chips that sing your birth-month's special anthem.
So what have you done lately that expanded your cultural horizon?
Wednesday, August 1, 2012
In May 1985 my neighbor Philip Johnson and I went birding on a fascinating little route that starts in San Antonio, NM, heads west through a slot canyon with vertical rock walls and a floor of sand, sometimes deep sand, and then meanders through rangelands for twenty miles or so and drops you back on US-60 at Box Canyon, about 15 miles west of Socorro.
On 2012-07-28 my friend Josef Brown, a math professor here at NM Tech, and I retraced it and were rewarded with another amazing day in the field. Formal bird notes for this interesting field day are elsewhere. Warning: Do not attempt this route without four-wheel drive. Wider tires, for good flotation in sand, are more helpful than high clearance. Here's the entrance to the slot canyon. I know nothing about the shrine's religious significance. Click on the thumbnail for a large version.
Much of this transect is open, pretty well overgrazed rangeland. However, just west of the slot canyon there is a tiny little oasis with several huge, ancient Arizona Walnuts (Juglans major). This species is actually native to the arid Southwest and evolved to fit the erratic but generally thin rainfall patterns. Here's a general shot of the grove. Figure this has been here for thousands of years.
Maybe fifty yards later, the road diverges from the streambed and heads west. The tree shown below is one of the largest walnuts. It is a tiny but complex ecosystem housing birds, lizards, chipmunks, and a sizeable bracket fungus, among others.
Most of the terrain looks like the photo below. The sign says, as far as I can tell, that it's 11 miles east to the Bianchi Ranch and the junction of I-25 and US-85, and 10 miles west to the Pound Ranch and US-60.
We didn't see anywhere near as many species as we would on even a slow summer day at the Bosque del Apache refuge, but what we did see were some rather special desert life forms. Like this Common Nighthawk (Chordeiles minor) dozing on a high branch.
Although I've specialized in bird photography since 1976, I'm always interested in all life forms: reptiles, invertebrates, wildflowers, even the occasional mammal. Lately, thanks to two excellent books I've recently acquired (details below), I've become a lot more aware of butterflies, and bird photo equipment is useful for shooting them. This Reikart's Blue (Echinargus isola) would fit on a dime and give you three cents back, so I'm delighted to get even a relatively cruddy flight shot. Here is one in flight, and one drinking from a muddy seep.
If you are at all interested in identifying butterflies, this field guide is comprehensive, beautiful, and fascinating.
Brock, Jim P., and Kenn Kaufman. Field guide to butterflies of North America. Houghton Mifflin, 2003, ISBN 978-0-618-76826-4.
An even more engrossing and beautiful book, this one emphasizes the complete ecological context of New Mexico's butterflies, reviewing them according to their habitat and life zones. How, for example, do pupas defend themselves against carnivorous ants? Much more depth than a field guide, and the kind of beautiful book that befits New Mexico magazine, the publisher. One great value of this work is that it tells you where to go, when, and the specific microhabitat: seeps, treetops, hilltops, specific plants.
Cary, Stephen J. Butterfly landscapes of New Mexico. New Mexico Magazine, 2009, ISBN 978-1934480038.