Re: String formatting with the format string syntax

2010-09-15 Thread Andre Alexander Bell
On 09/15/2010 10:48 AM, Peter Otten wrote: I personally would not be too concerned about the leading underscore, but you can use string.Formatter().parse(template) instead. Thanks for this pointer. I like it this way. So if I now combine your generator with your suggestion, I end up with som

Re: String formatting with the format string syntax

2010-09-15 Thread Peter Otten
Andre Alexander Bell wrote: > On 09/15/2010 10:00 AM, Peter Otten wrote: >> def extract_names(t, recurse=1): >> for _, name, fmt, _ in t._formatter_parser(): >> if name is not None: >> yield name >> if recurse and fmt is not None: >> for name in ext

Re: String formatting with the format string syntax

2010-09-15 Thread Andre Alexander Bell
On 09/15/2010 10:00 AM, Peter Otten wrote: def extract_names(t, recurse=1): for _, name, fmt, _ in t._formatter_parser(): if name is not None: yield name if recurse and fmt is not None: for name in extract_names(fmt, recurse-1): yi

Re: String formatting with the format string syntax

2010-09-15 Thread Peter Otten
Peter Otten wrote: > Andre Alexander Bell wrote: > >> On 09/14/2010 08:20 PM, Miki wrote: >>> You can use ** syntax: >> english = {'hello':'hello'} >> s.format(**english) >> >> Thanks for your answer. Actually your answer tells me that my example >> was misleading. Consider the template

Re: String formatting with the format string syntax

2010-09-14 Thread Peter Otten
Andre Alexander Bell wrote: > On 09/14/2010 08:20 PM, Miki wrote: >> You can use ** syntax: > english = {'hello':'hello'} > s.format(**english) > > Thanks for your answer. Actually your answer tells me that my example > was misleading. Consider the template > > s = 'A template with {vari

Re: String formatting with the format string syntax

2010-09-14 Thread Andre Alexander Bell
On 09/14/2010 08:20 PM, Miki wrote: You can use ** syntax: english = {'hello':'hello'} s.format(**english) Thanks for your answer. Actually your answer tells me that my example was misleading. Consider the template s = 'A template with {variable1} and {variable2} placeholders.' I'm seeking

Re: String formatting with the format string syntax

2010-09-14 Thread Benjamin Kaplan
On Tue, Sep 14, 2010 at 3:20 PM, Thomas Jollans wrote: > On Tuesday 14 September 2010, it occurred to Miki to exclaim: >> You can use ** syntax: >> >>> english = {'hello':'hello'} >> >>> s.format(**english) > > No, you can't. This only works with dicts, not with arbitrary mappings, or > dict subcl

Re: String formatting with the format string syntax

2010-09-14 Thread Thomas Jollans
On Tuesday 14 September 2010, it occurred to Miki to exclaim: > You can use ** syntax: > >>> english = {'hello':'hello'} > >>> s.format(**english) No, you can't. This only works with dicts, not with arbitrary mappings, or dict subclasses that try to do some kind of funny stuff. > > On Sep 14,

Re: String formatting with the format string syntax

2010-09-14 Thread Miki
You can use ** syntax: >>> english = {'hello':'hello'} >>> s.format(**english) On Sep 14, 9:59 am, Andre Alexander Bell wrote: > Hello, > > I'm used to write in Python something like > >  >>> s = 'some text that says: %(hello)s' > > and then have a dictionary like > >  >>> english = { 'hello': 'h

String formatting with the format string syntax

2010-09-14 Thread Andre Alexander Bell
Hello, I'm used to write in Python something like >>> s = 'some text that says: %(hello)s' and then have a dictionary like >>> english = { 'hello': 'hello' } and get the formatted output like this: >>> s % english Occasionally I want to extract the field names from the template string. I w