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


Groups > comp.lang.python > #2599

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!34g2000pru.googlegroups.com!not-for-mail
From Alia Khouri <alia_khouri@yahoo.com>
Newsgroups comp.lang.python
Subject Re: proposal to allow to set the delimiter in str.format to something other than curly bracket
Date Mon, 4 Apr 2011 14:32:18 -0700 (PDT)
Organization http://groups.google.com
Lines 41
Message-ID <e86c298c-069d-447a-bb8a-ea2f9fffb552@34g2000pru.googlegroups.com> (permalink)
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> <mailman.7.1301933897.9059.python-list@python.org>
NNTP-Posting-Host 77.64.117.20
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1301952738 31658 127.0.0.1 (4 Apr 2011 21:32:18 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Mon, 4 Apr 2011 21:32:18 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info 34g2000pru.googlegroups.com; posting-host=77.64.117.20; posting-account=JpguVgkAAADqN74X0DsRXOFZeQmOOovX
User-Agent G2/1.0
X-HTTP-UserAgent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:2599

Show key headers only | View raw


Peter Otten wrote:

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

<snip>

Here's a class wrapping your functionality:

import re

class Template(object):
    '''uses double brackets e.g [[ob.attr]] as delims to get
      around curly bracket ({}) collisions when generating code
    '''
    _pattern = re.compile(r"\[\[|]]|\{|\}")
    _lookup = {
        "[[": "{",
        "]]": "}",
        "{": "{{",
        "}": "}}",
    }
    def __init__(self, txt):
        self.txt = txt
        self.type = type

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

    def render(self, *args, **kwds):
        return self._pattern.sub(
            self._substitute, self.txt).format(*args, **kwds)

def test_Template():
    class P: pass
    p = P()
    p.name = 'peter'
    txt = 'hello there [[o.name]]'
    t = Template(txt)
    assert t.render(o=p) == 'hello there peter'

Back to comp.lang.python | Previous | NextPrevious 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