Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63776 > unrolled thread
| Started by | "Eric S. Johansson" <esj@harvee.org> |
|---|---|
| First post | 2014-01-12 10:08 -0500 |
| Last post | 2014-01-17 09:25 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
extracting string.Template substitution placeholders "Eric S. Johansson" <esj@harvee.org> - 2014-01-12 10:08 -0500
Re: extracting string.Template substitution placeholders Steven D'Aprano <steve@pearwood.info> - 2014-01-13 07:24 +0000
Re: extracting string.Template substitution placeholders "Eric S. Johansson" <esj@harvee.org> - 2014-01-14 22:07 -0500
Re: extracting string.Template substitution placeholders gmflanagan <cyclebelfast@gmail.com> - 2014-01-16 22:07 -0800
Re: extracting string.Template substitution placeholders Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-17 09:25 +0000
| From | "Eric S. Johansson" <esj@harvee.org> |
|---|---|
| Date | 2014-01-12 10:08 -0500 |
| Subject | extracting string.Template substitution placeholders |
| Message-ID | <mailman.5370.1389539678.18130.python-list@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
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2014-01-13 07:24 +0000 |
| Message-ID | <52d394ad$0$29874$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #63776 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Eric S. Johansson" <esj@harvee.org> |
|---|---|
| Date | 2014-01-14 22:07 -0500 |
| Message-ID | <mailman.5490.1389755274.18130.python-list@python.org> |
| In reply to | #63813 |
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.
[toc] | [prev] | [next] | [standalone]
| From | gmflanagan <cyclebelfast@gmail.com> |
|---|---|
| Date | 2014-01-16 22:07 -0800 |
| Message-ID | <075b7403-a7cb-42bd-9ae9-ff3e3f3844a5@googlegroups.com> |
| In reply to | #63776 |
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))
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-01-17 09:25 +0000 |
| Message-ID | <mailman.5630.1389951008.18130.python-list@python.org> |
| In reply to | #64142 |
On 17/01/2014 06:07, gmflanagan wrote:
> 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))
>
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web