Re: String formatting with the format string syntax

2010-09-15 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 {variable1} and

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 s = 'A template with

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):

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

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

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 was used

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 p...@andre-bell.de 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':

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, 9:59 am,

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 tho...@jollybox.de 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

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