Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extracting string.Template substitution placeholders

50 views
Skip to first unread message

Eric S. Johansson

unread,
Jan 12, 2014, 10:08:31 AM1/12/14
to pytho...@python.org
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

Steven D'Aprano

unread,
Jan 13, 2014, 2:24:30 AM1/13/14
to
On Sun, 12 Jan 2014 10:08:31 -0500, Eric S. Johansson wrote:

> 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.


--
Steven

Eric S. Johansson

unread,
Jan 14, 2014, 10:07:55 PM1/14/14
to pytho...@python.org

On 1/13/2014 2:24 AM, Steven D'Aprano wrote:
> 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"

------
at the end I only care about the keys in identifier which I fill in
after user interaction.

gmflanagan

unread,
Jan 17, 2014, 1:07:07 AM1/17/14
to
On Sunday, January 12, 2014 3:08:31 PM UTC, Eric S. Johansson wrote:
> 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))

Mark Lawrence

unread,
Jan 17, 2014, 4:25:20 AM1/17/14
to pytho...@python.org
Would you please read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the
double line spacing above, thanks.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

0 new messages