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

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 2011-04-04 14:32 -0700
Organization http://groups.google.com
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>

Show all headers | 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