Implement the interpolate function, using the getchunks function, which has been provided for you.
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"""
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)
Expected output:
My name is Mr. Guido van Rossum, Esquire. I have visited the following cities: Amsterdam, LA, New York, DC, Chicago.
Hints:
Solution: interpolate2.py