Sunday, March 16, 2008

Frame Hacks

Someone (I forget who--I've listened to so many smart people here at PyCon) said yesterday that Python treats us like adults--if we want go "off-road" we can. (Other languages try to protect programmers from themselves and others. With Python we need to write tests to get that protection. Not that statically-typed languages with protected attributes and methods make testing unnecessary. But that's a subject for another time.) But until I took the "Secrets of the Framework Creators" tutorial at PyCon, I had no idea just how far into the wilderness one could go.

As Leihong and Kumar explain in the getting started chapter of the frame hacks section, one can call sys._getframe() to get the "frame" of the calling functions, all the way to the bottom of the call stack. This made immediate sense to me, since one of the first bits of real code I wrote when I started at Altera did a stack trace on the Nios (Classic) processor. (That was tricky, since the original Nios used register windows--but I'd better stop before I go down a rat hole.)

As I wrote previously, I'm working my way through the tutorial again on my own to give myself a chance to get to the point where I can use the techniques (rather than just understand them when I see them used).

I worked my way through the three steps of the first frame hacks recipe without a problem, though (to be honest), I've never used the string.Template class and the re.finditer function before (though they did seem familiar, so I guess I've read about them). I don't think I've used the yield statement either, though I'm eager to now that I've been immersed in generators and co-routines in the "Generator Tricks for Systems Programmers" tutorial (which was also excellent and which I'll write about later).

But I noticed that the code in the third step (step 2) of that recipe actually works without using sys._getframe():

SPOILER WARNING
import sys, re
from string import Template

def getchunks(s):
matches = list(re.finditer(r"\$\{(.*?)\}", s))

if matches:
pos = 0
for match in matches:
yield s[pos : match.start()]
yield [match.group(1)]
pos = match.end()
yield s[pos:]

def interpolate(templateStr):
result = ''
for chunk in getchunks(templateStr):
if isinstance(chunk, list):
result = ''.join((result, str(eval(chunk[0]))))
else:
result = ''.join((result, chunk))
return result

name = 'Guido van Rossum'
places = 'Amsterdam', 'LA', 'New York', 'DC', 'Chicago',

s = """My name is ${'Mr. ' + name + ', Esquire'}.

I have visited the following cities: ${', '.join(places)}.
"""

print interpolate(s)
That's because the eval in the interpolate function can find "name" and "places" in the global namespace. I can't recall if Leihong or Kumar noticed this in the tutorial, but I imagine they intended something like this:
import sys, re
from string import Template

def getchunks(s):
matches = list(re.finditer(r"\$\{(.*?)\}", s))

if matches:
pos = 0
for match in matches:
yield s[pos : match.start()]
yield [match.group(1)]
pos = match.end()
yield s[pos:]

def interpolate(templateStr):
"""Implement this function"""

def main():
name = 'Guido van Rossum'
places = 'Amsterdam', 'LA', 'New York', 'DC', 'Chicago',

s = """My name is ${'Mr. ' + name + ', Esquire'}.

I have visited the following cities: ${', '.join(places)}.
"""
print interpolate(s)

if __name__ == '__main__':
main()
You'll need to use sys._getframe() in interpolate() to get this to work.

TODO

I should look into Trellis, Elixer and lxml, which were used in the tutorial to give use cases for frame hacks.

And before I move on to the next section of the "Secrets of the Framework Creators" tutorial, I should describe the string.Template class and the re.finditer function. Stay tuned.

No comments: