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)
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()
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:
Post a Comment