Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2573

Re: proposal to allow to set the delimiter in str.format to something other than curly bracket

From Peter Otten <__peter__@web.de>
Subject Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
Date 2011-04-04 18:18 +0200
Organization None
References <a227e6f7-45ae-4404-973f-b69adf156440@d19g2000yql.googlegroups.com> <mailman.180.1301894287.2990.python-list@python.org> <902f640f-7540-4309-bb54-a1e71d86bb87@d28g2000yqc.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.7.1301933897.9059.python-list@python.org> (permalink)

Show all headers | View raw


Alia Khouri wrote:

> Terry Reedy wrote:
> 
>> Just double the brackets, just as one doubles '\\' to get '\' in a
>> string.
>>
>> >>> "class {0}Model {{ public bool IsModel(){{ returntrue;
>> }}}}".format('My')
>>
>> 'class MyModel { public bool IsModel(){ returntrue; } }'
>>
> 
> Indeed, I tried that, but it means I have to double bracket all the
> csharp code which just creates more work for me.

You could automatically convert from a custom format like that in your 
original post:

import re

_lookup = {
    "[[": "{",
    "]]": "}",
    "{": "{{",
    "}": "}}",
}

def _substitute(m):
    return _lookup[m.group()]

def custom_format(template, *args, **kw):
    return (re.compile(r"\[\[|]]|\{|\}")
            .sub(_substitute, template)
            .format(*args, **kw))

code = "class [[0]]Model { public bool IsModel(){ return a[42] || true; } }"
print custom_format(code, "My")

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

proposal to allow to set the delimiter in str.format to something other than curly bracket Alia Khouri <alia_khouri@yahoo.com> - 2011-04-03 03:07 -0700
  Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Corey Richardson <kb1pkl@aim.com> - 2011-04-03 06:53 -0400
    Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Alia Khouri <alia_khouri@yahoo.com> - 2011-04-03 05:17 -0700
  Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Terry Reedy <tjreedy@udel.edu> - 2011-04-04 01:17 -0400
    Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Alia Khouri <alia_khouri@yahoo.com> - 2011-04-04 08:42 -0700
      Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Peter Otten <__peter__@web.de> - 2011-04-04 18:18 +0200
        Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Alia Khouri <alia_khouri@yahoo.com> - 2011-04-04 13:45 -0700
          Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Neil Cerutti <neilc@norwich.edu> - 2011-04-05 13:45 +0000
        Re: proposal to allow to set the delimiter in str.format to something other than curly bracket Alia Khouri <alia_khouri@yahoo.com> - 2011-04-04 14:32 -0700

csiph-web