Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'indeed,': 0.07; 'terry': 0.07; 'bool': 0.09; 'doubles': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'def': 0.13; 'wrote:': 0.14; '"class': 0.16; '*args,': 0.16; 'brackets,': 0.16; 'post:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'reedy': 0.16; 'subject:set': 0.16; 'code': 0.22; 'convert': 0.22; 'tried': 0.27; 'skip:( 20': 0.31; 'from:addr:web.de': 0.31; 'import': 0.32; 'to:addr:python- list': 0.32; 'creates': 0.33; 'header:X-Complaints-To:1': 0.34; 'that,': 0.35; 'print': 0.35; 'skip:. 10': 0.36; 'but': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'header:Mime-Version:1': 0.39; 'header:Received:5': 0.40; 'double': 0.69; 'subject:other': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org 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: Mon, 04 Apr 2011 18:18:08 +0200 Organization: None References: <902f640f-7540-4309-bb54-a1e71d86bb87@d28g2000yqc.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ae35.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301933897 news.xs4all.nl 81478 [::ffff:82.94.164.166]:41589 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2573 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")