extracting string.Template substitution placeholders

E

Eric S. Johansson

As part of speech recognition accessibility tools that I'm building, I'm
using string.Template. In order to construct on-the-fly grammar, I need
to know all of the identifiers before the template is filled in. what is
the best way to do this?

can string.Template handle recursive expansion i.e. an identifier
contains a template.

Thanks
--- eric
 
S

Steven D'Aprano

As part of speech recognition accessibility tools that I'm building, I'm
using string.Template. In order to construct on-the-fly grammar, I need
to know all of the identifiers before the template is filled in. what is
the best way to do this?


py> import string
py> t = string.Template("$sub some $text $here")
py> t.template
'$sub some $text $here'

Now just walk the template for $ signs. Watch out for $$ which escapes
the dollar sign. Here's a baby parser:

def get_next(text, start=0):
while True:
i = text.find("$", start)
if i == -1:
return
if text[i:i+2] == '$$':
start += i
continue
j = text.find(' ', i)
if j == -1:
j = len(text)
assert i < j
return (text[i:j], j)

start = 0
while start < len(t.template):
word, start = get_next(t.template, start)
print(word)

can string.Template handle recursive expansion i.e. an identifier
contains a template.

If you mean, recursive expand the template until there's nothing left to
substitute, then no, not directly. You would have to manually expand the
template yourself.
 
E

Eric S. Johansson

On Sun, 12 Jan 2014 10:08:31 -0500, Eric S. Johansson wrote:


Now just walk the template for $ signs. Watch out for $$ which escapes
the dollar sign. Here's a baby parser:
found a different way

import string
cmplxstr="""a simple $string a longer $string a $last line"""
nst=string.Template(cmplxstr)

identifiers = {}

while True:
try:
result = nst.substitute(identifiers)
except KeyError, error:
print error
identifiers[error[0]] = "x"
else:
break
print "loop done"
 
G

gmflanagan

As part of speech recognition accessibility tools that I'm building, I'm

using string.Template. In order to construct on-the-fly grammar, I need

to know all of the identifiers before the template is filled in. what is

the best way to do this?

Try this:

import string
cmplxstr="""a simple $string a longer $string a $last line ${another} one"""

def finditer(s):
for match in string.Template.pattern.finditer(s):
arg = match.group('braced') or match.group('named')
if arg:
yield arg


if __name__ == '__main__':
print set(finditer(cmplxstr))
 
M

Mark Lawrence

Try this:

import string
cmplxstr="""a simple $string a longer $string a $last line ${another} one"""

def finditer(s):
for match in string.Template.pattern.finditer(s):
arg = match.group('braced') or match.group('named')
if arg:
yield arg


if __name__ == '__main__':
print set(finditer(cmplxstr))

Would you please read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the
double line spacing above, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top